exist function in perl 5.12.1

D

Dilbert

In perl 5.12.1, with reference to the exist function "perldoc -f
exist" ( see also http://perldoc.perl.org/functions/exists.html ) it
says
[...]
Although the mostly deeply nested array or hash will
not spring into existence just because its existence
was tested, any intervening ones will. Thus $ref->{"A"}
and $ref->{"A"}->{"B"} will spring into existence due to
the existence test for the $key element above.
[...]
This surprising autovivification in what does not at first
--or even second-- glance appear to be an lvalue context
may be fixed in a future release.

Has this particular case of surprising autovivification always
existed, even in perl 5.10 or 5.8 ?
 
J

jl_post

In perl 5.12.1, with reference to the exist function "perldoc -f
exist" ( see alsohttp://perldoc.perl.org/functions/exists.html) it
says
[...]
Although the mostly deeply nested array or hash will
not spring into existence just because its existence
was tested, any intervening ones will. Thus $ref->{"A"}
and $ref->{"A"}->{"B"}  will spring into existence due to
the existence test for the $key element above.
[...]
This surprising autovivification in what does not at first
--or even second-- glance appear to be an lvalue context
may be fixed in a future release.

Has this particular case of surprising autovivification always
existed, even in perl 5.10 or 5.8 ?


I believe so, yes. Ever since autovivification existed, this has
always been a caveat to watch out for. And I think autovivification
has been around for all of Perl 5. (But if I'm mistaken, feel free to
correct me.)

Cheers,

-- Jean-Luc
 
D

Dilbert

In perl 5.12.1, with reference to the exist function "perldoc -f
exist" ( see alsohttp://perldoc.perl.org/functions/exists.html) it
says
[...]
Although the mostly deeply nested array or hash will
not spring into existence just because its existence
was tested, any intervening ones will. Thus $ref->{"A"}
and $ref->{"A"}->{"B"}  will spring into existence due to
the existence test for the $key element above.
[...]
This surprising autovivification in what does not at first
--or even second-- glance appear to be an lvalue context
may be fixed in a future release.
Has this particular case of surprising autovivification always
existed, even in perl 5.10 or 5.8 ?

   I believe so, yes.  Ever since autovivification existed, this has
always been a caveat to watch out for.  And I think autovivification
has been around for all of Perl 5.  (But if I'm mistaken, feel free to
correct me.)

I think you are right.
I managed to dig up perldoc.com for version 5.8.8 and it has exactly
the same description with regards to the exists function:

http://perldoc.perl.org/5.8.8/functions/exists.html

Let's hope that the surprising autovivification will be fixed in Perl
5.14
 
U

Uri Guttman

D> Let's hope that the surprising autovivification will be fixed in Perl
D> 5.14

let's hope not! exists was just a function that provided no information
to the hash expression it was checking. the autoviv happens in the hash
lookup no matter what the outer code is doing. changing that could break
existing code which is a bad thing and the p5p people know that. perl 6
is planning on changing that. maybe a future perl could deal with this
but only enabled under a pragma like other new features.

uri
 
I

Ilya Zakharevich

D> Let's hope that the surprising autovivification will be fixed in Perl
D> 5.14
let's hope not!

lets hope yes
exists was just a function that provided no information
to the hash expression it was checking.

.... which in 99.9999% of cases is not what the intent was.
the autoviv happens in the hash
lookup no matter what the outer code is doing. changing that could break
existing code

True. So one may hook it on `use 5.14' or whatever.

Ilya
 
U

Uri Guttman

D> Let's hope that the surprising autovivification will be fixed in Perl
D> 5.14

IZ> lets hope yes

IZ> ... which in 99.9999% of cases is not what the intent was.

and writing a deep exists is such an easy task anyhow. doing direct deep
exists is usually poor code as well. see my autovivication tutorial for
more and the code for a deep exists.

http://sysarch.com/Perl/autoviv.txt

uri
 
D

Dr.Ruud

Dilbert said:
Let's hope that the surprising autovivification will be fixed in Perl
5.14

It is not surprising, and it should not get changed.

$ perl -MData::Dumper -wle'
my $x;
print Dumper( $x ) if $x->{a}{b}{c}{d} or $x;
'
$VAR1 = {
'a' => {
'b' => {
'c' => {}
}
}
};


Insecurity about intermediate levels, just means that your model is wrong.

You can of course write your own deep_exists() to do what you think that
you need, but you really need to go back to the drawing board.
 
I

Ilya Zakharevich

IZ> ... which in 99.9999% of cases is not what the intent was.

and writing a deep exists is such an easy task anyhow.

Technically: yes, and this is why Perl should provide this itself.

Yours,
Ilya
 
W

Willem

<about exists autovivifying intermediates>

Ilya Zakharevich wrote:
) For each person for whom it is not surprising, how many for which it is?

For each person for whom it is surprising, how many for which it is not?


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
S

sln

In perl 5.12.1, with reference to the exist function "perldoc -f
exist" ( see also http://perldoc.perl.org/functions/exists.html ) it
says
[...]
Although the mostly deeply nested array or hash will
not spring into existence just because its existence
was tested, any intervening ones will. Thus $ref->{"A"}
and $ref->{"A"}->{"B"} will spring into existence due to
the existence test for the $key element above.
[...]
This surprising autovivification in what does not at first
--or even second-- glance appear to be an lvalue context
may be fixed in a future release.

Has this particular case of surprising autovivification always
existed, even in perl 5.10 or 5.8 ?


This may be a workaround (suprisingly nitpicky to do).

-sln
-------------------

use strict;
use warnings;
use Data::Dumper;

my $x;
$x->{''}{''}{2} = '';
$x->{a} {''}{c}{d} = undef;
print Dumper( $x );

print "1> pass = ", scalar deep_exists( $x, 'a', undef, 'c'), "\n";
print "2> pass = ", scalar deep_exists( $x, '' , undef), "\n";
print "3> pass = ", scalar deep_exists( $x, '' , undef, '2', ''), "\n";

my ($pass, $found) = deep_exists($x, 'a', '', 'c', 'd');
print "4> pass = $pass, found = $found\n";
print "5> pass = ", scalar deep_exists( $x), "\n";
print "6> pass = ", scalar deep_exists(), "\n";
exit 0;

##
sub deep_exists {
return (0,0) unless @_;
my $t = shift;
my $count = map {
my $key = $_ // '';
( ref($t) eq "HASH" and exists $t->{$key} )
? $t = $t->{$key}
: ()
} @_;
my $res = (@_ && $count == @_) ? 1 : 0;
return wantarray ? ($res, $count) : $res;
}
 
I

Ilya Zakharevich

) For each person for whom it is not surprising, how many for which it is?
For each person for whom it is surprising, how many for which it is not?

0, up to experiment's errors. But you know this already; why ask?

Puzzled,
Ilya
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top