>>>>> "PS" == Peter Scott <[email protected]> writes:
PS> On Mon, 26 Apr 2010 22:16:27 +0800, Tim Bowden wrote:
>> I've just realised I almost never use named arrays or hashes anymore.
>> It's almost always anonymous references instead. That lead me to wonder
>> what criteria experienced perl hackers have as to when to use a named
>> array or hash, and when to start with an anonymous ref instead. My very
>> informal criteria tends to be to use an anonymous ref from the start if
>> I'm going to be passing it to a sub, and a named array or hash
>> otherwise. I've found the former to be much more common. Thoughts?
PS> I create arrays and hashes by default, not references to anonymous
PS> versions. I'd sooner not be putting arrows in unnecessarily. I can
PS> always enreference an aggregate in the call to a sub.
my choice is usually based on usage. if i am building up data
structures, the members are almost always anon refs - no need to have
named vars for them. if i am passing stuff around you almost have to use
refs. if i have a need for scoped data then named vars are usually
best. same for file lexicals in general. but again, if one was being set
via a sub i will pass a ref (e.g. a parse_args sub that uses a getopt
will return a hash ref of all the options for use by the program).
you never NEED anon refs as you can always make one from my declaring a
named variable and getting and keeping a reference to it. the next time
the my is executed perl will allocate a new variable and the previous
content will now be anonymous. an example is
$ref = do{ \my %foo } ; # same as $ref = {}
the [] and {} syntax is therefore very nice sugar that i would surely
miss if not allowed.
uri
--
Uri Guttman ------ [email protected] -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/