Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Package variables

by reptile (Monk)
on Apr 25, 2000 at 14:15 UTC ( [id://8925]=perlquestion: print w/replies, xml ) Need Help??

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

I've been searching for the answer to this for hours. I want to be able to access variables declared within another package. I know the $Package::name syntax, but my() variables are private, and local() and non-declared variables die with 'use strict'. Exporter won't export my() variables either. Is this even possible? (it should be).

Thanks.

Replies are listed 'Best First'.
Re: Package variables
by reptile (Monk) on Apr 25, 2000 at 14:48 UTC

    NEVERMIND. I figured it out already. I was going to check the vars pragma manpage *before* I posted this question, but it slipped my mind.

    For the record, in case anyone else wants to know:

    package Foo; use strict; use vars qw/$BAR/; $BAR = "baz"; package main; print $Foo::BAR;

    So, use vars declares 'global' variables (global to the package) that aren't private like my().

      You can also initially declare them in a package:
      use strict; # just to prove my point package hidden; $hidden::foo = "This is the first time I've been used."; package main; print $hidden::foo;
      Close enough. 'use vars' just selectively excludes some variables from the 'use strict'.

      So you can do the same by omitting 'use strict'.

      Of course, the way in which you did it was much better...

Re: Package variables
by cciulla (Friar) on Apr 25, 2000 at 14:22 UTC
    Perhaps you can encapsulate the package's variables as "getter" and "setter" methods (properties?).

    For example:

    sub thisvariable { my $self = shift; if (@_) { $self->{THISVARIABLE} = shift } return $self->{THISVARIABLE}; }


    Check out http://www.perl.com/pub/doc/manual/html/pod/perltoot.html

    Of course, I'm kind of biased towards OOP anyway.
      You could also just bless the variable in your constructor:
      package Foo; use strict; my $Bar = "Baz"; sub new { return bless { BAR => $Bar }; } package main; my $Foo = new Foo(); print $Foo->{'BAR'};
      OO is great when you need it, but it is considerably slower, so I try to reserve creating method calls for situations where that sort of abstraction is needed. A good module will use both. For example, I like being able to:
      use CGI::Pretty qw( param ); if ( defined( param('url') ) { #lets not waste any CPU here, this gets + called even when I'm index.cgi #okay, this isn't the front page, lets see what we've got. my $object = new CGI(); my $url = $object->('url'); # etc....
      of course that could be optimized further by parsing out the url at the same time that we test for it, and setting it to 'index' if it's not there, but you get the idea.
      That's part of the beauty of Perl; there is more than one way to do it.
      I don't think Larry intended us to just pick our favorite way, but to use each of these ways, when the time is right.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-23 22:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found