in reply to Re: main package
in thread main package

"If you declared it as a package variable"

So if I declare a variable without "my" , is that a package variable ? If I declare a variable in the sub routine without "my" , is it still a package variable ?

"whichever package statement is in effect"

How to know which opackage statement is in effect ? From the comment , it appears that default is main package. If I want to use same variable name in another program, how can I do that ?

Replies are listed 'Best First'.
Re^3: main package
by ELISHEVA (Prior) on Oct 06, 2009 at 19:13 UTC

    You should always declare variables with either our or my.

    "whichever package statement is in effect".Here's how you set the package.

    use strict; use warnings; # the default namespace is main # so this is a variable in main. our $movie='Chitty Chitty Bang Bang'; # set the current package package A::B::C; # We've changed the package - it is no longer main # now its A::B::C # if a variable is undefined in the current package, # then Perl looks for a variable in main of the same name # these two statements are the same and do not cause # warnings print "movie=$movie\n"; print "movie=$main::movie\n"; # but once we declare the variable in A::B::C, an # unqualified name is presumed to be in the current # package # declare a package variable in the namespace A::B::C our $movie = 'Mary Poppins'; # these print different things print "movie=$movie\n"; # prints Marry Poppins print "movie=$main::movie\n"; # prints Chitty Chitty Bang Bang { # this overrides the current package. The override # will last until the next matching curly brace package NotA::NotB; # this is $movie but in the NotA::NotB namespace # its a different variable from either $main::movie # or $A::B::C::movie my $movie = 'Pippi Longstocking'; print "movie=$movie\n"; #prints Pippi Longstocking print "movie=$A::B::C::movie\n"; #prints Mary Poppins print "movie=$main::movie\n"; #prints Chitty Chitty Bang Bang # } matches { enclosing package statement # so this ends the code block where NotA::NotB is the # package. } # now we are back to A::B::C being the current package print "movie=$movie\n"; # prints Marry Poppins print "movie=$main::movie\n"; # prints Chitty Chitty Bang Bang

    Hope that helps.

    Best, beth

Re^3: main package
by ikegami (Patriarch) on Oct 06, 2009 at 19:55 UTC

    So if I declare a variable without "my" , is that a package variable ?

    There are three ways to declare a variable.

    my $var; declares a lexical var.
    our $var; declares a package var. The declaration is lexical.
    use vars qw( $var ); declares a package var. The declaration is global.

    If I declare a variable in the sub routine without "my" , is it still a package variable ?

    It's irrelevant whether a sub is being compiled or not.

    How to know which package statement is in effect ?

    Look for a package statement in the source, or call __PACKAGE__.

    If I want to use same variable name in another program, how can I do that ?

    The question makes no sense. Please clarify