The Elite Noob has asked for the wisdom of the Perl Monks concerning the following question:

hey there, this is kind of simple question. So I wrote this sample code,
#!/usr/bin/perl -w use strict; package Foo; # this creates a package called Foo my $bar = "Hello World"; package main; # goes back to default package of class print $Foo::bar; # this prints $bar form package Foo
However I cannot get it to work, does anyone know i can access $bar from package/class Foo? All help is appreciated!

Replies are listed 'Best First'.
Re: how to use packages as classes in perl?
by toolic (Bishop) on Apr 04, 2011 at 22:52 UTC
    our
    #!/usr/bin/perl -w use strict; package Foo; # this creates a package called Foo our $bar = "Hello World"; package main; # goes back to default package of class print $Foo::bar; # this prints $bar form package Foo __END__ Hello World
      Just for completeness, our is like my not restricted to the scope of the package.

      So this example can be simplified as long as the packages are not enclosed in a block or in different files.

      #!/usr/bin/perl -w use strict; { package Foo; our $bar = "Hello World"; package Zoo; print $bar; # no "Foo::" necessary } print $Foo::bar; # now again in package main __END__ Hello WorldHello World

      Cheers Rolf

      Thank you! This is just what i needed :)
Re: how to use packages as classes in perl?
by jwkrahn (Abbot) on Apr 04, 2011 at 23:28 UTC

    Any variable you see that has a double colon (Foo::bar) or apostrophe (Foo'bar) in it is a package variable.    Any variable created with my is not a package variable.

    $ perl -le' use warnings; use strict; package Foo; # this creates a package called Foo my $Foo::bar = "Hello World"; ' "my" variable $Foo::bar can't be in a package at -e line 6, near "my $ +Foo::bar " Execution of -e aborted due to compilation errors.
    $ perl -le' use warnings; use strict; package Foo; # this creates a package called Foo $Foo::bar = "Hello World"; package main; # goes back to default package of class print $Foo::bar; # this prints $bar form package Foo ' Hello World
Re: how to use packages as classes in perl?
by luis.roca (Deacon) on Apr 04, 2011 at 22:58 UTC

    toolic covered it but you may also want to read: Lexical scoping like a fox


    "...the adversities born of well-placed thoughts should be considered mercies rather than misfortunes." — Don Quixote
Re: how to use packages as classes in perl?
by JavaFan (Canon) on Apr 05, 2011 at 11:53 UTC
    Note that you are accessing $bar in package Foo. But as pointed out, my $bar creates a lexical variable - one that isn't in any package.
      Ahh,, this explanation makes alot of sense! thank you!
Re: how to use packages as classes in perl?
by shawnhcorey (Friar) on Apr 05, 2011 at 13:42 UTC

    A variable declared with my is scoped until the end of it's block or if it is outside of any block, to the end of the file. Thus, this works:

    package Foo;
    my $bar = 'Hello World';
    
    package main;
    print "$bar\n";
    

    A variable declared with our is global and can be accessed by a fully-qualified name. Example: $Foo::bar.

    See perldoc perlmod for details on fully-qualified names.