SPUG: Predeclaring packages

DeRykus, Charles E charles.e.derykus at boeing.com
Fri Jan 2 20:11:22 PST 2009


 
> I did some more experimentation, and I think I know what was confusing
me. 
> Maybe you can confirm this: the scoping for an 'our' statement occurs
during 
> compiling, but actual variable assignments occur during runtime.

> use strict;
> use warnings;

> Hello::sayHi();
> exit(0);

> package Hello;
> {

> our $greeting = 'Hello there';

> sub sayHi { print $greeting . "\n" }

> }


Um, it's not a compilation vs. runtime issue:  "our" just exposes a
value 
for a global variable within a specific scope. Since the <our $greeting
= 
'Hello there'> in package Hello is not in scope when Hello::sayHi() is
called, 
it's undefined. That's why  it only works if you declare
<$Hello::greeting =
'Hello there'>  before the  call to Hello::sayHi. Note though a
deckaration 
such as <"our $greeting =  "Hello there'> just prior to Hello::sayHI
wouldn't 
work because the scope  of 'our' won't extend into package Hello. And
that's 
why  I suggested assigning the global variable to an "our" variable to
avoid 
tediously needing  a package qualifier for $greeting when used in other
scopes. 

> That outputs the 'use of an unitialized variable...' warning. I'm
guessing that is because 
> nothing has been assigned to the package variable $greeting when sayHi
is called, 
> and so 'use warnings' spits out a warning for attempting to use an
variable without first
> initializing it.

Correct.

-- 
Charles DeRykus



-----Original Message-----
From: Christopher Howard [mailto:choward at indicium.us] 
Sent: Friday, January 02, 2009 3:34 PM
To: Yitzchak Scott-Thoennes
Cc: Seattle Perl Users Group
Subject: Re: SPUG: Predeclaring packages

On Thu, 1 Jan 2009, Yitzchak Scott-Thoennes wrote:

> On Wed, December 31, 2008 7:36 pm, Christopher Howard wrote:
>> Question: Is there a way to predeclare packages, without moving the 
>> package to a separate file?
>>
>> This is my issue: When I write a script, I like to use packages to 
>> keep everything nice and neat. And I like to have subs and packages 
>> after the 'initializing' part of the program, so it is easy to find. 
>> So what I'm doing now is something like this:
>>
>> ### EXAMPLE START ###
>
>> This doesn't work because $timer is not predeclared along with the 
>> subroutines.
>
> There's something you're missing, but I'm not completely sure what it 
> is.  Can you explain more what isn't working?
>
> Your example should work just fine, though the predeclarations are 
> unnecessary.  Remember that first your entire script is compiled, 
> including the sub definitions, and then it begins executing.
>
>

Blah!! You're right, I tried it again and it worked! (Figures...)

I did some more experimentation, and I think I know what was confusing
me. 
Maybe you can confirm this: the scoping for an 'our' statement occurs
during compiling, but actual variable assignments occur during runtime.

For example, I tried this:

### EXAMPLE START ###

use strict;
use warnings;

$Hello::greeting = 'Hello there';
Hello::sayHi();
exit(0);

package Hello;
{

our $greeting;

sub sayHi { print $greeting . "\n" }

}

### EXAMPLE END ###

This program outputs 'Hello there'. But if I move the value assignment
inside the package, it doesn't work like I want:

### EXAMPLE START ###

use strict;
use warnings;

Hello::sayHi();
exit(0);

package Hello;
{

our $greeting = 'Hello there';

sub sayHi { print $greeting . "\n" }

}

### EXAMPLE END ###

That outputs the 'use of an unitialized variable...' warning. I'm
guessing that is because nothing has been assigned to the package
variable $greeting when sayHi is called, and so 'use warnings' spits out
a warning for attempting to use an variable without first initializing
it.

Am I getting it right?

--
Christopher Howard
http://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/


More information about the spug-list mailing list