in reply to Please help me understand string eval better

Your main problem is that perl does not consider '09' to be a number. The function 'eval' will fail no matter what you do. Why not explicitly remove the leading zero?
use strict; use warnings; use Scalar::Util qw(looks_like_number isdual); my $transf = '/3+6'; my $constant = '09'; my $var = \$constant; #if (looks_like_number($$var)) { if (!looks_like_number($$var)) { #correction per [haukex] print "$$var is not a number\n"; } else { print "$$var looks like a number\n"; } if (!isdual($$var)) { print "$$var does not have a hidden numeric value\n"; } $$var =~ s/^0*//; # make it into a valid number my$expr = "$$var$transf"; my $ans = eval "$$var$transf"; print "$expr yields: "; if (!$@) { print $ans, "\n"; } else { print $@, "\n"; }
Bill

Replies are listed 'Best First'.
Re^2: Please help me understand string eval better
by haukex (Archbishop) on Jun 13, 2017 at 15:00 UTC
    perl does not consider '09' to be a number

    Perl does consider the string "09" to be a number, 0 + "09" == 9. The only question is, does the OP want this to be interpreted as an octal, which in this case would be invalid, or not.

    $ perl -wMstrict -le 'print 77' 77 $ perl -wMstrict -le 'print 077 ' 63 $ perl -wMstrict -le 'print 0 + "077"' 77 $ perl -wMstrict -le 'print oct "077"' 63 $ perl -wMstrict -le 'print eval " 77"' 77 $ perl -wMstrict -le 'print eval "077"' 63
    if (looks_like_number($$var)) { print "$$var is not a number\n";

    You've got mistake in your logic there. Also note that looks_like_number tells you whether the string will properly auto-convert to a number (0+$str), but does not cover all of the numeric literals that Perl accepts in its source code, i.e. what eval will parse, such as 0b1001 or 1234_56; strings like "0b1001" won't automagically convert to numbers. I wrote about this a bit more at Re^3: why are hex values not numbers?