Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: The default hash - accident, coincidence or conspiracy?

by tlm (Prior)
on Jun 20, 2005 at 11:58 UTC ( #468290=note: print w/ replies, xml ) Need Help??


in reply to The default hash - accident, coincidence or conspiracy?

FWIW, I have seen CPAN modules use %_, though right off the bat I can't remember which. My point is that you can't assume it is free for the taking.

Personally, I'd stay away from using variables corresponding to unused slots of special variable globs. It's asking for trouble, IMO.

the lowliest monk


Comment on Re: The default hash - accident, coincidence or conspiracy?
Re^2: The default hash - accident, coincidence or conspiracy?
by jdporter (Canon) on Jul 02, 2005 at 17:39 UTC

    It is free for the using, if not taking. The local keyword is there to facilitate multiple code chunks using the same global variable independently. It works well enough for $_ and @_, it will work just as well for %_. (That's not to say it's a panacea, though.)

    I don't understand why people think it's asking for trouble. It is no more asking for trouble than using $_ — and I sure wouldn't counsel anyone to avoid using $_. Quite the opposite, in fact.

    That being said, there isn't any really good reason to use %_. The situations in which it would be the most obvious, "natural" name for a variable must be rare indeed.

      I don't understand why people think it's asking for trouble.

      IMO, using package variables in one's code, in general, is asking for trouble (though there may be situations in which one is willing to accept this trouble). Moreover, as I pointed out, some module authors have had the bright idea of using variables such as %_, so one can't be sure that these variables are completely available, and that the other code that uses it is being good about using local whenever it uses the same variables. IMO, having to even worry about these things falls into the category of "trouble." The fact that the docs specifically state that the variables the OP asked about are reserved for Perl's use makes it all the more troublesome to use them.

      the lowliest monk

        Sure, and some module authors have had the bright idea of using $_. If you use a module that uses $_ (or any other global) and they haven't properly localized, then maybe you should rethink using that module. For better or worse, that's the way it is in the perl world. It's true for $_, and it's just as true for %_.

        You know, just because a module is out there doesn't mean people should use it. Would you use a module that didn't use strict and made all its variables global in %main:: ? I sure wouldn't.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (10)
As of 2014-09-17 19:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (95 votes), past polls