Variable scoping
am 20.02.2010 00:29:08 von Barry Brevik
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.
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";
}
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: Variable scoping
am 20.02.2010 00:35:33 von Michael Ellery
On 2/19/2010 3:29 PM, 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.
>
> 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";
> }
>
>
http://perldoc.perl.org/functions/our.html --
"In other words, our has the same scoping rules as my, but does not
necessarily create a variable."
....so, it's more like a static variable from the c world. I think you
might be thinking of 'local'.
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: Variable scoping
am 22.02.2010 12:23:49 von Brian Raven
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
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs