Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > prevent further hash auto-vivification

Reply
Thread Tools

prevent further hash auto-vivification

 
 
ivowel@gmail.com
Guest
Posts: n/a
 
      11-24-2006

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

 
Reply With Quote
 
 
 
 
DJ Stunks
Guest
Posts: n/a
 
      11-24-2006

http://www.velocityreviews.com/forums/(E-Mail Removed) 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

 
Reply With Quote
 
 
 
 
boyd
Guest
Posts: n/a
 
      11-24-2006
In article <(E-Mail Removed)>,
Sherm Pendley <(E-Mail Removed)> wrote:

> (E-Mail Removed) 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
 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      11-24-2006
boyd wrote:
> In article <(E-Mail Removed)>,
> Sherm Pendley <(E-Mail Removed)> wrote:
>
> > (E-Mail Removed) 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

 
Reply With Quote
 
boyd
Guest
Posts: n/a
 
      11-24-2006
In article <(E-Mail Removed)>,
Michele Dondi <(E-Mail Removed)> wrote:

> On Fri, 24 Nov 2006 21:38:35 GMT, boyd <(E-Mail Removed)> 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
 
Reply With Quote
 
DJ Stunks
Guest
Posts: n/a
 
      11-24-2006
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

 
Reply With Quote
 
boyd
Guest
Posts: n/a
 
      11-25-2006
In article <(E-Mail Removed) om>,
"DJ Stunks" <(E-Mail Removed)> 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
 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      11-25-2006

Quoth (E-Mail Removed):
>
> 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.
[(E-Mail Removed)]
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      11-25-2006
>>>>> "b" == boyd <(E-Mail Removed)> writes:

b> In article <(E-Mail Removed) om>,
b> "DJ Stunks" <(E-Mail Removed)> 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 ------ (E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      11-25-2006
>>>>> "BM" == Ben Morrow <(E-Mail Removed)> writes:

BM> Quoth (E-Mail Removed):
>>
>> 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 ------ (E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
How to prevent duplicated entry in array of the hash Cyrus Perl Misc 19 12-22-2006 09:34 PM
How to prevent duplicated entry in array of the hash Cyrus Perl Misc 1 12-20-2006 11:47 PM
Part 2: How to prevent duplicated entry in array of the hash ...please Cyrus Perl Misc 1 12-20-2006 11:45 PM



Advertisments