Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: PERL newbie Cant figure out where to start

by GrandFather (Saint)
on May 01, 2006 at 19:12 UTC ( [id://546736]=note: print w/replies, xml ) Need Help??


in reply to PERL newbie Cant figure out where to start

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://546736]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2024-04-16 06:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found