Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   correct syntax if hash does not exist (http://www.velocityreviews.com/forums/t915181-correct-syntax-if-hash-does-not-exist.html)

John 08-15-2011 12:59 PM

correct syntax if hash does not exist
 
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






Rainer Weikusat 08-15-2011 01:08 PM

Re: correct syntax if hash does not exist
 
"John" <john1949@yahoo.com> writes:
> 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.

Rainer Weikusat 08-15-2011 05:27 PM

Re: correct syntax if hash does not exist
 
Rainer Weikusat <rweikusat@mssgmbh.com> writes:
> "John" <john1949@yahoo.com> writes:
>> 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.

John 08-16-2011 06:07 AM

Re: correct syntax if hash does not exist
 

"Rainer Weikusat" <rweikusat@mssgmbh.com> wrote in message
news:87pqk69609.fsf@sapphire.mobileactivedefense.c om...
> "John" <john1949@yahoo.com> writes:
>> 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.


Many thanks.

Cannot find any reference to //.

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

Regards
John



Rainer Weikusat 08-16-2011 11:04 AM

Re: correct syntax if hash does not exist
 
Rainer Weikusat <rweikusat@mssgmbh.com> writes:
> "John" <john1949@yahoo.com> writes:
>> 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'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} || '';



Rainer Weikusat 08-16-2011 11:07 AM

Re: correct syntax if hash does not exist
 
Rainer Weikusat <rweikusat@mssgmbh.com> writes:
> Rainer Weikusat <rweikusat@mssgmbh.com> writes:


[...]

> 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.

Jim Gibson 08-16-2011 05:52 PM

Re: correct syntax if hash does not exist
 
In article <j2d1e6$4vp$1@news.albasani.net>, John <john1949@yahoo.com>
wrote:

> "Rainer Weikusat" <rweikusat@mssgmbh.com> wrote in message
> news:87pqk69609.fsf@sapphire.mobileactivedefense.c om...


> >
> > Also, for a sufficiently recent version of Perl,
> >
> > my $error = $hash->{error} // '';
> >

>
> 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;

--
Jim Gibson


All times are GMT. The time now is 10:29 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.