Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^2: do, local and a qualified identifier?

by pg (Canon)
on Oct 09, 2005 at 03:22 UTC ( [id://498500]=note: print w/replies, xml ) Need Help??


in reply to Re: do, local and a qualified identifier?
in thread do, local and a qualified identifier?

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.

Replies are listed 'Best First'.
Re^3: do, local and a qualified identifier?
by ikegami (Patriarch) on Oct 09, 2005 at 16:36 UTC
    yeah, but using my would render $bar inaccessible from the main program. Since the OP was asking for the value of $Foo::bar as seen from the main program, using my probably won't work. Thanks for the clarification, though.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://498500]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-03-28 16:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found