Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^3: greater efficiency required (ls, glob, or readdir?)

by ikegami (Pope)
on Aug 27, 2008 at 18:33 UTC ( #707251=note: print w/replies, xml ) Need Help??


in reply to Re^2: greater efficiency required (ls, glob, or readdir?)
in thread greater efficiency required (ls, glob, or readdir?)

using ls has some advantages.

That's why some use glob.

Doing it in pure Perl requires several lines

It would take more than 7 lines to do the equivalent of or die $! when using cat. It's so complex you probably don't even bother doing it.

The OP's code is the perfect example. By using cat,

  • he used three lines instead of two,
  • he removed the error checking he'd do with open,
  • he introduced a lot of overhead in a loop,
  • he introduced a bug that deletes trailing blank lines, and
  • he introduced a bug for files with spaces and other special characters in their names.

Update: Added OP as an example.

Replies are listed 'Best First'.
Re^4: greater efficiency required (ls, glob, or readdir?)
by JavaFan (Canon) on Aug 27, 2008 at 19:23 UTC
    It would take more than 7 lines to do the equivalent of or die $! when using cat.
    die if $?;
    only takes one short line.
      Do you deem "Died at x.pl line y." acceptable for something that could be an I/O or spawning error? (It doesn't if it is and which it is).

        In this case, cat outputs its own error with what would be $!.

        cat: file-that-does-not-exist: No such file or directory

        It might be cleaner to "exit 1 if $?" rather than die to avoid the useless "Died" message (but then you don't get the line number).

        Update: Hmmm, when I hit 'reply', the node I replied to said only, Do you deem "Died at x.pl line y." acceptable?

        Oh, if you don't like the died message, replace it with
        exit 1 if $?;
        A few character strokes more, but still a very short line.

        And no, the program won't be silent. "cat" is perfectly able to tell the user why it can't read the file.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2020-11-27 00:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?