SPUG: Question about variables in packages

Reed, Keith keith.reed at philips.com
Fri Oct 24 07:37:17 PDT 2008


I believe the reprimand is solely because you didn't put 'my' before your variable declarations.

The following seems to work fine...

>> use strict;
>> use warnings;
>> package Stuff;
>>
>> my $var1 = 10;
>> my $var2 = 3;
>> my $var3;
>>
>>
>>
>> sub doThis()
>> {
>>     print "something\n";
>>     $var3 = $var1 + $var2;
>>     print $var3;
>> }
>>
>> doThis();


Regards,

Keith Reed
Software Engineer
Philips Healthcare
Bothell, WA
(425) 482-8338

-----Original Message-----
From: spug-list-bounces+keith.reed=philips.com at pm.org [mailto:spug-list-bounces+keith.reed=philips.com at pm.org] On Behalf Of Christopher Howard
Sent: 2008 Oct 24 12:33 AM
To: Seattle Perl Users Group
Subject: SPUG: Question about variables in packages

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;
$var3;

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
http://www.indicium.us

_____________________________________________________________
Seattle Perl Users Group Mailing List
     POST TO: spug-list at pm.org
SUBSCRIPTION: http://mail.pm.org/mailman/listinfo/spug-list
    MEETINGS: 3rd Tuesdays
    WEB PAGE: http://seattleperl.org/

The information contained in this message may be confidential and legally protected under applicable law. The message is intended solely for the addressee(s). If you are not the intended recipient, you are hereby notified that any use, forwarding, dissemination, or reproduction of this message is strictly prohibited and may be unlawful. If you are not the intended recipient, please contact the sender by return e-mail and destroy all copies of the original message.


More information about the spug-list mailing list