Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: eof not recognised when applying diamond operator to invocation arguments?

by Illuminatus (Curate)
on Jan 12, 2011 at 16:43 UTC ( [id://881942]=note: print w/replies, xml ) Need Help??


in reply to eof not recognised when applying diamond operator to invocation arguments?

Filters

fnord

  • Comment on Re: eof not recognised when applying diamond operator to invocation arguments?

Replies are listed 'Best First'.
Re^2: eof not recognised when applying diamond operator to invocation arguments?
by pat_mc (Pilgrim) on Jan 12, 2011 at 16:53 UTC
    Hm ... thanks, Illuminatus ... I looked at that link ... not sure it provided me with the answer (or at least - if it did, I did not get it). Could you please be a little more verbose? Your wisdom will be much appreciated!

    Pat
      When you do not explicitly open the files by name, but simply start using <>, you are reading a catenation of all of the files on the command line. Therefore, the first time through the loop, you read all of the files. As the link implied, $ARGV refers to whatever file <> is currently referencing (the last one). If you want it to work as you intend, you could do something like:
      foreach my $file (@ARGV) { open (FILE, $file) || die "could not open $file"; while (<FILE>) { print $_; } close FILE; print "whatever you wanted to use as a separator $file\n"; }

      fnord

        Style tips.
        • Using 2 argument open is potentially buggy if the file name can contain unusual characters like ">" or "|". It is therefore a good habit to use 3 argument open instead. Note that this error is implicit in <>, which is a reason not to use it. See magic-diamond <> behavior -- WHAT?! for an explanation.
        • As perlstyle says, you should always include $! in your error messages. Always, always, always.
        • I've personally encountered enough cases where a file can't be opened because the filename includes accidental whitespace that I like to always quote it in the error message.
        • There is no need to explicitly close the filehandle since Perl does it for you. (And does it quite promptly if you're using lexically scoped filehandles. Which you are not.) Unless, of course, you are concerned that the close may fail and are error checking it. If you're serious about error checking, I would recommend using autodie.
        With those changes your snippet becomes:
        foreach my $file (@ARGV) { open (FILE, "<", $file) || die "could not open '$file': $!"; while (<FILE>) { print $_; } print "whatever you wanted to use as a separator $file\n"; }
        Getting more contentious, if you spell "foreach" as "for", add the normal indentation and brace style, lexical filehandles, and convert the while into an inline form, you get the more idiomatic:
        for my $file (@ARGV) { open (my $fh, "<", $file) || die "could not open '$file': $!"; print while <$fh>; print "whatever you wanted to use as a separator $file\n"; }

        Edit: Argel is right. The for/foreach distinction belongs in the stylistic nitpicks, not in the more substantive section.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2024-04-18 21:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found