|
Prev: How to know if data is piped into my script
Next: Matching URLs with REs (was "Some questions about q{} and qr{}").
From: Tad J McClellan on 15 Apr 2008 21:47 Gibbering <roblund(a)gmail.com> wrote: > I want to access some data from a hash, but want to build that hash's > name on the fly... I suggest that it would be of great benefit to you to stop wanting that... > here's the code: > > %hello = (a => 'doggy'); > print ${'hell' . lc 'O'}{a}; > > does the trick as does: > print %{'hell' . lc 'O'}->{a}; > > however, under "use strict", this fails, since %hello isn't declared > with "my". > > If I do put a my in front of the %hello declaration, the print > statement gives me nothing. > I have a sinking suspicion that the above code is wrong, dangerous, > and error prone since I'm not even sure why it works. Because Perl has two separate systems of variables. Your code as shown uses "package variables" while my() declares "lexical variables" instead. See: "Coping with Scoping": http://perl.plover.com/FAQs/Namespaces.html -- Tad McClellan email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/" |