The last part of your post might misled people to think that our is a must for removing the syntax error. So just a little bit more detail...

Remove the package qualifier, but keep local, it results syntax error:

use strict; use warnings; package Foo; local $bar; sub init { $bar = 42; } 1;
Global symbol "$bar" requires explicit package name at Foo.pm line 6. Global symbol "$bar" requires explicit package name at Foo.pm line 9. Global symbol "$bar" requires explicit package name at Foo.pm line 10. Foo.pm had compilation errors.

But use either my or our removes the error:

use strict; use warnings; package Foo; my $bar; sub init { $bar = 42; print $bar; } 1;

But the use of our or my does make difference in other sense. The following script works when $bar is defined with our:

use strict; use warnings; use Foo; Foo::init(); print $Foo::bar;

But does not work if $bar is defined with my:

Name "Foo::bar" used only once: possible typo at math1.pl line 7. Use of uninitialized value in print at math1.pl line 7.

my or our? depends on what you want to do - in what scope you want your variables to be seen.


In reply to Re^2: do, local and a qualified identifier? by pg
in thread do, local and a qualified identifier? by tomazos

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.