Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^4: Hmmm: while(), file handle and $_ gotcha

by ikegami (Pope)
on Mar 06, 2009 at 17:48 UTC ( #748919=note: print w/replies, xml ) Need Help??


in reply to Re^3: Hmmm: while(), file handle and $_ gotcha
in thread Hmmm: while(), file handle and $_ gotcha

Using a variable is even required if you want to add error checking.

while (my $line = <DATA>) { if ( $. % 10 == 1 ) { print $line; } for (0 .. 3) { defined( my $important_line = <DATA> ) or die("Premature EOF\n"); print $important_line; } }

Replies are listed 'Best First'.
Re^5: Hmmm: while(), file handle and $_ gotcha
by Porculus (Hermit) on Mar 06, 2009 at 21:47 UTC

    For small values of "required".

    print( scalar <DATA> // die "Premature EOF\n" );

    (But don't do that, because the version with the explicit variable is obviously right, whereas the version without one forces the reader to pause and wonder whether it will work or not. So it is, I suppose, required in the sense of "required by any sane coding standards" ...)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (1)
As of 2021-12-02 01:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (16 votes). Check out past polls.

    Notices?