Hi again,
I have this code from a script I want to modify:
open FP, "find . -ls |"
From what I understand, this will pipe the output of find() to the FP filehandle. Then you can loop through it like a standard text file:
while (my $file = <FP>) {
chomp $file;
...
}
What is the advantage of using
open FP, "find . -ls |" and then looping through the handle, as opposed to using backticks?
my @files = `find . -ls`;
foreach my $line (@lines) {
chomp $file;
...
}
Also, regarding system(), it returns the exact same thing as using backticks, plus the final element of @files is the exit status (0 in this case for success). I thought system() was only supposed to return an exit status, not full output
TL;DR -- why did the coder use a pipe in open() instead of using backticks or system()?
FYI, this script basically reads the find . -ls output and writes a file listing files and their sizes.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.