question on de-referencing expressions

O

olingaa

Is there a way do this with one assignment so that the $hash_ref
variable is not necessary?

my $hash_ref = functionReturningReference();
my %hash = %$hash_ref;
 
J

Jens Thoms Toerring

Is there a way do this with one assignment so that the $hash_ref
variable is not necessary?
my $hash_ref = functionReturningReference();
my %hash = %$hash_ref;

my %hash = %{ functionReturningReference() };

Regards, Jens
 
G

Gunnar Hjalmarsson

Is there a way do this with one assignment so that the $hash_ref
variable is not necessary?

my $hash_ref = functionReturningReference();
my %hash = %$hash_ref;

my %hash = %{ functionReturningReference() };
 
J

Jürgen Exner

Is there a way do this with one assignment so that the $hash_ref
variable is not necessary?

my $hash_ref = functionReturningReference();
my %hash = %$hash_ref;

See "perldoc perlreftut",
Using References -> Use Rule 1

jue
 
T

Ted Zlatanov

JTT> my %hash = %{ functionReturningReference() };


GH> my %hash = %{ functionReturningReference() };

It's worth noting here that if the returned value is undef, the program
will die here. This is why I always use an intermediate variable and
test if for definedness, even if it takes a few more lines.

Ted
 
U

Uri Guttman

TZ> On 12 Feb 2009 22:32:12 GMT (e-mail address removed) (Jens Thoms Toerring) wrote:

JTT> my %hash = %{ functionReturningReference() };


GH> my %hash = %{ functionReturningReference() };

TZ> It's worth noting here that if the returned value is undef, the program
TZ> will die here. This is why I always use an intermediate variable and
TZ> test if for definedness, even if it takes a few more lines.

where did you get that result?

perl -e '%h = %{$x}'
perl -e '%h = %{undef}'

neither caused a die.

uri
 
T

Tim McDaniel

GH> my %hash = %{ functionReturningReference() };

TZ> It's worth noting here that if the returned value is undef, the program
TZ> will die here. This is why I always use an intermediate variable and
TZ> test if for definedness, even if it takes a few more lines.

where did you get that result?

perl -e '%h = %{$x}'
perl -e '%h = %{undef}'

neither caused a die.

-w and "use strict" show different things. Note that the first test
shows that %{undef} doesn't mean what you and I thought it meant.

$ perl -we '%h = %{undef}'
Ambiguous use of %{undef} resolved to %undef at -e line 1.
Name "main::h" used only once: possible typo at -e line 1.
Name "main::undef" used only once: possible typo at -e line 1.

$ perl -we 'my %h = %{undef()}'
Use of uninitialized value at -e line 1.

$ perl -e 'my %h = %{undef()}'
[no output]

$ perl -we 'use strict; my %h = %{undef()}'
Can't use an undefined value as a HASH reference at -e line 1.
 
S

sln

GH> my %hash = %{ functionReturningReference() };

TZ> It's worth noting here that if the returned value is undef, the program
TZ> will die here. This is why I always use an intermediate variable and
TZ> test if for definedness, even if it takes a few more lines.

where did you get that result?

perl -e '%h = %{$x}'
perl -e '%h = %{undef}'

neither caused a die.

-w and "use strict" show different things. Note that the first test
shows that %{undef} doesn't mean what you and I thought it meant.

$ perl -we '%h = %{undef}'
Ambiguous use of %{undef} resolved to %undef at -e line 1.
Name "main::h" used only once: possible typo at -e line 1.
Name "main::undef" used only once: possible typo at -e line 1.

$ perl -we 'my %h = %{undef()}'
Use of uninitialized value at -e line 1.

$ perl -e 'my %h = %{undef()}'
[no output]

$ perl -we 'use strict; my %h = %{undef()}'
Can't use an undefined value as a HASH reference at -e line 1.
^^^ ^^^^
Because it does not indirectly point to an intrinsic type, variable, when its
cast as a reference (by dereferencing it).
my $p;
$$p;
 
U

Uri Guttman

TM> In article <[email protected]>,
TZ> It's worth noting here that if the returned value is undef, the program
TZ> will die here. This is why I always use an intermediate variable and
TZ> test if for definedness, even if it takes a few more lines.
TM> -w and "use strict" show different things. Note that the first test
TM> shows that %{undef} doesn't mean what you and I thought it meant.

TM> $ perl -we '%h = %{undef}'
TM> Ambiguous use of %{undef} resolved to %undef at -e line 1.
TM> Name "main::h" used only once: possible typo at -e line 1.
TM> Name "main::undef" used only once: possible typo at -e line 1.

hmm. yep.

TM> $ perl -we 'my %h = %{undef()}'

i tried it with +undef for the same result

TM> Use of uninitialized value at -e line 1.

and that is a warning, not die.

TM> $ perl -e 'my %h = %{undef()}'
TM> [no output]

TM> $ perl -we 'use strict; my %h = %{undef()}'
TM> Can't use an undefined value as a HASH reference at -e line 1.

that is a runtime die under strict.

uri
 
G

Gunnar Hjalmarsson

Ted said:
JTT> my %hash = %{ functionReturningReference() };


GH> my %hash = %{ functionReturningReference() };

It's worth noting here that if the returned value is undef, the program
will die here.

If a function, that is supposed to return a hashref, returns undef, it
indicates some sort of failure, so die()ing may be a good thing.
This is why I always use an intermediate variable and
test if for definedness, even if it takes a few more lines.

That sounds wise if undef is a normal return value.
 
J

Jürgen Exner

Ted Zlatanov said:
GH> my %hash = %{ functionReturningReference() };

It's worth noting here that if the returned value is undef, the program
will die here. This is why I always use an intermediate variable and
test if for definedness, even if it takes a few more lines.

If undef is a possible return value according to the function's
specificiation, then that is certainly a very good idea.
If not then something must have gone terribly wrong and die()ing in the
face of an illegal return value is maybe not such a bad thing.

jue
 
B

Brad Baxter

Is there a way do this with one assignment so that the $hash_ref
variable is not necessary?

my $hash_ref = functionReturningReference();
my %hash = %$hash_ref;

my %hash = map defined && %$_, func();

__
Brad
 
T

Ted Zlatanov

JE> If undef is a possible return value according to the function's
JE> specificiation, then that is certainly a very good idea.
JE> If not then something must have gone terribly wrong and die()ing in the
JE> face of an illegal return value is maybe not such a bad thing.


GH> If a function, that is supposed to return a hashref, returns undef, it
GH> indicates some sort of failure, so die()ing may be a good thing.
....
GH> That sounds wise if undef is a normal return value.

[note this is under `use strict', see below]

I always try to trap preventable fatal errors. It's almost always (with
very few exceptions) better for the users, no matter what the programmer
thinks. At least the error message should be better than the one Perl
produces in this case ("Can't use an undefined value as a HASH
reference").


Actually %{undef} looks for the hash named 'undef' so that's only
accidentally the right example (as `use strict' will tell you).
%{undef()} is it.

Sorry I forgot to mention `use strict'. I simply don't program without
it, and tend to forget many people do.

Ted
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,212
Messages
2,571,104
Members
47,698
Latest member
TerraT521

Latest Threads

Top