Tuesday 15 July 2014

What does Perl return when a numeric comparison is false? -



What does Perl return when a numeric comparison is false? -

i trying execute code. puzzles me why doesn't comparing homecoming value when false. default behavior of these comparing operators?

my $output = (2 <= 1); print "value=$output";

will comparing operator homecoming undef when logical check fails?

the relational operators

return 1 true , special version of defined empty string, "" , counts 0 exempt warnings improper numeric conversions, "0 true" is.

the value dualvar. has separate numeric , string values (not special version of empty string, really). numeric value 0 , string value empty string. used string portion, empty, 0 still there. can within variable records devel::peek:

use devel::peek; $result = ( 1 == 2 ); dump( $result );

in sv (scalar value) thing behind scenes, see string value in pv , numeric value in iv , nv (the integer , numeric values):

sv = pvnv(0x7fe602802750) @ 0x7fe603002e70 refcnt = 1 flags = (padmy,iok,nok,pok,piok,pnok,ppok) iv = 0 nv = 0 pv = 0x7fe6026016b0 ""\0 cur = 0 len = 16

there other sorts of dualvars too. $! error variable has error number , error text, instance (and talk in mastering perl). isn't have worry because perl right thing context.

if want numeric value, utilize in numeric context:

my $numeric = 0 + $result; # 0

you can create own dualvars scalar::util's dualvar, , can check if scalar dualvar isdual.

use scalar::util qw(isdual); $result = ( 1 == 2 ); print isdual($result) ? "dualvar" : "not dualvar";

if wanted check value got defined (you said wasn't), can check defined. empty string defined, though. if want check it's not empty string, can utilize length. these help when value have isn't printable.

perl

No comments:

Post a Comment