Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

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

by tilly (Archbishop)
on Jan 12, 2011 at 18:15 UTC ( #881956=note: print w/replies, xml ) Need Help??

in reply to Re^3: eof not recognised when applying diamond operator to invocation arguments?
in thread eof not recognised when applying diamond operator to invocation arguments?

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.

Replies are listed 'Best First'.
Re^5: eof not recognised when applying diamond operator to invocation arguments?
by Argel (Prior) on Jan 12, 2011 at 23:59 UTC
    Don't you think the "foreach" comment is nitpicking a bit? It's an alias to "for" and when performing the loop the the Perlish way the phrase "for each element" makes more sense than "for element". Meanwhile, when using it the repressed memory inducing, nightmarish C-style way, the phrase "for this series of numbers" is a better fit. In my experience, "foreach" is easier for people new to programming to grasp and it makes it easier to wean C-junkies off of the C-styled for loops.

    Elda Taluta; Sarks Sark; Ark Arks

      Why is it always C-style loops (usually used as counting loops) vs list iterator loops? Perl-style counting loops are always ignored. By your argument, one should use for for them. Others argue they are just optimisations of list iteration loops. It just goes to show how meaningless the choice of keyword is. In fact, you'll realise after a while that you never rely on the keyword anyway, so you should just use what's simpler ("for").

        Actually, I do use "for" for C-style iterations and "foreach" for the more Perlish iterator loops! I like to use what "feels" right -- typing four more letters isn't a big deal. And like I said, people just learning to program in my experience tend to "get it" sooner with "foreach". Quite frankly, I wish they were distinct so we could discourage "for" loops and encourage "foreach" loops without any ambiguity -- useful when dragging those kicking and screaming, die hard C coders into the world of Perl!!! :-)

        Elda Taluta; Sarks Sark; Ark Arks

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://881956]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2018-07-21 04:29 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (444 votes). Check out past polls.