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

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

by webfiend (Vicar)
on Mar 06, 2009 at 17:40 UTC ( #748917=note: print w/replies, xml ) Need Help??


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

Hold up. How does one variable - two, if you did what I do and use a variable for the outer read as well - equate to clutter? I'd probably remove the unused $cnt if I was going to knock clutter out.

use Modern::Perl; while (my $line = <DATA>) { if ( $. % 10 == 1 ) { print $line; } for (0 .. 3) { my $important_line = <DATA>; print $important_line; } }

I realize this is a matter of personal aesthetics, of course. I was just a little confused how declaring and using a variable (and avoiding weird behavior from <DATA>) was better than declaring an unused variable and getting the weird behavior.

Replies are listed 'Best First'.
Re^4: Hmmm: while(), file handle and $_ gotcha
by ikegami (Pope) on Mar 06, 2009 at 17:48 UTC

    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; } }

      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
Node Status?
node history
Node Type: note [id://748917]
help
Chatterbox?
[perldigious]: On occasion my scripts break, because they will do something that seems laregly pointless to me... like in this case, changing a column name from "ABC Category" to "ABC Category Override".
[perldigious]: Being the ignorant one, my initial reaction to that is, "that's sort of a dick move, a lot of people like me expect column names like that to stay the same, why the hell would you change them like that for seemingly no good reason?"
[perldigious]: Or is my irritation/ frustration well founded?
[Corion]: perldigious: That seems to be more the export and likely it's the recipients of that export that like the titles changes
[Corion]: ... "changed"
[Corion]: I usually expect fixed header names, but am sometimes lenient in the order of columns. But changing the report titles often sounds to me as if you are not the sole consument of the export ;)
[shmem]: perldigious: as always - if it ain't broke, don't fix it. Ther must be a very compelling reason for changing column names in a database. Those are rare.

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (8)
As of 2017-05-25 13:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?