Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Why are these undefs different?

by BillKSmith (Monsignor)
on Sep 13, 2023 at 17:58 UTC ( [id://11154428]=note: print w/replies, xml ) Need Help??


in reply to Why are these undefs different?

A little more code handles all the common errors correctly without throwing an exception and without altering the original data.
use strict; use warnings; use List::Util qw(first); use Scalar::Util qw(looks_like_number); use Test::More tests=>5; sub first_a_value { my @x = @_; my $refer = first { exists $_->{aaa} and looks_like_number($_->{aaa}) and $_->{aaa} < 4 } @x; my $value = undef; $value = $refer->{aaa} if defined $refer; return $value; } my @x; my $aaa; @x = ( {aaa=>3} ); $aaa = first_a_value(@x); ok ($aaa == 3, 'valid value'); @x = ( {aaa=>100} ); $aaa = first_a_value(@x); ok (!defined($aaa), 'no numer small enough'); @x = ( {aaa=>'foo'} ); $aaa = first_a_value(@x); ok (!defined($aaa), 'not a number'); @x = ( {bbb=>3} ); $aaa = first_a_value(@x); ok (!defined($aaa), 'no aaa'); @x = ( {} ); $aaa = first_a_value(@x); ok (!defined($aaa), 'no hash');

UPDATD - Added tests accidentally omitted.

Bill

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11154428]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (6)
As of 2026-04-21 16:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.