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

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

am 03.01.2008 10:00:16 von himanshu.garg

% 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.

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

am 03.01.2008 12:54:46 von Tad J McClellan

himanshu.garg@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/"

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

am 04.01.2008 05:56:08 von Ivan Novick

On Jan 3, 1:00 am, himanshu.g...@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

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

am 13.01.2008 15:34:28 von himanshu.garg

On Jan 4, 9:56 am, Ivan Novick wrote:
> On Jan 3, 1:00 am,himanshu.g...@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.