Using an undefined value as a hash reference
am 07.08.2011 11:28:27 von Octavian Rasnita
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@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: Using an undefined value as a hash reference
am 07.08.2011 14:52:15 von Shlomi Fish
Hi Octavian,
On Sun, 7 Aug 2011 12:28:27 +0300
"Octavian Rasnita" wrote:
> Hi,
>=20
> I made the following test script:
>=20
> use strict;
> use warnings FATAL =3D> 'all';
> use LWP::UserAgent;
>=20
> my $fields;
>=20
> my $ua =3D LWP::UserAgent->new;
> my $res =3D $ua->get( 'http://www.google.com/', %$fields );
>=20
> 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.
>=20
> 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;
>=20
> Does anyone have an explanation why in the first example it works fine?
>=20
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 @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@colon.colondot.net at Mon Aug 1
17:37:33 2011
Hi all. From beginners@perl.org, why doesn't this program throw an
exception: http://paste.debian.net/125354/ .
[ debian Pastezone ]=20
autovivification does not warn
vincent: how is that autovivification?
look at $fields at the end
vincent: oh, so it creates an empty hash?
vincent: but it's OK if I do my @a =3D (%$fields) - then it warns.
because in that case %$fields is not a lvavlue
s,vl,l,
perl must autovivify when %$fields is passed as an argument, beca=
use
you could do @_ =3D (key =3D> $value) in your sub
vincent: ah.
well, it doesn't work. but argument lists are lvalue contexts, so=
it
could be argued that it ought to work that way
vincent: OK.
vincent: well, it seems like a leaky abstraction and it's another
reason why one should use «my ($self, $args) =3D @_;» instead of =
«my ($self,
%args) =3D @_;».
vincent: thanks for the explanation.
please don't advocate anything in my name
vincent: what?
[/QUOTE]
Regards,
Shlomi Fish
--=20
------------------------------------------------------------ -----
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@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: Using an undefined value as a hash reference
am 07.08.2011 17:15:46 von Octavian Rasnita
Hi Shlomi,
Thanks for the explanation.
> vincent: well, it seems like a leaky abstraction and it's =
another
> reason why one should use «my ($self, $args) =3D @_;» =
instead of «my ($self,
> %args) =3D @_;».
Yes, in LWP::UserAgent::get I've seen:
my($self, @parameters) =3D @_;
so just like in case it would have used %parameters instead of =
@parameters... it doesn't break.
Octavian
----- Original Message -----=20
From: "Shlomi Fish"
To: "Octavian Rasnita"
Cc:
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" wrote:
> Hi,
>=20
> I made the following test script:
>=20
> use strict;
> use warnings FATAL =3D> 'all';
> use LWP::UserAgent;
>=20
> my $fields;
>=20
> my $ua =3D LWP::UserAgent->new;
> my $res =3D $ua->get( 'http://www.google.com/', %$fields );
>=20
> 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.
>=20
> 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;
>=20
> Does anyone have an explanation why in the first example it works =
fine?
>=20
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 @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@colon.colondot.net at Mon =
Aug 1
17:37:33 2011
Hi all. From beginners@perl.org, why doesn't this program =
throw an
exception: http://paste.debian.net/125354/ .
[ debian Pastezone ]=20
autovivification does not warn
vincent: how is that autovivification?
look at $fields at the end
vincent: oh, so it creates an empty hash?
vincent: but it's OK if I do my @a =3D (%$fields) - then it =
warns.
because in that case %$fields is not a lvavlue
s,vl,l,
perl must autovivify when %$fields is passed as an argument, =
because
you could do @_ =3D (key =3D> $value) in your sub
vincent: ah.
well, it doesn't work. but argument lists are lvalue contexts, =
so it
could be argued that it ought to work that way
vincent: OK.
vincent: well, it seems like a leaky abstraction and it's =
another
reason why one should use «my ($self, $args) =3D @_;» instead =
of «my ($self,
%args) =3D @_;».
vincent: thanks for the explanation.
please don't advocate anything in my name
vincent: what?
[/QUOTE]
Regards,
Shlomi Fish
--=20
------------------------------------------------------------ -----
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@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/