Why does undef->{id} give a warning and $undefined_var->{id} doesn't

% cat test.pl
use Test::Simple tests => 2;
use strict;
my $undefined_var;
ok(!$undefined_var->{id}, '!$undefined_var->{id}');
ok(!undef->{id}, '!undef->{id}')

% perl test.pl
1..2
ok 1 - !$request->{id}
Can't use an undefined value as a HASH reference at test.pl line 5.
# Looks like you planned 2 tests but only ran 1.
# Looks like your test died just after 1.
himanshu.garg [ Do, 03 Januar 2008 10:00 ] [ ID #1899177 ]

Re: Why does undef->{id} give a warning and $undefined_var->{id} doesn't

himanshu.garg [at] gmail.com <himanshu.garg [at] gmail.com> wrote:

> Subject: Why does undef->{id} give a warning and $undefined_var->{id} doesn't


Because of autovivification.


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
Tad J McClellan [ Do, 03 Januar 2008 12:54 ] [ ID #1899182 ]

Re: Why does undef->{id} give a warning and $undefined_var->{id}

On Jan 3, 1:00 am, himanshu.g... [at] gmail.com wrote:
> % cat test.pl
> use Test::Simple tests => 2;
> use strict;
> my $undefined_var;
> ok(!$undefined_var->{id}, '!$undefined_var->{id}');
> ok(!undef->{id}, '!undef->{id}')
>
> % perl test.pl
> 1..2
> ok 1 - !$request->{id}
> Can't use an undefined value as a HASH reference at test.pl line 5.
> # Looks like you planned 2 tests but only ran 1.
> # Looks like your test died just after 1.

Try running this code:

###########################
use strict;
my $x = undef;
print "$x\n";
$x->{id} = 3;
print "$x\n";
###########################

You will see $x starts as undefined but perl will create a hash when
you try to assign something to a $x->{id}. As Tad, said, they call
that autovivification which means "to bring oneself to life".

The second time you print $x it has a memory address.

You can not do the same by directly using undef as the address of a
hash because that is not a scalar variable that can hold the address
of a created hash and thus its illegal syntax.

Regards,
Ivan Novick
http://www.0x4849.net
Ivan Novick [ Fr, 04 Januar 2008 05:56 ] [ ID #1900017 ]

Re: Why does undef->{id} give a warning and $undefined_var->{id}

On Jan 4, 9:56 am, Ivan Novick <i... [at] 0x4849.net> wrote:
> On Jan 3, 1:00 am,himanshu.g... [at] gmail.com wrote:
>
> > % cat test.pl
> > use Test::Simple tests => 2;
> > use strict;
> > my $undefined_var;
> > ok(!$undefined_var->{id}, '!$undefined_var->{id}');
> > ok(!undef->{id}, '!undef->{id}')
>
> > % perl test.pl
> > 1..2
> > ok 1 - !$request->{id}
> > Can't use an undefined value as a HASH reference at test.pl line 5.
> > # Looks like you planned 2 tests but only ran 1.
> > # Looks like your test died just after 1.
>
> Try running this code:
>
> ###########################
> use strict;
> my $x = undef;
> print "$x\n";
> $x->{id} = 3;
> print "$x\n";
> ###########################
>
> You will see $x starts as undefined but perl will create a hash when
> you try to assign something to a $x->{id}. As Tad, said, they call
> that autovivification which means "to bring oneself to life".
>
> The second time you print $x it has a memory address.
>
> You can not do the same by directly using undef as the address of a
> hash because that is not a scalar variable that can hold the address
> of a created hash and thus its illegal syntax.
>
> Regards,
> Ivan Novickhttp://www.0x4849.net

Yes,

Got it now. Couldn't have been explained better.

Thank You,
Himanshu.
himanshu.garg [ So, 13 Januar 2008 15:34 ] [ ID #1906884 ]
Perl » comp.lang.perl.misc » Why does undef->{id} give a warning and $undefined_var->{id} doesn't

Vorheriges Thema: Simple "rm -rf"?
Nächstes Thema: FAQ 8.5 How do I read just one key without waiting for a return key?