Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Re: Stumped with $_ being modified

by ihb (Deacon)
on Jan 09, 2003 at 15:24 UTC ( [id://225555]=note: print w/replies, xml ) Need Help??


in reply to Re: Stumped with $_ being modified
in thread Stumped with $_ being modified

I too am stumped... by your reply. I don't know where to begin. How can you say that he shouldn't localize? What's wrong with localizing? I even find it hard to argue with you since it all boils down to "why are you so anti localization?" and all my arguments seem so obvious that they shouldn't have to be mentioned. I just don't see it.

How far do you take this? Should you localize filehandles? Or should you use "good names" so you don't have to localize? (In perl5.6 you're saved by the open my $fh, $file syntax, but the issue is still very interesting.)

If you steer away from using "local declared variables", do you also always use your own named variable in foreach loops? foreach my $foo (LIST) { ... }

I just wrote a note about localization and while loops; the relevant thread is here. As you see, I have the very opposite opinion of you. So I'm very curious about what I've overlooked.

Curiously,
ihb

Replies are listed 'Best First'.
Re: Stumped with $_ being modified
by tadman (Prior) on Jan 10, 2003 at 03:18 UTC
    There's a difference between lexically scoping with my and localising using local, although the terminology is one that seems rather contentious. Like the difference between arrays and lists.

    There are only a few things you still need to localise with local, such as the $" variable. Ever since 5.0 came out, a lot of effort has been put into moving people away from local and to my, for example, the way you can use lexically scoped filehandles. Where possible, it's good to use filehandles like that since they can be passed around easily from subroutine to subroutine, or stored in an object's hash easily. In this example, though, I'm using Old-School handles because it's not really an issue.

    As for loops, I won't hesitate to use $_ as long as it doesn't get too complicated. Where it's obvious what's being iterated, that is. For example:
    $_->iron() foreach (@shirts);
    You could say my $shirt, but it would be redundant. On the other hand, where there's no hint as to what you're using, a simple my declaration acts as documentation.
    foreach my $shirt ($laundry->contents()) ...
    You have to be careful with $_, just like with $1 and its relatives. Where there's risk of contamination, I use alternate names or copies. If there's no risk, then it's a matter of preference.

    I think my point is really this: Don't use local $_, instead, use a named lexical variable such as my $line.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-20 00:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found