Hi, guys. I am seeing some Perl weirdness. The following code compiles and runs without complaint, even though 'use strict' is on. I've tried both 5.16 and 5.14 releases. (Also, I realize the code does nothing useful. It's just some learning I'm doing.)

use v5.14; use warnings; use autodie; $a = "main"; print "$a\n"; package Cat; $a = "a cat"; package Dog; $a = "a dog"; print "$a\n"; package Cat; print "$a\n"; print "$::a\n"; package main; $b = 17; print "$b\n";

When I first entered the code and compiled it, I got a bunch of errors -- but not the "requires explicit package name" errors that I was expecting. Rather, Perl complained that the $a variables in Cat and Dog namespaces were shadowing the $a in main. Weird. So I turned off 'use strict', and everything worked just fine. But when I turned 'use strict' back on, everything STILL worked just fine. But wait .. that's not all! With 'use strict' still on, I can make certain changes, like adding blank lines and adding '$b = "a boy";' to the Cat and Dog namespaces. These changes cause no problems. But as soon as I add a variable with a name that hasn't been seen before ... like '$c = "another cat";' ... I get the "requires explicit package name" error. Finally, I was able to make a new file that duplicated this behavior without me ever having to turn off strict to begin with. I'm hoping someone can shed some light on this. For the record: I'm running Win 7 64-bit and using Eclipse with the EPIC plug-in. I have also edited the scripts outside Eclipse and run them from the command line. My 5.16 Perl was the 64-bit version, while my 5.14 Perl is the 32-bit version. That's about all the info I can think to provide.


In reply to Unexpected behavior with 'use strict' by phramus

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.