Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > correct syntax if hash does not exist

Reply
Thread Tools

correct syntax if hash does not exist

 
 
John
Guest
Posts: n/a
 
      08-15-2011
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





 
Reply With Quote
 
 
 
 
Rainer Weikusat
Guest
Posts: n/a
 
      08-15-2011
"John" <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
Rainer Weikusat
Guest
Posts: n/a
 
      08-15-2011
Rainer Weikusat <(E-Mail Removed)> writes:
> "John" <(E-Mail Removed)> 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.
 
Reply With Quote
 
John
Guest
Posts: n/a
 
      08-16-2011

"Rainer Weikusat" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> "John" <(E-Mail Removed)> 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


 
Reply With Quote
 
Rainer Weikusat
Guest
Posts: n/a
 
      08-16-2011
Rainer Weikusat <(E-Mail Removed)> writes:
> "John" <(E-Mail Removed)> 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} || '';


 
Reply With Quote
 
Rainer Weikusat
Guest
Posts: n/a
 
      08-16-2011
Rainer Weikusat <(E-Mail Removed)> writes:
> Rainer Weikusat <(E-Mail Removed)> 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.
 
Reply With Quote
 
Jim Gibson
Guest
Posts: n/a
 
      08-16-2011
In article <j2d1e6$4vp$(E-Mail Removed)>, John <(E-Mail Removed)>
wrote:

> "Rainer Weikusat" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
Is it possible to correct word(s) via Perl?Does this kind of modules exist? mike Perl Misc 4 06-06-2007 12:56 AM
Complex Bit Index Syntax, does this exist? Alex VHDL 2 10-19-2006 10:52 AM
Does my object exist? So why its HWND doesn't exist? That's a question... (CMonthCalCtrl control) LT C++ 7 07-25-2004 07:08 PM



Advertisments