Hi Octavian,

On Sun, 7 Aug 2011 12:28:27 +0300
"Octavian Rasnita" <orasn...@gmail.com> wrote:

> Hi,
> 
> I made the following test script:
> 
> use strict;
> use warnings FATAL => 'all';
> use LWP::UserAgent;
> 
> my $fields;
> 
> my $ua = LWP::UserAgent->new;
> my $res = $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 = %$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 => 'all';

my $fields;

sub mysub
{
    return;
}

mysub('hello', %$fields);
# my @x = ('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!wlzig6e...@colon.colondot.net at Mon Aug  1
17:37:33 2011
<rindolf> Hi all. From beginners@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 @a = (%$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 @_ = (key => $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 «my ($self, $args) = @_;» instead of «my ($self,
%args) = @_;».
<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-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to