in reply to Re: While but not for clobbers $_
in thread While but not for clobbers $_

Although I can see the point of this to determine the scope of $_, I hope to never see code like this in production. If you want to have separate variables for nested loops, name the darn things and make the maintainer's life a little easier...

But then again, I just spent some time today tracing a bug in one of my scripts where I did something very similar (stomp on scope when I didn't mean to, which would have been really obvious with named variables). So who knows, the sanity you save may be your own.

Replies are listed 'Best First'.
Re: Re: Re: While but not for clobbers $_
by BrowserUk (Patriarch) on Apr 15, 2003 at 11:16 UTC

    I whole heartedly agree.

    Whilst there are many conveniences that come from using $_, I doubt that there are many times when the benefits outweight the dangers of using 'it' nested like that. In this case I would definately name the variable on the for loop--BUT, I might explicitly localise $_ in the while loop also.

    The convenience of using 'it' within the body of the while loop when parsing files greatly simplifies and IM(NS)HO greatly clarifies the code.

    Explicitly localising it myself goes some way to reducing the potential for it tripping me up, and I can't see any downsides in doing so--even the extra 8 chars is too little to concern the lazy typists:)


    Examine what is said, not who speaks.
    1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
    2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
    3) Any sufficiently advanced technology is indistinguishable from magic.
    Arthur C. Clarke.