Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Interacting with the shell ( backticks, system(), open -| )

by afoken (Parson)
on Feb 05, 2014 at 19:10 UTC ( #1073589=note: print w/ replies, xml ) Need Help??


in reply to Interacting with the shell ( backticks, system(), open -| )

why did the coder use a pipe in open() instead of using backticks or system()?

An even better question: Why didn't (s)he use File::Find? There is no need to spawn a child process just to find files in a directory tree.

Consider rewriting that code to use File::Find.

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)


Comment on Re: Interacting with the shell ( backticks, system(), open -| )
Download Code
Re^2: Interacting with the shell ( backticks, system(), open -| )
by jktstance (Novice) on Feb 05, 2014 at 22:44 UTC
    How could I use File::Find and still keep the feature of the pipe into a filehandle, namely the while loop only reading in one line at a time? As with using backticks, the only way I can see is to read the entire output of File::Find into an array, which could be very large.
      How could I use File::Find and still keep the feature of the pipe into a filehandle, namely the while loop only reading in one line at a time? As with using backticks, the only way I can see is to read the entire output of File::Find into an array, which could be very large.

      Do you know File::Find? If not, did you read the documentation?

      My guess is that your answers are NO and NO, and the second one makes me sad.

      File::Find has no output. File::Find uses a callback function (commonly called wanted) that is invoked exactly once for every file found. The loop that traverses the directory tree, the equivalent of the pipe open and the while / readline loop, is inside the File::Find::find funcion, so you don't need the while loop any more. The equivalent of the loop body is the callback function, this is where you process any file found by File::Find. You do not need an array.

      Consult the really good documentation of File::Find for details.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2014-12-20 13:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (95 votes), past polls