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

Re: globbing filehandles in arrays

by Fletch (Bishop)
on Jan 23, 2009 at 13:42 UTC ( [id://738482]=note: print w/replies, xml ) Need Help??


in reply to globbing filehandles in arrays

Well for starters you're trying to read from filehandles you've opened for writing. (And you're using C-style for loops, but that's more of a style issue :).

The cake is a lie.
The cake is a lie.
The cake is a lie.

Replies are listed 'Best First'.
Re^2: globbing filehandles in arrays
by why_bird (Pilgrim) on Jan 26, 2009 at 08:14 UTC
    Well for starters you're trying to read from filehandles you've opened for writing.
    Yes, this is true.. sorry about that (I've edited it now), it used to seem obvious to me which was which, but recently I've been getting them all backwards with the result that I've managed to write over files that I meant to read. Clever. At least I know I'm prone to do this kind of think and back up files before using them in a perl script!!

    (And you're using C-style for loops, but that's more of a style issue :).

    In mitigation, defence or whatever, I've been writing a lot of C lately. But out of interest, what is the stylistic 'problem' with that style of for loop? I kind of prefer it but am open to (logical :P) reasons why there are better ways.

    Cheers
    why_bird
    ........
    Those are my principles. If you don't like them I have others.
    -- Groucho Marx
    .......

      The PBP rationale is (very loosely (and probably poorly :) paraphrasing; see p100-101) that the Perl-style iteration is more explicit in that you can see right off what's being iterated.

      ## Compare . . . for( my $i = 0; $i < $#array; $i++ ) { frobnitz( $array[ $i ] ); } ## VS for my $item ( @array ) { frobnitz( $item ); }

      The first case the fact that it's working with each element of @array isn't as readily apparent (you've got to parse that it's looking at $#array and then match that up with the indexing); whereas the foreach-style comes right out up top and says "I'm frobnitz'ing every item of @array".

      Another thing is that in order to use the C-style loops you need to have an actual array to work with, whereas a foreach-style loop will work with any arbitrary list of values (e.g. for my $key ( sort { $h{$b} <=> $h{$a} } keys %h ) { ... }).

      Again it's just a style issue and it's not "wrong", per se; it just doesn't read as . . . "Perlish".

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-03-29 07:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found