![]() |
prevent further hash auto-vivification
dear perl experts: how can I stop perl from auto-vivifying new keys in a hash? this seems like something that everyone would want as an optional feature in an object oriented context, isn't it? sincerely, /iaw |
Re: prevent further hash auto-vivification
ivowel@gmail.com wrote: > dear perl experts: how can I stop perl from auto-vivifying new keys in > a hash? this seems like something that everyone would want as an > optional feature in an object oriented context, isn't it? http://www.perlarchive.com/articles/perl/ug0002.shtml -jp |
Re: prevent further hash auto-vivification
In article <m2slg8acer.fsf@Sherm-Pendleys-Computer.local>,
Sherm Pendley <spamtrap@dot-app.org> wrote: > ivowel@gmail.com writes: > > > dear perl experts: how can I stop perl from auto-vivifying new keys in > > a hash? > > Use exists() to check for their presence before accessing them. > > perldoc -f exists > > sherm-- Not true. Exists will cause them to be vivified see the reference quoted before: http://www.perlarchive.com/articles/perl/ug0002.shtml |
Re: prevent further hash auto-vivification
boyd wrote:
> In article <m2slg8acer.fsf@Sherm-Pendleys-Computer.local>, > Sherm Pendley <spamtrap@dot-app.org> wrote: > > > ivowel@gmail.com writes: > > > > > dear perl experts: how can I stop perl from auto-vivifying new keys in > > > a hash? > > > > Use exists() to check for their presence before accessing them. > > > > perldoc -f exists > Not true. Exists will cause them to be vivified see the reference > quoted before: > http://www.perlarchive.com/articles/perl/ug0002.shtml Did you read that article? Using exists() does NOT cause a hash level to be autovivified. Accessing a level on the way towards using exists() does. Therefore, you have to use exists on all levels that you want to test: if (exists $h{'baz'} and exists $h{'baz'}{'alpha'}) { ... } rather than simply: if (exists $h{'baz'}) { ... } Paul Lalli |
Re: prevent further hash auto-vivification
In article <ogtem29b640vqt5pn30m130c3n2sufih5r@4ax.com>,
Michele Dondi <bik.mido@tiscalinet.it> wrote: > On Fri, 24 Nov 2006 21:38:35 GMT, boyd <tbmoore9@verizon.net> wrote: > > >> > dear perl experts: how can I stop perl from auto-vivifying new keys in > >> > a hash? > >> > >> Use exists() to check for their presence before accessing them. > >> > >> perldoc -f exists > >> > >> sherm-- > > > >Not true. Exists will cause them to be vivified see the reference > >quoted before: > >http://www.perlarchive.com/articles/perl/ug0002.shtml > > Not true. Using "exist() to check for their presence before accessing > them" yields a way to "stop perl from auto-vivifying new keys in a > hash". That it won't prevent autovivification at some shallower levels > when accessing a complex data structure is a whole another story and > only means that you have to apply the same technique repeatedly and if > you end up doing so quite often or otherwise find it convenient, then > write some code that will factorize that apart, or find a module that > addresses the issue. > > > Michele Sorry. I didn't read the question and/or article carefully enough. You are correct. In other words: exists($ref->{a}) will not cause $ref->{a} to be created, but exists($ref->{a}->{b}) will cause $ref->{a} to be created as an empty hash. Right? Thanks. Boyd |
Re: prevent further hash auto-vivification
boyd wrote:
> Sorry. I didn't read the question and/or article carefully enough. You > are correct. In other words: exists($ref->{a}) will not cause $ref->{a} > to be created, but exists($ref->{a}->{b}) will cause $ref->{a} to be > created as an empty hash. Right? don't ask, try! you could whip up a script to test that in about 4 lines -jp |
Re: prevent further hash auto-vivification
In article <1164412019.779359.99050@m7g2000cwm.googlegroups.c om>,
"DJ Stunks" <DJStunks@gmail.com> wrote: > boyd wrote: > > Sorry. I didn't read the question and/or article carefully enough. You > > are correct. In other words: exists($ref->{a}) will not cause $ref->{a} > > to be created, but exists($ref->{a}->{b}) will cause $ref->{a} to be > > created as an empty hash. Right? > > don't ask, try! you could whip up a script to test that in about 4 > lines > > -jp That's what I did. Except I did it within the debugger: perl -de1 I posted my question sort of rhetorically... Thanks. Boyd |
Re: prevent further hash auto-vivification
Quoth ivowel@gmail.com: > > dear perl experts: how can I stop perl from auto-vivifying new keys in > a hash? this seems like something that everyone would want as an > optional feature in an object oriented context, isn't it? sincerely, As of 5.8.0 you can use Hash::Util::lock_keys to prevent new hash keys from being added in any way (including autoviv). This feature was added to support OOP, IIRC (it is a replacement for the deprecated pseudohash semantics). Ben -- Raise your hand if you're invulnerable. [benmorrow@tiscali.co.uk] |
Re: prevent further hash auto-vivification
>>>>> "b" == boyd <tbmoore9@verizon.net> writes:
b> In article <1164412019.779359.99050@m7g2000cwm.googlegroups.c om>, b> "DJ Stunks" <DJStunks@gmail.com> wrote: >> boyd wrote: >> > Sorry. I didn't read the question and/or article carefully enough. You >> > are correct. In other words: exists($ref->{a}) will not cause $ref->{a} >> > to be created, but exists($ref->{a}->{b}) will cause $ref->{a} to be >> > created as an empty hash. Right? >> >> don't ask, try! you could whip up a script to test that in about 4 >> lines >> >> -jp b> That's what I did. Except I did it within the debugger: perl -de1 b> I posted my question sort of rhetorically... and you could read the article mentioned. it covers autoviv, exists and all related stuff. i should know. :) uri -- Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com --Perl Consulting, Stem Development, Systems Architecture, Design and Coding- Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org |
Re: prevent further hash auto-vivification
>>>>> "BM" == Ben Morrow <benmorrow@tiscali.co.uk> writes:
BM> Quoth ivowel@gmail.com: >> >> dear perl experts: how can I stop perl from auto-vivifying new keys in >> a hash? this seems like something that everyone would want as an >> optional feature in an object oriented context, isn't it? sincerely, BM> As of 5.8.0 you can use Hash::Util::lock_keys to prevent new hash keys BM> from being added in any way (including autoviv). This feature was added BM> to support OOP, IIRC (it is a replacement for the deprecated pseudohash BM> semantics). i wouldn't say hash locking had anything to do with psuedo hashes. psuedos were intended to be a fast way to do object and look up their attributes. it was an unholy mess and was rightly ripped out of perl (i think it was finally removed in 5.8 after being deprecated for years). maybe the locked hashes are useful to various OO implementation styles (inside out object don't need that AFAIK) but they aren't similar to psuedo hashes in any way i can see. uri -- Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com --Perl Consulting, Stem Development, Systems Architecture, Design and Coding- Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org |
| All times are GMT. The time now is 10:42 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.