Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using find in perl system or qx ? 2

Status
Not open for further replies.

TheNewbie81

Technical User
Feb 12, 2007
15
IE
hi,


why in perl does this return what i want:
system(qq[find $DIR -name "file" -exec ls {} \\;]);

but this returns nothing?
qx{find $DIR -name "file" -exec ls {} \\;};


where $DIR = directory read in from input.

Its bugging me!!
 
You haven't said what you're expecting either of them to do, so we can't tell what's different about what is happening. Please elaborate on what you mean.
 
Well, for for one thing the system call returns an (empty) array, and the qx// constructs returns a number (-1 on failure, and somthing else when it succeeds...).

Does that help..?!?
 
hi sorry,

in the case of

system(qq[find $DIR -name "file" -exec ls {} \\;]);

this prints out all the files to the screen when i execute the script.

in the case of
qx{find $DIR -name "file" -exec ls {} \\;};

this prints nothing at all? I thought qx and system where similar?


 
You're mixing up "returning" and "printing". They're not the same thing at all.

qx and system are similar in that they both run an external command (but MacTommy has the differences mixed up).

qx runs the command and returns the output of the command (i.e. the list of files/directories that "find" produces). However in the code above you're not actually doing anything with that output, including printing it, so nothing further happens.

system runs the command and returns its return value (0 for success, non-zero else for failure). Again, you're not doing anything with this return value. However this time, as the find command prints to STDOUT it's not being captured by "system" (unlike qx) so it's printed directly to Perl's STDOUT.
 
cheers for the help guys,

one more thing ..

so can i have

Code:
 @list = qx{find $DIR -name "file" -exec ls {} \\;};
print "@list\n";


or am i being too simple here? :)

 
You can, by the way, also use back ticks (``) to capture some system call's output.

BTW, I remember having read somewhere that you shouldn't use any of these 'capturing constructs' if you don't care about the output (in which case you can just use system()), as Perl goes through quite some (in that case, needless) trouble in getting it for you.

There is a nice summary over here
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top