Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Please help me with the output

by Thakur (Acolyte)
on Jan 14, 2012 at 06:02 UTC ( #947860=perlquestion: print w/ replies, xml ) Need Help??
Thakur has asked for the wisdom of the Perl Monks concerning the following question:

Please help me with the output of the code below. Also please explain the answer

use strict; use warnings; use diagnostics; $letter = 'b'; addtwo(); addtwo(); print $letter; sub addtwo { $letter +=2; }

Comment on Please help me with the output
Download Code
Re: Please help me with the output
by quester (Vicar) on Jan 14, 2012 at 06:39 UTC

    When run as-is, it says

    $ perl 947860.pl Global symbol "$letter" requires explicit package name at 947860.pl li +ne 5. Global symbol "$letter" requires explicit package name at 947860.pl li +ne 8. Global symbol "$letter" requires explicit package name at 947860.pl li +ne 10. Execution of 947860.pl aborted due to compilation errors (#1) (F) You've said "use strict" or "use strict vars", which indicates that all variables must either be lexically scoped (using "my" or +"state"), declared beforehand using "our", or explicitly qualified to say which package the global variable is in (using "::"). BEGIN not safe after errors--compilation aborted at /usr/lib/perl5/5.1 +0/Carp/Heavy.pm line 11. Compilation failed in require at /usr/lib/perl5/5.10/Carp.pm line 33.

    The errors relating to $letter and the diagnostic can be fixed with a "my $letter;" (our and state are more specialized, my is the most common way of declaring variables.) Generally it is best to fix the first few obvious errors before worrying about less-obvious ones. That's because Perl, like most compilers, can get confused enough by errors early in the program that it can't handle otherwise-valid code later in the program. So, fixing just the declaration of $letter,

    use strict; use warnings; use diagnostics; my $letter = 'b'; addtwo(); addtwo(); print $letter; sub addtwo { $letter +=2; }

    says

    Argument "b" isn't numeric in addition (+) at 947860a.pl line 10 (#1) (W numeric) The indicated string was fed as an argument to an oper +ator that expected a numeric value instead. If you're fortunate the me +ssage will identify which operator was so unfortunate. 4

    Now, what is two more than "b"? Well... it depends. The interpretation that Perl took is to convert "b" to zero. It might be that the "b" should have been a number, but my *guess* is that addtwo was meant to increment the letter twice, to get "d" after the first call and "f" after the second. There is a way of doing that: "$letter++" will produce the next letter, because of some magic that applies only to the autoincrement "++" operator. That magic doesn't apply to assignment operators such as "+=". The details are in perlop.

    Fixing that, and fixing the spacing to make the function stand out better (the perltidy command from the Perl::Tidy module in CPAN can do it automatically, although it's overkill for these few lines) we get

    use strict; use warnings; use diagnostics; my $letter = 'b'; addtwo(); addtwo(); print $letter; sub addtwo { $letter++; $letter++; }
    which prints
    f
Re: Please help me with the output
by Anonymous Monk on Jan 14, 2012 at 12:53 UTC
    Since you're doing a masters degree in CS, I'd have expected you to be able to do some research and find out what this means on your own. No offence.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://947860]
Approved by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2014-07-26 13:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (177 votes), past polls