SPUG: Question about variables in packages

Christopher Howard choward at indicium.us
Fri Oct 24 00:32:59 PDT 2008

Hi.  I want to use packages in the program I'm working on.  Not for 
creating classes, but just to keep my variables and subroutines in neat, 
tidy namespaces.  But what should the variables look like in the packages?

I tried this:

use strict;
use warnings;
package Stuff;

$var1 = 10;
$var2 = 3;

sub doThis() { ... }

But then I get the regular reprimand about using globals.

I wondered if I was suppose to be doing this:

$Stuff::var1 = 10;
$Stuff::var2 = 3;

But that seems like a lot of extra typing.

And I wondered if I was supposed to be making them lexical with

my $var1 = 10;
my $var2 = 3;

If they are lexical, will I be able to use them in other scripts?  And 
while we are on the subject, what are my options for using the variables 
in other scripts?  Is this supposed to work?:

use Stuff;
my $sum = $Stuff::var1 + $Stuff::var2;

Forgive the newbie questions, but the language of "perldocs package" 
left me a bit uncertain about what exactly is going on here.

Christopher Howard
choward at indicium.us

More information about the spug-list mailing list