Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^3: Performance Trap - Opening/Closing Files Inside a Loop

by apotheon (Deacon)
on Dec 10, 2004 at 12:10 UTC ( #413816=note: print w/replies, xml ) Need Help??


in reply to Re^2: Performance Trap - Opening/Closing Files Inside a Loop
in thread Performance Trap - Opening/Closing Files Inside a Loop

Doesn't placing a variable declaration in the conditional statement for a conditional loop occasionally lead to strange errors?

print substr("Just another Perl hacker", 0, -2);
- apotheon
CopyWrite Chad Perrin

  • Comment on Re^3: Performance Trap - Opening/Closing Files Inside a Loop

Replies are listed 'Best First'.
Re^4: Performance Trap - Opening/Closing Files Inside a Loop
by Animator (Hermit) on Dec 10, 2004 at 12:38 UTC
    It shouldn't (or atleast not as far as I know)
Re^4: Performance Trap - Opening/Closing Files Inside a Loop
by Ven'Tatsu (Deacon) on Dec 10, 2004 at 16:47 UTC
    Variable declaration with a statment modifier can have stange effects, usally a lexical not getting cleared as expected, due to my having both runtime and compile time effects, the test only blocks the runtime effect of resetting the variable. This can be used for some interesting obfu but should be avoided in serious code IMO.
    # if EXPR is false $foo will still have the last set value for this sc +ope's (but not other scope's) $foo my $foo if EXPR; #same here if EXPR is false the first time it's tested on this pass. my $bar while EXPR;
    It's rather common to stick a declaration inside the test of a (BLOCK) loop, often seen with input loops, or looping over both keys and values of a hash at the same time.
    while (my $foo = <$fh>) { #do something } while (my ($key, $value) = each %hash) { #do other things }
Re^4: Performance Trap - Opening/Closing Files Inside a Loop
by diotalevi (Canon) on Dec 10, 2004 at 16:42 UTC

    You're thinking of my() on statements using modifiers. The problem is avoiding only calling my() sometimes. Then the variable may retain is previous value from earlier loops in a way that is declared off-limits and buggy but never fixed because it would be too much overhead to make it consistent.

    Ok

    while ( my ( $k, $v ) = each %h ) { ... } while ( my $line = <> ) { ... } for ( my $ix = 0; ...; ... ) { ... }

    Not ok

    my $... = ... if ...; ... or my $... = ...;

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2018-07-16 14:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (342 votes). Check out past polls.

    Notices?