Hi all. Been reading for a while, never posted before.

I'm trying to create a variable that exists in package space that is persistent without an instantiation of any objects. I'd like to accomplish something like a public static variable.

Here's a simplest case script:

foo.pl

#!/usr/bin/perl use strict; use lib '.'; use Bar; ### Before and after snapshots. print 'var before setting: (' . $Bar::package_variable . ")\n"; $Bar::package_variable = 'Ogopogo'; print 'var after setting: (' . $Bar::package_variable . ")\n"; ### From an object. my $obj = new Bar( ); print 'var from obj--unqualified: (' . $obj->{'unqualified'} . ")\n"; print 'var from obj--qualified: (' . $obj->{'qualified'} . ")\n"; print "\nAddresses:\n"; print '$Bar::package_variable '.\$Bar::package_variable."\n"; print '$obj->{"unqualified_ref"} '.\${$obj->{'unqualified_ref'}}."\n"; print '$obj->{"qualified_ref"} '.\${$obj->{'qualified_ref'}}."\n"; exit;

And the module it's using:

Bar.pm

package Bar; our $package_var; sub new { my $class = shift; my $self = {}; bless $self, $class; $self->{'unqualified'} = $package_var; $self->{'qualified'} = $Bar::package_var; $self->{'unqualified_ref'} = \$package_var; $self->{'qualified_ref'} = \$Bar::package_var; return $self; } 1;

And the output of running the script:
Output of $>./foo.pl

Package var before setting: () Package var after setting: (Ogopogo) Package var from obj--unqualified: () Package var from obj--qualified: () Addresses: $Bar::package_variable SCALAR(0x815363c) $obj->{"unqualified_ref"} SCALAR(0x8188950) $obj->{"qualified_ref"} SCALAR(0x8188950)
Why doesn't object instantiation work as I'm expecting? The script seems to be able to interact with the package variable, but the module doesn't? From the "Addresses" section, it appears that they're looking in two different places, but I'm not sure why -- especially since the qualified and unqualified versions are looking in the same place, from within the package.

Help!

Thanks =)


In reply to Accessibility of package-level variables by sirrobert

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.