Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re^3: Perl Rename

by Marshall (Abbot)
on Jul 05, 2010 at 12:38 UTC ( #848057=note: print w/replies, xml ) Need Help??

in reply to Re^2: Perl Rename
in thread Perl Rename

There are several+ flavors of "glob" (various DOS globbers, BSD glob, POSIX glob) and there are differences and that sometimes matters a lot! For example some DOS globbers don't work with filenames with spaces in them!

Internally within my code, I don't use glob, preferring to open a directory and use grep{} with a regex to filter out what I want as shown below (note: file test operators can also be added to the grep, etc). This method is multi-platform and works with Perl 5.6 (even Win 98 ports). Independent of command line expansion issues (there is a good post about that already), this sort of thing happens all the time, e.g. I want to know the .backup files for my application in directory X.

Basically use glob() only when forced to do so.

#!/usr/bin/perl -w use strict; #print all files ending in .pl within C:/Temp directory my $dir = 'C:/Temp'; opendir (DIR, $dir) || die "couldn't open $dir"; my @files = grep {/\.pl$/}readdir DIR; print "@files";

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://848057]
LanX LOL of the day ... the Turkish Anonymous Alcoholics stopped their "No to alcohol" campaign for political reasons ...
LanX omg my browser is posting again on its own ...
[LanX]: choroba: GREAT JOKE! xD
LanX starts googling the punch line ...
[choroba]: suddenly, a weird noise can be heard, and the conductor stops them
[choroba]: (sorry, was interrupted by a call)
[choroba]: What was that? asks he
[choroba]: and the triangle player says, "That was me. It's written here in the score"

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2017-03-27 11:51 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (319 votes). Check out past polls.