correct syntax if hash does not exist

J

John

Hi

Part of code.

If hash does not exist make it null.

Is this the correct syntax?

my %data; my $hash=\%data;
........
my $error=$hash->{error} or '';

Regards
John
 
R

Rainer Weikusat

John said:
Part of code.

If hash does not exist make it null.

Is this the correct syntax?

my %data; my $hash=\%data;
.......
my $error=$hash->{error} or '';

It is correct, but more than a little weird: There is no reason to
declare a hash and assign a reference to that to a scalar variable to
get a hash reference. Provided that a hash reference is what it
actually desired,

my $hash = {};

or just using $hash as hash references in an lvalue context would be
sufficient, eg

my $hash;
$hash->{key} = 'value';

Also, for a sufficiently recent version of Perl,

my $error = $hash->{error} // '';

might be a better choice because it only uses the empty string in
place of the value stored in the hash when this value happened to be
undefined, as opposed to 'something evaluating to false', ie, an empty
string or a numerical zero.

Lastly, this smells a lot like trying to obfuscate one's way around
the "It is undef! Panic in the streets!!" warning somebody has chosen
to code into Perl for some weird reason. A good solution for the
'computer prints nonsense when doing X' problem is 'not doing X', in
this case, not running with warnings enabled.
 
R

Rainer Weikusat

Rainer Weikusat said:
John said:
Part of code.
[...]
my $error=$hash->{error} or '';
[...]

Lastly, this smells a lot like trying to obfuscate one's way around
the "It is undef! Panic in the streets!!" warning somebody has chosen
to code into Perl for some weird reason.

As opposed to what I assumed, the assignment dosn't trigger this
warning.
 
J

John

Rainer Weikusat said:
It is correct, but more than a little weird: There is no reason to
declare a hash and assign a reference to that to a scalar variable to
get a hash reference. Provided that a hash reference is what it
actually desired,

my $hash = {};

or just using $hash as hash references in an lvalue context would be
sufficient, eg

my $hash;
$hash->{key} = 'value';

Also, for a sufficiently recent version of Perl,

my $error = $hash->{error} // '';

might be a better choice because it only uses the empty string in
place of the value stored in the hash when this value happened to be
undefined, as opposed to 'something evaluating to false', ie, an empty
string or a numerical zero.

Lastly, this smells a lot like trying to obfuscate one's way around
the "It is undef! Panic in the streets!!" warning somebody has chosen
to code into Perl for some weird reason. A good solution for the
'computer prints nonsense when doing X' problem is 'not doing X', in
this case, not running with warnings enabled.

Many thanks.

Cannot find any reference to //.

I might as well stick to if (not defined ..

Regards
John
 
R

Rainer Weikusat

Rainer Weikusat said:
It is correct, but more than a little weird:

There's also a possible precedence problem here: The precedence of
'or' is lower than that of =, meaning,

my $error = $hash->{error} or '';

equivalent to

(my $error = $hash->{error}) or '';

and Perl will quite appropriately warn[*] about that:

[rw@sapphire]~ $perl -cwe 'my $hash; my $error = $hash->{error} or "";'
Useless use of a constant in void context at -e line 1.
-e syntax OK

What was likely intended would look like this:

my $error = $hash->{error} || '';
 
R

Rainer Weikusat

Rainer Weikusat said:
[...]

and Perl will quite appropriately warn[*] about that:

Forgotten footnote: Not using perl compile-time warnings is IMHO very
unwise because (as opposed to, say, gcc warnings) they usually point
at actual problems.
 
J

Jim Gibson

John <[email protected]> said:
Cannot find any reference to //.

I might as well stick to if (not defined ..

It is new in Perl 5.10. See 'perldoc perlop' and search for "C-style
Logical Defined-Or".

The statement

$x //= $y;

is equivalent to:

$x = defined($x) ? $x : $y;
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top