Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

PERL newbie Cant figure out where to start

by jwashburn (Acolyte)
on May 01, 2006 at 18:09 UTC ( #546707=perlquestion: print w/ replies, xml ) Need Help??
jwashburn has asked for the wisdom of the Perl Monks concerning the following question:

I am learning PERL little by little. I have no programming background and I am having a tough time navigating through my Learn PERL in 21 days book because I get bored, so I decided to start with a task and try and work backwards. I am a windows administrator and I have a text file that I have created that I would like to parse through and output into HTML to make it look a little nicer for our execs to look at. I keep getting stuck at where to start. 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. 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 Onl +y Allowed \Everyone Read and Execute This Folder Onl +y Allowed NT AUTHORITY\SYSTEM Full Control This Folder Onl +y No Auditing set Owner: BUILTIN\Administrators ********************************************************************** +**** ********************************************************************** +**** Directory: G:\1500Jennilsa Permissions: Type Username Permissions Inheritance + Allowed OURDOMAIN\1500Jennilsa Modify This Folder, Sub +folde Allowed BUILTIN\Administrators Full Control This Folder, Su +bfolde Allowed \CREATOR OWNER Full Control Subfolders and +Files Allowed NT AUTHORITY\SYSTEM Full Control This Folder, Su +bfolde 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

Comment on PERL newbie Cant figure out where to start
Select or Download Code
Re: PERL newbie Cant figure out where to start
by ww (Bishop) on May 01, 2006 at 18:28 UTC
    welcome, jwashburn
    1. Use 21 days as kindling for a nice bonfire (Personal view, YMMV)
      (Update, upon mature reconsideration and a few LARTings: Lay it aside, for now, against a day when it may read more compellingly for you. My response is not a learned critique of the coding therein but I too found it difficult to focus upon -- and that's an issue of personal style.)
    2. Try Learning Perl from O'Reilly ...
    3. Overcome your boredom -- thru willpower or by getting a good intro. Learning a language takes serious attention to detail and not a little concentrated work.
    You should also check out the Tutorials here at the Monastery...
      Very good advice, that a newcomer would do well to follow.

      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 <insert language> in 48 hours". All the ones I have seen read much more like "Learn <insert language> in 48 hours and then write a book on it".

      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 ;)

Re: PERL newbie Cant figure out where to start
by Hue-Bond (Priest) on May 01, 2006 at 18:29 UTC
    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

      C:\ perldoc perldoc for reading about perldoc, and
      C:\ perldoc -f <topic>
Re: PERL newbie Cant figure out where to start
by TedPride (Priest) on May 01, 2006 at 19:11 UTC
    If I'm correct, you want the script to open the file and print just the permissions listing for the directory. The following will work:
    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 $/; <IN> }; ##### 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);
