Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Persistent Variable

by Adam (Vicar)
on Jan 15, 2001 at 17:42 UTC ( [id://51998]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to Persistent Variable

First off, if you are already comfortable with OO thinking, then you might want to check out Perl's OO. Its not as solid as Java or C++, but it is there. (perltoot is a good tutorial.)

Ok, on to your question:
You don't need a separate package for these variables. If you declare the variables in your script before declaring any subs then all the subs in main:: can see them. If you have them in a namespace other then main::, then you'll either want to use our (5.6+) or use vars($^V<5.6)

#!perl -w use strict; #Always! my $username=user; my $sessionid=1234; subone($comm); #in subroutines i could call subone { print "$comm->$username"; }

Replies are listed 'Best First'.
Re: Re: Persistent Variable
by Anonymous Monk on Jan 15, 2001 at 17:45 UTC
    It may not have been clear from my original post, but the subs themselves are in different package, that are being called by main, so they variables wouldn't be seen if I just declared them as globals.
      Actually, that's what I meant when I said, "If you have them in a namespace other then main::, then you'll either want to use our (5.6+) or use vars($^V<5.6)." With out playing games with the import() method or Exporter, you can get to package globals by fully specifying the package name. ie:
      use strict; # I can't emphasize that enough! { package Common; use vars qw/ $Var1 @Var2 /; $Var1 = "some value"; @Var2 = ( 1, 2, 3); if( $^V =~ m/^5\.6/ ) { our $Var3 = "The 5.6 way to do the above"; } } # Now in main:: name space print $Common::Var1; print reverse @Common::Var2;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://51998]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.