PDA

View Full Version : Perl Programmers... system function??



nitro_n2o
December 16th, 2007, 10:00 PM
Hi Perl programmers,
I was wondering if someone knows how to do this in perl

"ls *.txt" using the system function or something equivalent?

I want perl to execute the command with regular expression expansion. Is that possible?

like: system("ls", "*.txt"); to list all files with the extension "txt" or system("cp", "*.JPG", "*.jpg.backup");

Apparently, it doesn't work using "system" it doesn't perform the regexp expansion
any ideas?

Thanks!!

Trumpen
December 16th, 2007, 11:48 PM
Well, I am not a very good perl programmer but I think you could do either by letting bash to expand wildcards (actually the exact name should be globbing) for you


system("bash","-c","ls *.txt")

or better by using the perl funcion glob("*.txt") (or with the same result <*.txt>):


foreach $el (glob("*.txt")) {
print("$el ");
}
print("\n");

Btw, as far as I know "cp *.JPG *.jpg.backup" won't work even on a command line so you'll have to think at something else (foreach?).

slavik
December 16th, 2007, 11:58 PM
@files = <*.txt>;
print $_."\n" for(@files);

nitro_n2o
December 17th, 2007, 12:03 AM
Thx for the info this is really helpful :)

and yeah you are right cp *.JPG *.jpg.backup won't .. i just couldn't think of something else

nitro_n2o
December 17th, 2007, 12:03 AM
OMG.. you can do everything in perl :)


@files = <*.txt>;
print $_."\n" for(@files);

nhandler
December 17th, 2007, 12:35 AM
An easy way to execute commands like this from withing perl is to use ` (Note: That is shift + the key to the left of 1 and below Esc). So for example


$result=`ls *.jpg`;
$result will end up holding the output of the command.

slavik
December 17th, 2007, 01:45 AM
it is more overhead though since that is pretty much a system() call.

use glob, it's a built in function and doesn't go through the shell.

tr333
December 17th, 2007, 02:20 AM
$ perldoc -q glob # search the perldoc FAQ for 'glob'
$ perldoc -f glob # read the POD page for the 'glob' function

If you just want to copy all .jpg to .jpg.backup then BaSH might be easier.

for file in *.jpg *.JPG ; do
cp $file ${file}.backup
done

ghostdog74
December 17th, 2007, 03:14 AM
"ls *.txt" using the system function or something equivalent?


There are a few ways to do it, however, i would not encourage this method as its non portable way of doing things. use built in always, like the glob function , or the loop
eg


while (<*.txt>)
{
print $_;
}