manishrathi has asked for the wisdom of the Perl Monks concerning the following question:

#!/usr/bin/perl $var = "foo"; print $var, "\n"; modify_var(); print $var, "\n"; sub modify_var { $var = "bar"; } print $var; ___OUTPUT___ foo bar bar
In this code $var is declared in program. What package does it belong to ? What is main package ? If I declare $var in a program, does it automatically belong to main package ? If I declare variable $anything , which does not exist in main package. Now , if I use strict, is it required to use main before the $anything ($main::anything) ? How does $anything become a part of main package ? Or does main mean main body (and not subroutines) of program that we are dealing with ? So does it mean that everything from "#!/usr/bin/perl" to the "print $var; " excluding subroutines is main ?

Replies are listed 'Best First'.
Re: main package
by moritz (Cardinal) on Oct 06, 2009 at 15:38 UTC
    Perl is a language where you can just try out things. You are encouraged to do so.
    What package does it belong to?

    The package it is being declared in. You can find that out with

    print __PACKAGE__, "\n";
    Now , if I use strict, is it required to use main before the $anything ($main::anything) ?

    Try it out!

    Or does main mean main body (and not subroutines) of program that we are dealing with ?

    No. Packages can be only changed with the package declaration. Declaring a subroutine doesn't change the package at all.

    Perl 6 - links to (nearly) everything that is Perl 6.
Re: main package
by ikegami (Patriarch) on Oct 06, 2009 at 16:26 UTC

    What package does it belong to ?

    If you declared it as a package variable, whichever package statement is in effect. In your code, that would be "main".

    If you declared it as a lexical variable, it's not found in a package.

    What is main package ?

    The currently selected package when the interpreter starts.

    If I declare $var in a program, does it automatically belong to main package ?

    No, the package in effect.

    if I use strict, is it required to use main before the $anything ($main::anything) ?

    If you declared it as a package variable, no.

    If you declared it as a lexical variable, yes. Otherwise, you'll be accessing the lexical variable.

    How does $anything become a part of main package ?

    I don't understand.

    Or does main mean main body (and not subroutines) of program that we are dealing with ?

    It's just a namespace into which variables and subs can be placed. It doesn't mean anything.

    So does it mean that everything from "#!/usr/bin/perl" to the "print $var; " excluding subroutines is main ?

    Absent an overriding package statement, all code (including subroutines) will be compiled into main. That means that package variables and subs will be placed in the main namespace by default.

      "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 ?

        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

        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

Re: main package
by Anonymous Monk on Oct 06, 2009 at 15:41 UTC
    In this code $var is declared in program.

    Oops, it actually wasn't declared in the code you posted.

    use strict; use warnings; and then declare your variable with my $var;. Assignment using the = operator is not variable declaration.

    main is the default package name, you can change it by saying package foo;, useful in modules so that you can declare variables without stomping on other people's variables.