open (XCACLS, "xcacls.txt") or die "Cant open file: $!\n";But then I get stuck. I want to pull some data out and discard and then print some of it to an HTML file. Here is a snippet of what the data looks like from the text file
Starting XCACLS.VBS (Version: 5.2) Script at 5/1/2006 11:02:33 AM Startup directory: "G:\" Arguments Used: Filename = "*.*" /L (File: "xcacls.txt") ************************************************************************** File: G:\xcacls.txt Permissions: Type Username Permissions Inheritance Allowed BUILTIN\Administrators Full Control This Folder Only Allowed \Everyone Read and Execute This Folder Only Allowed NT AUTHORITY\SYSTEM Full Control This Folder Only No Auditing set Owner: BUILTIN\Administrators ************************************************************************** ************************************************************************** Directory: G:\1500Jennilsa Permissions: Type Username Permissions Inheritance Allowed OURDOMAIN\1500Jennilsa Modify This Folder, Subfolde Allowed BUILTIN\Administrators Full Control This Folder, Subfolde Allowed \CREATOR OWNER Full Control Subfolders and Files Allowed NT AUTHORITY\SYSTEM Full Control This Folder, Subfolde No Auditing set Owner: BUILTIN\Administrators **************************************************************************I would like to pull out all the stuff at the top where it talks about starting the script etc. and basically list the folder and who has access to it. So I think I would have to have an IF statement to read through the data and then pattern match looking for G:\. I feel a little overwhelmed
Admittedly I already knew the basics of programming in Pascal, C, Fortran, cshell, Prolog and a little Miranda & Haskell.
Update: That got me to thinking that there is a step (0) - learn some good practises about how to convert your human desires into stuff that a computer can understand. Maybe Elements of Programming with Perl that Posthumous recommended might be a good introduction to programming.
Having said that, getting from University to the point where I would consider myself an accomplished Perl programmer (among other things) took the following steps:
1) Had someone lend me an old copy of Learning Perl from O'Reilly. My copy was for Perl version 4. I wouldn't recommend you use that version ;)
2) Wrote many small scripts while managing servers, building ISPs etc.
3) Intentionally avoided a book that I saw in a bookshop with a sticker that said "258 pages for only $19.95" (It was probably a SAMS publication).
4) Bought "WIN32 Perl Bookshelf" from O'Reilly (no longer in print) with the express purpose of bringing greater sanity to some Windows servers. Realised that 99% of the pages was just nicely rendered CPAN module documentation.
5) Wrapped with that useless purchase, however, was a single edition of the then premier The Perl Journal. I subscribed and discovered that reading about the crazy algorithms and problems being published in said journal were very motivating and enlightening. Current equivalents would be The (new) Perl Journal and PerlMonks (any others anyone?)
6) Bought Programming Perl and used it as a reference using Perl as a piece of my toolkit while running my own IT business.
7) Read Programming Perl cover to cover in a weekend to brush up before doing a technical test for a full time Perl job.
8) Program in Perl (wash, rinse, repeat)
That might sound like a long time (and it is!) but I'm hoping to use my journey's example to back up the advice given by others in this thread.
First you need a good introduction that doesn't trade simplicity for correctness, but also that doesn't babmboozle you with details. Learning Perl is an excellent example of this.
Then you actually need to do something with your knowledge - for business or pleasure. That's what makes it interesting, reinforces what you have learnt (in positive and negative ways ;) and keeps you hungry to learn.
Once you have done this for a while, then you can get a big, thick, detail heavy book and read through it. Because of your pracital experience it will sink in (with more than a few 'aha' moments).
When you are accomplished in programming (any language), then you will start to be able to learn the essence of new languages quickly. For instance I have read about a third of Programming Ruby and have been able to write a number of non-trivial applications in Ruby. Even now, though, I would steer well clear of any "Learn
Also remember that you never "get there" or stop learning. I recently enjoyed becomming familiar with Ruby, learning Objective-C and writing some Cocoa applications for MacOS X. The most recent Perl books I have browsed are Higher Order Perl and Perl 6 Essentials. Online I have recently re-digested Perl Guts illustrated (which now makes MUCH more sense ;)
I am learning PERL little by little.
First, there's no such "PERL" thingy. Talk about either the programming language Perl or the interpreter perl.
I get bored, so I decided to start with a task and try and work backwards.
Nice approach. People learn things by doing them, not by reading lot of books on the subject.
You'll like to know that there is plenty of documentation in your own hard drive, waiting for you to read it. You can begin by reading perldoc perl, that is a kind of index of all the other documents. Then, continue by glancing at perldoc perlintro, perldoc perlsyn, perldoc perlop and perldoc perlfunc. Of course, you can come here to ask any questions when you don't understand something. After that, you should have an overview of the language and maybe you'll want to try and experiment some things. That's a good thing. Unfortunately I cannot tell you how to run perldoc on your machine since I've never installed any perl in any Windows.
Besides this, you also should know about CPAN, the Comprehensive Perl Archive Network. It's a huge collection of so-called modules that you can install on your machine and use for whatever you want. There are modules for everything: email, FTP, encryption, compression, HTML, openGL... more often than not, when you want to do something, you'll find that there's some module that implements it for you, so that you just install it, read its doc and enjoy it.
--
David Serrano
use strict;
use warnings;
open(IN, "xcacls.txt") or die "Cant open file: $!\n";
##### Puts file into $_ all at once, rather than just a
##### line at a time.
$_ = do { local $/; };
##### Matches the section bounded by lines of 74 *'s
##### that has Directory: in its first line.
m/\*{74}\n(Directory: .*?)\*{74}/s;
##### Print the first part of the match (the first
##### section in parentheses)
print $1;
close(IN);
Welcome to Perl [jwashburn]. [ww] already gave some good advice. I'd like to add - play. Try stuff out. Do things a little bit at a time, but each time try something new. When you run into a brick wall, come here and ask about it.
For your current problem, take it a little bit at a time. So far you have a file open, but for test purposes there is a trick you can do: include the data in the program like this:
use strict;
use warnings;
while () {
chomp;
print "$_\n";
}
__DATA__
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
File: G:\xcacls.txt
Permissions:
Type Username Permissions Inheritance
Allowed BUILTIN\Administrators Full Control This Folder Only
Allowed \Everyone Read and Execute This Folder Only
Allowed NT AUTHORITY\SYSTEM Full Control This Folder Only
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
DATA is a magic file handle (Perl has a lot of magic) that you don't have to open and that is used to read the stuff following __DATA__ or __END__. The chomp removes the line seperator character sequence from the end of the string. In this case the string is in the default variable $_. So the script reads the data following __DATA__ a line at a time, removes the line end sequence from each line, then prints it (putting the line end sequence back in).
Now you can focus on finding stuff.
use strict;
use warnings;
while () {
chomp;
if (m/^File: (.*)/) {
print "$1 allows:\n";
} elsif (m/^Allowed (.*)/) {
print "$1\n";
}
}
__DATA__
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
File: G:\xcacls.txt
Permissions:
Type Username Permissions Inheritance
Allowed BUILTIN\Administrators Full Control This Folder Only
Allowed \Everyone Read and Execute This Folder Only
Allowed NT AUTHORITY\SYSTEM Full Control This Folder Only
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
The additional magic here is using if and regexes to find and extract interesting data. The ^File at the start of the regex says 'match the text File: at the start of the string. Which string? The one in the default variable $_. The (.*) is a capture match. The brackets mean 'capture whatever is matched inside the brackets'. The dot means 'match any character' (not quite the full story, but enough for now). The asterix means 'match the thing to the left as many times as possible'.
In the print the special variable $1 is used to give the stuff captured.
Ok, there's a start. The next step is to figure out what and how you want formatted using HTML and pull out the pieces, possibly using more regexs, or more likely by using substr (because the files are using fixed size fields).
After that you can put your file handling back in and print to another file rather than to the console and you should be in business.
Note that it is strongly recommended that you use the three parameter open rather than the two parameter open that you did in your sample code. THe three parameter open looks like:
open inFile, '<', 'inFileName';
It avoids various security issues and makes it clear that the file is being opened for input.
As others have said:
To that I would add:
An experienced programmer could skip straight to Programming Perl (the camel) but if, as you say, you have no programming background, you might find it a struggle. The llama is a gentler introduction.
I'm fairly new to Perl myself, but I could perhaps give you a gentle prod in the right direction:
open (XCACLS, "xcacls.txt") or die "Cant open file: $!\n"; while () { chomp; if (/regex goes here/) { code goes here } }
In place of "regex goes here", you need a regular expression.
Good luck!I am learning PERL little by little. I have no programming background and I am having a tough time
No wonder you are having a tough time. On the other hand, "little by little" is the best way to learn anything new.
Taking a task and trying to program it is the best way... reading a computer language book can be quite dull otherwise, until probably later in our journey when we might become nerdy enough to get "Design Patterns" for night-time reading. ;-)
So, you've got a task. Now, forget about the language. Convert the tast into smaller tasks, in a stilted English-like text... call them instructions... each one of them suitably small... not so small that it becomes idiotic, and not so large that it cries out to be broken up. This is pseudo-code.
For example, for the task above, the pseuod code would be...
% somehow open the file for reading % read in line by line % discard lines not wanted % transform the wanted lines into html % print % close the file
Check the pseudo-code above and refine it. Then go about finding the methods and functions that will perform the above task. Always build a small part before continuing on to the next.
Here is another suggested task -- given a date, print out a month calendar. A lot of loops and arrays will come your way, and it will be a nice little task. When you finish this, change it to a week calendar, maybe a year calendar, etc.
Check out the tutorials on this site (the best thing about Perl), and CPAN (the second best thing about Perl).
Have fun.
Here's the best piece of advice that I can give:
Try and break down the program into different tasks and write a mini-script to achieve each task. Once they're all working, write a bigger script that does all the stuff you want. As you're working on the mini-scripts, you'll realise that something you thought was one task is actually several, so break that into more mini-scripts.
Then, when you get confused, you can come to us with the mini-script instead of the big script, which makes it easier for us and more comprehensible for you.
If I was working on solving your problem, I'd probably think..
Breaking it up like this also makes it easier to know where to start by making basically everything a valid starting point.
Good luck!
--Pileofrogs
From what I can tell I need to start with open (XCACLS, "xcacls.txt") or die "Cant open file: $!\n"; But then I get stuck.
Okay, you're opening xcacls.txt for input. You can then read lines out of it with the <> operator, thusly:
You probably also want to open a file for output:
The best advice anyone has given you is to ditch the 21-Days book. The worst computer technical book I've ever read, and I've read some pretty lousy ones, was from that series (although it was the VB one, not the Perl one). In my experience, even "for Dummies" books are better than "... in 21 Days" books, but really you probably want to get an O'Reilly book, or use the online documentation, or both. Perlmonks is also a great resource, when you have specific questions or problems.
And yes, having a project to work on is a good way to learn a programming language. Each time you learn some new feature of the language, you can maybe apply it to your project, which gives you a feeling of progress. Extra bonus points if the project is a fun project, but any project at all is better than having nothing particular in mind.
my $line; # start out ignoring/suppressing all output my $show = 0; # your code to open XACLS goes here while (defined($line =I'm just not sure what it is you're after.)) { # this bit ignores lines of stars # it also turns on output when it "sees (8) stars" if ($line =~ /^\*{8}/) { $show = 1; next; } # note: you could also turn $show on and off to # ignore entire sections, if that's what you need. # just stick the appropriate variation of the # above if-statement and have it turn $show on/off # as needed. # you can suppress other types of lines by adding # more tests, like this ... if ($line =~ /^No Auditing/) { next; } # only print if the $show flag is 1 if ($show) { print $line; } } # close XACLS here
perlmonks.org content © perlmonks.org and aufflick, chanio, GrandFather, Hue-Bond, jonadab, jwashburn, MonkE, pileofrogs, Polonius, Posthumous, punkish, TedPride, Tobin Cataldo, Trix606, ww
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03