Using an undefined value as a hash reference

Hi,

I made the following test script:

use strict;
use warnings FATAL =3D> 'all';
use LWP::UserAgent;

my $fields;

my $ua =3D LWP::UserAgent->new;
my $res =3D $ua->get( 'http://www.google.com/', %$fields );

This script runs with no errors, although the variable $fields is =
undefined and it is used as a hash reference and I don't understand why.

If I use it in the following line however, it gives the error "Can't use =
an undefined value as a HASH reference":
my %params =3D %$fields;

Does anyone have an explanation why in the first example it works fine?

Thanks.

Octavian


--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Octavian Rasnita [ So, 07 August 2011 11:28 ] [ ID #2063077 ]

Re: Using an undefined value as a hash reference

Hi Octavian,

On Sun, 7 Aug 2011 12:28:27 +0300
"Octavian Rasnita" <orasnita [at] gmail.com> wrote:

> Hi,
>
> I made the following test script:
>
> use strict;
> use warnings FATAL =3D> 'all';
> use LWP::UserAgent;
>
> my $fields;
>
> my $ua =3D LWP::UserAgent->new;
> my $res =3D $ua->get( 'http://www.google.com/', %$fields );
>
> This script runs with no errors, although the variable $fields is undefin=
ed
> and it is used as a hash reference and I don't understand why.
>
> If I use it in the following line however, it gives the error "Can't use =
an
> undefined value as a HASH reference": my %params =3D %$fields;
>
> Does anyone have an explanation why in the first example it works fine?
>

I've simplified the program to:

[CODE]
#!/usr/bin/perl

use strict;
use warnings FATAL =3D> 'all';

my $fields;

sub mysub
{
return;
}

mysub('hello', %$fields);
# my [at] x =3D ('hello', %$fields);

print("Hello\n");
[/CODE]

This is the IRC talk on #p5p about it:

[QUOTE]
* Now talking on #p5p
* Topic for #p5p is: git://perl5.git.perl.org/perl.git | "Well volunteered"
* Topic for #p5p set by Nicholas!wLzIG6eR2Z [at] colon.colondot.net at Mon Aug 1
17:37:33 2011
<rindolf> Hi all. From beginners [at] perl.org, why doesn't this program throw an
exception: http://paste.debian.net/125354/ .
<dipsy> [ debian Pastezone ]
<vincent> autovivification does not warn
<rindolf> vincent: how is that autovivification?
<vincent> look at $fields at the end
<rindolf> vincent: oh, so it creates an empty hash?
<rindolf> vincent: but it's OK if I do my [at] a =3D (%$fields) - then it warns.
<vincent> because in that case %$fields is not a lvavlue
<vincent> s,vl,l,
<vincent> perl must autovivify when %$fields is passed as an argument, beca=
use
you could do [at] _ =3D (key =3D> $value) in your sub
<rindolf> vincent: ah.
<vincent> well, it doesn't work. but argument lists are lvalue contexts, so=
it
could be argued that it ought to work that way
<rindolf> vincent: OK.
<rindolf> vincent: well, it seems like a leaky abstraction and it's another
reason why one should use =C2=ABmy ($self, $args) =3D [at] _;=C2=BB instead of =
=C2=ABmy ($self,
%args) =3D [at] _;=C2=BB.
<rindolf> vincent: thanks for the explanation.
<vincent> please don't advocate anything in my name
<rindolf> vincent: what?
[/QUOTE]

Regards,

Shlomi Fish


--
------------------------------------------------------------ -----
Shlomi Fish http://www.shlomifish.org/
Optimising Code for Speed - http://shlom.in/optimise

The X in XSLT stands for eXtermination.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Shlomi Fish [ So, 07 August 2011 14:52 ] [ ID #2063078 ]

Re: Using an undefined value as a hash reference

Hi Shlomi,

Thanks for the explanation.

> <rindolf> vincent: well, it seems like a leaky abstraction and it's =
another
> reason why one should use =C2=ABmy ($self, $args) =3D [at] _;=C2=BB =
instead of =C2=ABmy ($self,
> %args) =3D [at] _;=C2=BB.

Yes, in LWP::UserAgent::get I've seen:

my($self, [at] parameters) =3D [at] _;

so just like in case it would have used %parameters instead of =
[at] parameters... it doesn't break.

Octavian

----- Original Message -----
From: "Shlomi Fish" <shlomif [at] shlomifish.org>
To: "Octavian Rasnita" <orasnita [at] gmail.com>
Cc: <beginners [at] perl.org>
Sent: Sunday, August 07, 2011 3:52 PM
Subject: Re: Using an undefined value as a hash reference


Hi Octavian,

On Sun, 7 Aug 2011 12:28:27 +0300
"Octavian Rasnita" <orasnita [at] gmail.com> wrote:

> Hi,
>
> I made the following test script:
>
> use strict;
> use warnings FATAL =3D> 'all';
> use LWP::UserAgent;
>
> my $fields;
>
> my $ua =3D LWP::UserAgent->new;
> my $res =3D $ua->get( 'http://www.google.com/', %$fields );
>
> This script runs with no errors, although the variable $fields is =
undefined
> and it is used as a hash reference and I don't understand why.
>
> If I use it in the following line however, it gives the error "Can't =
use an
> undefined value as a HASH reference": my %params =3D %$fields;
>
> Does anyone have an explanation why in the first example it works =
fine?
>

I've simplified the program to:

[CODE]
#!/usr/bin/perl

use strict;
use warnings FATAL =3D> 'all';

my $fields;

sub mysub
{
return;
}

mysub('hello', %$fields);
# my [at] x =3D ('hello', %$fields);

print("Hello\n");
[/CODE]

This is the IRC talk on #p5p about it:

[QUOTE]
* Now talking on #p5p
* Topic for #p5p is: git://perl5.git.perl.org/perl.git | "Well =
volunteered"
* Topic for #p5p set by Nicholas!wLzIG6eR2Z [at] colon.colondot.net at Mon =
Aug 1
17:37:33 2011
<rindolf> Hi all. From beginners [at] perl.org, why doesn't this program =
throw an
exception: http://paste.debian.net/125354/ .
<dipsy> [ debian Pastezone ]
<vincent> autovivification does not warn
<rindolf> vincent: how is that autovivification?
<vincent> look at $fields at the end
<rindolf> vincent: oh, so it creates an empty hash?
<rindolf> vincent: but it's OK if I do my [at] a =3D (%$fields) - then it =
warns.
<vincent> because in that case %$fields is not a lvavlue
<vincent> s,vl,l,
<vincent> perl must autovivify when %$fields is passed as an argument, =
because
you could do [at] _ =3D (key =3D> $value) in your sub
<rindolf> vincent: ah.
<vincent> well, it doesn't work. but argument lists are lvalue contexts, =
so it
could be argued that it ought to work that way
<rindolf> vincent: OK.
<rindolf> vincent: well, it seems like a leaky abstraction and it's =
another
reason why one should use =C2=ABmy ($self, $args) =3D [at] _;=C2=BB instead =
of =C2=ABmy ($self,
%args) =3D [at] _;=C2=BB.
<rindolf> vincent: thanks for the explanation.
<vincent> please don't advocate anything in my name
<rindolf> vincent: what?
[/QUOTE]

Regards,

Shlomi Fish


--
------------------------------------------------------------ -----
Shlomi Fish http://www.shlomifish.org/
Optimising Code for Speed - http://shlom.in/optimise

The X in XSLT stands for eXtermination.

Please reply to list if it's a mailing list post - http://shlom.in/reply =
..

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Octavian Rasnita [ So, 07 August 2011 17:15 ] [ ID #2063079 ]
Perl » gmane.comp.lang.perl.beginners » Using an undefined value as a hash reference

Vorheriges Thema: including Matlab .m file in perl program
Nächstes Thema: Setting $! in xs