Re: PERL newbie Cant figure out where to start
by GrandFather (Cardinal) on May 01, 2006 at 19:12 UTC

    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 (<DATA>) { chomp; print "$_\n"; } __DATA__ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +* * File: G:\xcacls.txt Permissions: Type Username Permissions Inheritance + Allowed BUILTIN\Administrators Full Control This Folder Onl +y Allowed \Everyone Read and Execute This Folder Onl +y Allowed NT AUTHORITY\SYSTEM Full Control This Folder Onl +y * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +* *

    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 (<DATA>) { 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 Onl +y Allowed \Everyone Read and Execute This Folder Onl +y Allowed NT AUTHORITY\SYSTEM Full Control This Folder Onl +y * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +* *

    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.


    DWIM is Perl's answer to Gödel
Re: PERL newbie Cant figure out where to start
by Polonius (Friar) on May 01, 2006 at 19:25 UTC
    jwashburn,

    As others have said:

    • It's Perl, not PERL.
    • Try O'Reilly's Learning Perl (the llama).

    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 (<XCACLS>) { chomp; if (/regex goes here/) { code goes here } }

    In place of "regex goes here", you need a regular expression.

    Good luck!
    Polonius
Re: PERL newbie Cant figure out where to start
by punkish (Priest) on May 01, 2006 at 20:58 UTC
    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.

    --

    when small people start casting long shadows, it is time to go to bed
Re: PERL newbie Cant figure out where to start
by pileofrogs (Priest) on May 02, 2006 at 02:04 UTC

    Here's the best piece of advice that I can give:

    Fulfill Each Requirement Separately

    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..

    1. read the file and print each line
    2. write a script that can understand a line like Allowed  OURDOMAIN\1500Jennilsa Modify This Folder, Sub
    3. write a script that takes the values from the script above and prints the correct HTML
    4. do the above two steps for lines like Directory: G:\1500Jennilsa and Startup directory: and so on.
    5. Somehow your script has to know that Allowed  BUILTIN\Administrators  Full Control  This Folder Only is a property of File: G:\xcacls.txt. Write a script that shows how you handle that.

    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

Re: PERL newbie Cant figure out where to start
by Posthumous (Scribe) on May 02, 2006 at 02:53 UTC
    I was in your shoes a few years ago. The monks who have commented all provide excellent advice...punkish shows you how to take the first steps--outline what you want your script to do before you dive in. GrandFather reveals some of Perl's magic that makes your experimentation and testing easier. The others give you some specific coding ideas. So, how can I contribute? Two thoughts:
    1. I'd recommend Elements of Programming with Perl by Andrew L. Johnson. It assumes no prior programming knowledge, so you get a 2fer -- programming concepts and Perl. It's also an excellent reference you'll keep using once you've got the fundamentals down.
    2. Subscribe to the Perl Beginner's list and read, read, read.
Re: PERL newbie Cant figure out where to start
by jonadab (Parson) on May 02, 2006 at 11:43 UTC
    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:

    $line = <XCACLS>

    You probably also want to open a file for output:

    open HTML, '>', "xcacls.html" or die "Cannot write xcacls.html: $!\n";

    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.


    Sanity? Oh, yeah, I've got all kinds of sanity. In fact, I've developed whole new kinds of sanity. Why, I've got so much sanity it's driving me crazy.
Re: PERL newbie Cant figure out where to start
by Tobin Cataldo (Monk) on May 02, 2006 at 13:41 UTC
    First think filehandles. And you will need to see the listing of << < >> > +<< etc. I think it is easier to deal with chunky bits of text, run some regex etc. The while{} is initializing $line with a chunk of new text terminated by \n (and carriage return linefeed I think). If I had an address in variable $content like .. = "c:/temp/text.txt" then open would have open(my $fh1, "<", $content). If I wanted to actually parse the contents of a variable like $content = "a lot of text strings etc packed in a variable" then I would send a reference like open(my $fh1, "<", \$content). Make sure you close it when done. open(my $fh1, "<", \$content) or die('can not open content'); while (my $line = <$fh1>) { do something with $line } close($fh1);
Re: PERL newbie Cant figure out where to start
by Trix606 (Monk) on May 02, 2006 at 13:55 UTC
    I second all the great suggestions above and I would also like to add "Data Munging with Perl" by David Cross ISBN 1930110006. While this book is no substitute for "Learning Perl" it will give you hands-on guidance for the task that you specified and more.
Re: PERL newbie Cant figure out where to start
by chanio (Priest) on May 04, 2006 at 05:04 UTC
    I would like to add my own experience to all the wise previous advices:

    • * I find it better to read a 'solid' book than a virtual one. I can read it in a Public Library. And take my own notes. Try scraching some lines of code over a paper. And after erasing and writting a lot, try running it
    • * There is an inmediate execution of some little perl code that is called 'One-liner'. Just try from the command line to write the following: perl -e"print(\"Hello World!\n\")" And see how many things you could try before adding them to your script.
    You should try everything that you learn.Enjoy!

Re: PERL newbie Cant figure out where to start
by MonkE (Hermit) on May 04, 2006 at 21:18 UTC
    I'm not sure what it is you're trying to do. If you posted a bit of what you'd like you output to be, maybe that would help. In the meantime try this snippet of code. It ignores everything until the first line of asterisks. It also supresses printing of the lines of asterisks themselves as well.
    my $line; # start out ignoring/suppressing all output my $show = 0; # your code to open XACLS goes here while (defined($line = <XACLS>)) { # 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
    I'm just not sure what it is you're after.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://546707]
Approved by davidrw
Front-paged by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2014-08-30 12:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (293 votes), past polls