Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

When run as-is, it says

$ perl Global symbol "$letter" requires explicit package name at li +ne 5. Global symbol "$letter" requires explicit package name at li +ne 8. Global symbol "$letter" requires explicit package name at li +ne 10. Execution of 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/ line 11. Compilation failed in require at /usr/lib/perl5/5.10/ 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; }


Argument "b" isn't numeric in addition (+) at 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

In reply to Re: Please help me with the output by quester
in thread Please help me with the output by Thakur

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (2)
    As of 2018-03-17 06:54 GMT
    Find Nodes?
      Voting Booth?
      When I think of a mole I think of:

      Results (223 votes). Check out past polls.