Barry Brevik <> wrote:
> I was under the impession that when I use the keyword "our" when
> declaring a variable, that the variable was then available to
> subroutines called from the routine that declared the variable.  

No, it can only be referred to without package qualification within the
lexical scope in which it was declared, but see below.

> 
> However, the code shown below fails with the following message:
> 
> Variable "$clr" is not imported at test5.pl line 17.
> Global symbol "$clr" requires explicit package name at test5.pl line
> 17. 
> 
> What gives?
> 
> -Barry Brevik
> 
> ===================================
> use strict;
> use warnings;
> 
> my $color = 'blue';
> 
> setColor($color);
> 
> 
> sub setColor
> {
>   our $clr = $_[0];
>   adjustColor();
> }
> 
> sub adjustColor
> {
>   print "$clr\n";
> }

You have declared $clr as a package variable. It can be referred to
within the declared scope without being qualified by the package name,
and outside that scope with package name qualification. In this case the
package is main, so this should work:

  print "$main::clr\n";

However, I am not sure that this example is a good use of 'our', as you
are effectively declaring a global variable, but hiding its declaration
within a subroutine. I would prefer to see the two subs that refer to
the $clr being declared within a distinct package, and $clr declared at
package scope.

But then again, its not entirely clear from your simple example what you
are trying to achieve. It could even be an X/Y problem.

HTH

-- 
Brian Raven 

Please consider the environment before printing this email.

This e-mail may contain confidential and/or privileged information. If you are 
not the intended recipient or have received this e-mail in error, please advise 
the sender immediately by reply e-mail and delete this message and any 
attachments without retaining a copy.

Any unauthorised copying, disclosure or distribution of the material in this 
e-mail is strictly forbidden.

_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to