Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

how does perl handle variables

by jabirahmed (Initiate)
on May 17, 2013 at 11:27 UTC ( [id://1033963]=perlquestion: print w/replies, xml ) Need Help??

jabirahmed has asked for the wisdom of the Perl Monks concerning the following question:

@a=qw( 1 2 3 4 a b c g f); $s=0; foreach $i(@a){ $s+= $i; } print $s; $perl a.pl 10
> in this case it should ideally throw some error but it just considers > a,b,c... as 0 ? why does it do that

Replies are listed 'Best First'.
Re: how does perl handle variables
by marto (Cardinal) on May 17, 2013 at 11:41 UTC

    "in this case it should ideally throw some error but it just considers > a,b,c... as 0 ? why does it do that"

    Adding use strict; use warnings; is advisable. See strict, warnings:

    #!/usr/bin/perl use strict; use warnings; my @a=qw( 1 2 3 4 a b c g f); my $s=0; foreach my $i(@a){ $s+= $i; print "i: $i - s: $s\n"; } print $s;

    shows:

    D:\>perl derp.pl i: 1 - s: 1 i: 2 - s: 3 i: 3 - s: 6 i: 4 - s: 10 Argument "a" isn't numeric in addition (+) at derp.pl line 10. i: a - s: 10 Argument "b" isn't numeric in addition (+) at derp.pl line 10. i: b - s: 10 Argument "c" isn't numeric in addition (+) at derp.pl line 10. i: c - s: 10 Argument "g" isn't numeric in addition (+) at derp.pl line 10. i: g - s: 10 Argument "f" isn't numeric in addition (+) at derp.pl line 10. i: f - s: 10 10
Re: how does perl handle variables
by Corion (Patriarch) on May 17, 2013 at 11:43 UTC

    This is not as much a matter of variables, as a matter of the + operator in Perl. It treats both its arguments as numbers, and anything that starts with a letter will be treated as numerically 0 for that operation.

Re: how does perl handle variables
by AnomalousMonk (Archbishop) on May 17, 2013 at 22:17 UTC
    in this case it should ideally throw some error...

    Further to Laurent_R's reply: If jabirahmed really wants to make these missteps into (fatal) errors rather than just having them generate nagging warnings, that can also be done. See perllexwarn. (Note that this control of fatality is lexical!)

    >perl -wMstrict -le "print 4 + 'a'; ;; use warnings FATAL => 'numeric'; print 5 + 'b'; " Argument "a" isn't numeric in addition (+) at -e line 1. 4 Argument "b" isn't numeric in addition (+) at -e line 1. >perl -wMstrict -le "print 4 + 'a'; ;; use warnings FATAL => 'all'; print 5 + 'b'; " Argument "a" isn't numeric in addition (+) at -e line 1. 4 Argument "b" isn't numeric in addition (+) at -e line 1.

    (Many Perlers favor escalating  'all' warnings to fatality. I tend to agree with this practice, especially for modules, both OO and non-OO. However, I must admit that such fatality can get annoying during debugging.)

Re: how does perl handle variables
by Laurent_R (Canon) on May 17, 2013 at 21:03 UTC
    $ perl -e 'print 4 + "a"' 4 $ perl -e 'use strict; print 4 + "a"' 4 $ perl -e 'use warnings; print 4 + "a"' Argument "a" isn't numeric in addition (+) at -e line 1. 4 $ perl -we 'print 4 + "a"' Argument "a" isn't numeric in addition (+) at -e line 1. 4

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1033963]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-24 22:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found