#!/usr/bin/perl -w use strict; our $c = 2; print "Before: c=$c\n"; my $c = 1 unless $c; print "After: c=$c\n";
This highlights the duality of my.

If you had actually run that, you should have got the compiler error:

"my" variable $c masks earlier declaration in same scope at 492281.pl +line 6.
because you had warnings on.

Let's do it line by line:

our $c = 2;
The global variable also known as $main::$c is formally declared (for strictness), and assigned the value 2.
print "Before: c=$c\n";
The value of $main::$c (2) is printed.
my $c = 1 unless $c;
If the current $c variable is true, declare a lexical variable with the same name, and assign it the value 1. On the first (and here only) pass, $c is just shorthand for the global $main::$c, and it's value is 2, so the lexical assignment doesn't happen.

However, the lexical creation did! Until the lexical $c goes out of scope (in this case, at the end of the file), all instances of $c refer to this lexical variable, and not to the global $main::$c. To access $main::$c, the "long" name must be used, as the short name points to something different.

print "After: c=$c\n";
The value of the lexical $c is printed. However, since the assignment didn't happen, it's still undefined. And if you'd actually tried it, you would have seen this:
Use of uninitialized value in concatenation (.) or string at 492281.pl + line 7.
just before the "After..." string was printed.

To separate these issues, try changing my $c to my $d:

#!/usr/bin/perl -w use strict; our $c = 2; print "Before: c=$c\n"; my $d = 1 unless $c; print "After: d=$d\n";
gives
Before: c=2 Use of uninitialized value in concatenation (.) or string at 492281.pl + line 7. After: d=
Do you see why?

-QM
--
Quantum Mechanics: The dreams stuff is made of


In reply to Re^5: Have you netted a Perl Monk or Perl Pretender in 5 minutes or less? by QM
in thread Have you netted a Perl Monk or Perl Pretender in 5 minutes or less? by Anonymous Monk

Title:
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, details, 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, summary, 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.