Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Re: how to rename all files in a dir

by BrowserUk (Pope)
on Jul 07, 2002 at 05:01 UTC ( #179942=note: print w/replies, xml ) Need Help??


in reply to Re: how to rename all files in a dir
in thread how to rename all files in a dir

Having been bitten by the "unadorned filename" return of readdir (isn't every newbie?), I got to wondering if there is ever the situation where you do not want the the full path? If so, how often, relative to the times when you do?

I guess it's way to late to consider that this might be changed, given the golden rule that "Thou shalt not break old code".

However, as it seems that Perl6 is going to comprehensively break this rule - maybe a new default would be in order?

  • Comment on Re: Re: how to rename all files in a dir

Replies are listed 'Best First'.
Re: Re: Re: how to rename all files in a dir
by jjohn (Beadle) on Jul 07, 2002 at 13:04 UTC

    If so, how often, relative to the times when you do?

    While I share your pain (and frequently stumb my toe on this behavior), I think this idiom will help you. Use the chdir() function to get to the desired directory, then read away. That way, your can use the result of readdir directly.

    use CWD; my $org_dir = cwd; chdir($desired_dir) || die "can't cd: $!"; opendir(DIR, $desired_dir) || die "can't opendir: $!"; while(my $file = readdir DIR){ next if -d $file; # preform operations below on $file } closedir DIR; chdir($org_dir) || die "can't get back to $org_dir: $!";
Re^3: how to rename all files in a dir
by Aristotle (Chancellor) on Jul 07, 2002 at 12:40 UTC
    Personally I like it the way it is - it is much harder to remove the path from a fully specified pathname than to add the path to an unadorned one. It might be helpful to have it as an option to readdir, though.

    Makeshifts last the longest.

Re: Re: Re: how to rename all files in a dir
by blakem (Monsignor) on Jul 07, 2002 at 12:27 UTC
    Its not a matter of not wanting the full path... readdir() can't really give you the full path. It take a DIRHANDLE as an argument, and there is no fool-proof way of getting the correct "full path" from a DIRHANDLE.

    -Blake

Re: Re: Re: how to rename all files in a dir
by oakbox (Chaplain) on Jul 07, 2002 at 14:37 UTC
    The real poison pill here is symlinks. Let's say you chdir to a location on your file system and read the listings there. Perl has no way of knowing if you came to that directory through 'hard' links or symlinks. And what is returned by pwd from the command line may or may not match up with what that command will return within perl.

    So, the answer to your question is, you probably DO want the full path in many situations, but there is no way to automatically get there from within a script. You will usually end up putting in some code that forces Perl to use a particular directory location.

    oakbox

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2020-09-21 18:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If at first I donít succeed, I Ö










    Results (126 votes). Check out past polls.

    Notices?