Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: help please

by Revelation (Deacon)
on Mar 26, 2002 at 01:58 UTC ( #154300=note: print w/replies, xml ) Need Help??


in reply to help please

The readdir() function will return ., and .. as directories, in addition to the other directories on under that directory.. I'm not sure you want to use that, as you may end up coding a reverse recursive function, until you have done this to every directory you have access to. To fix this you may wish to do something as simple as greping to check if your result is actually there.

Also: Instead of -d $dirname, it may be simpler to just die, if the directory can't be opened?
opendir(DIR, $some_dir) || die "can't opendir $some_dir: $!";
Or you can write a function to nicely die, and output the reason for it to the user. Just remember to exit;, with that function.

so: instead of
if ( -d $dirname) { print "$dirname is a directory thankyou \n"; opendir(DIR, $dirname); @filenames = readdir(DIR); blah blah blah blah blah
You Could Use:
opendir(DIR, $dirname) || die "can't opendir $dirname: $!"; my @directory = readdir(DIR); my @filenames = grep { -f "$dirname/$_" } @directory; # Either use th +e array @directory, or rewinddir(DIR) here, and grep readdir(DIR) twi +ce. my @directories = grep {-d "$some_dir/$_" && !/^\.$/ && !/^\..$/} @di +rectory; closedir DIR;

And you'd have a list of everything in the directory, without . and .., and with diff arrays for directories and files. In fact, since you're only editing certain types of files, you could change the first grep to suit your needs, with a regex, or you could add a few greps for the differant types of files, which would let you change shtml files to html files rather easily. If you want to have only two greps, then use && regex.

It's also my opinion that your teacher will not be very happy if you use a ton of perl modules, as the point of this excersize seems to be to test your knowledge of perl, not the writer of a perl modules' knowledge. Using a perl module is like using somebody else code, which is alright in the GNU/perl world, but is not alright in school. I would advise hard coding much of it, and adding comments to show your teacher that you know perl modules.
Gyan Kapur
gyan.kapur@rhhllp.com

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://154300]
help
Chatterbox?
[Lady_Aleena]: Your Mother, how would that work? I'm having a hard time understanding the pod.
[Your Mother]: $dir->chidren, grep {} $file->slurp &c &c. :P
[Your Mother]: Stick with what you know. Or post a SOPW and if it's not complicated I guarantee someone will answer; me if it's straightforward and no one else gets it better first.
[1nickt]: ++YourMother (Grammar Vigilante of Bristol)
[Lady_Aleena]: Um, how does grep do multiline? print "$file:$_" if grep { /get_(array|hash| data)\(.+\)/ } $_; only returns the same 18 lines the command line grep does.
[Lady_Aleena]: my scratchpad has what I have so far.
[choroba]: what do you mean by multiline?
[Discipulus]: thanks 1nickt!
[choroba]: grep is a filter, it selects elements from a list based on a boolean condition
[moritz]: you need to read the file in one go to do a multiline regex match

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (11)
As of 2017-05-23 19:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?