Ask a Question related to PERL Beginners, Design and Development.
-
Joshua A. Gage #1
Local Scope Variables
Hi,
I'm having a bit of trouble with using some local scope variables. I
have a function where I want to define a variable, then use that
variable in a sub function. That sounds like the text book definition
for when to use a local scope variable. Below is the test script that
I'm using.
I'm using the 'use strict' declaration as all the books I've read
(O'Reilly's) say that's a Good Thing to do.
Can someone offer some insight as to why when I declare $a it's ok, but
declaring $aa give me an error?
I'm using Active State's Perl v5.8.3 build 809 for Windows.
Thanks for any help
- Josh
[email]jgage@kyocera-wireless.com[/email]
--------------------------------------------------------------------------------------------
use strict;
test1();
sub test1
{
local $a = 1;
local $aa = 2;
test2();
}
sub test2
{
print "a = $a\n";
print "aa = $aa\n";
}
# Global symbol "$aa" requires explicit package name at
C:\projects\Tracing\test.pl line 8.
# Global symbol "$aa" requires explicit package name at
C:\projects\Tracing\test.pl line 15.
# Execution of C:\projects\Tracing\test.pl aborted due to compilation
errors.
Joshua A. Gage Guest
-
Application Scope variables ?
I am migrating to PHP from ASP where there are the Application Scope variables which are accessible from any page on a website and which are used, in... -
#39833 [NEW]: Session variables overwritten by local variables (register_globals=off)
From: sup1382 at accedo dot es Operating system: OpenBSD 3.9 PHP version: 5.2.0 PHP Bug Type: Session related Bug... -
Global Variables and scope
Ok, I'm creating a fairly large RIA using many components set on a page. They all share a common list. It is a Project list that is displayed in a... -
Local Variable Scope in Ruby2
Hi, I was going through matz's slides from the Ruby Conference 2003, and was checking out what the slides claimed was the most regrettable behaviour... -
Enumerate Local Variable Scope
Does anyone know of a way to enumerate the local variable scope? The reason I ask is that I have a function that dumps the contents of... -
Eternius #2
Re: Local Scope Variables
Joshua A. Gage wrote:
> Hi,
>
> I'm having a bit of trouble with using some local scope variables. I
> have a function where I want to define a variable, then use that
> variable in a sub function. That sounds like the text book definition
> for when to use a local scope variable. Below is the test script that
> I'm using.
>
> I'm using the 'use strict' declaration as all the books I've read
> (O'Reilly's) say that's a Good Thing to do.
>
> Can someone offer some insight as to why when I declare $a it's ok, but
> declaring $aa give me an error?
>
> I'm using Active State's Perl v5.8.3 build 809 for Windows.
>
> Thanks for any help
> - Josh
> [email]jgage@kyocera-wireless.com[/email]
>
> --------------------------------------------------------------------------------------------
>
> use strict;
>
> test1();
>
> sub test1
> {
> local $a = 1;
> local $aa = 2;
> test2();
> }
>
> sub test2
> {
> print "a = $a\n";
> print "aa = $aa\n";
> }
>
> # Global symbol "$aa" requires explicit package name at
> C:\projects\Tracing\test.pl line 8.
> # Global symbol "$aa" requires explicit package name at
> C:\projects\Tracing\test.pl line 15.
> # Execution of C:\projects\Tracing\test.pl aborted due to compilation
> errors.
hello,
use my
extract from perldoc -f my :
A "my" declares the listed variables to be local
(lexically) to the enclosing block, file, or
"eval".
cheers
Eternius Guest
-
Randy W. Sims #3
Re: Local Scope Variables
On 02/12/04 18:19, Joshua A. Gage wrote:
'local' does not create a variable; the variable must already exist. In> Hi,
>
> I'm having a bit of trouble with using some local scope variables. I
> have a function where I want to define a variable, then use that
> variable in a sub function. That sounds like the text book definition
> for when to use a local scope variable. Below is the test script that
> I'm using.
>
> I'm using the 'use strict' declaration as all the books I've read
> (O'Reilly's) say that's a Good Thing to do.
>
> Can someone offer some insight as to why when I declare $a it's ok, but
> declaring $aa give me an error?
>
> I'm using Active State's Perl v5.8.3 build 809 for Windows.
>
> Thanks for any help
> - Josh
> [email]jgage@kyocera-wireless.com[/email]
>
> --------------------------------------------------------------------------------------------
>
> use strict;
>
> test1();
>
> sub test1
> {
> local $a = 1;
> local $aa = 2;
> test2();
> }
>
> sub test2
> {
> print "a = $a\n";
> print "aa = $aa\n";
> }
>
> # Global symbol "$aa" requires explicit package name at
> C:\projects\Tracing\test.pl line 8.
> # Global symbol "$aa" requires explicit package name at
> C:\projects\Tracing\test.pl line 15.
> # Execution of C:\projects\Tracing\test.pl aborted due to compilation
> errors.
>
order to create a package variable you must use 'our' or 'use vars':
our $a;
our $aa;
-or-
our ($a, $aa);
-or-
use vars qw($a $aa);
The above are all equivelant, and they all create to package variables
named $a and $aa;
The reason you get no error for $a is because there is already a package
variable with that name: the variables $a and $b exist automatically in
every package for use in the 'sort' function.
Regards,
Randy.
Randy W. Sims Guest
-
Wiggins D'Anconia #4
Re: Local Scope Variables
Eternius wrote:
In most cases you would be right, but in this case it appears that the> Joshua A. Gage wrote:
>>>> Hi,
>>
>> I'm having a bit of trouble with using some local scope variables. I
>> have a function where I want to define a variable, then use that
>> variable in a sub function. That sounds like the text book definition
>> for when to use a local scope variable. Below is the test script that
>> I'm using.
>>
>> I'm using the 'use strict' declaration as all the books I've read
>> (O'Reilly's) say that's a Good Thing to do.
>>
>> Can someone offer some insight as to why when I declare $a it's ok,
>> but declaring $aa give me an error?
>>
>> I'm using Active State's Perl v5.8.3 build 809 for Windows.
>>
>> Thanks for any help
>> - Josh
>> [email]jgage@kyocera-wireless.com[/email]
>>
>> --------------------------------------------------------------------------------------------
>>
>> use strict;
>>
>> test1();
>>
>> sub test1
>> {
>> local $a = 1;
>> local $aa = 2;
>> test2();
>> }
>>
>> sub test2
>> {
>> print "a = $a\n";
>> print "aa = $aa\n";
>> }
>>
>> # Global symbol "$aa" requires explicit package name at
>> C:\projects\Tracing\test.pl line 8.
>> # Global symbol "$aa" requires explicit package name at
>> C:\projects\Tracing\test.pl line 15.
>> # Execution of C:\projects\Tracing\test.pl aborted due to compilation
>> errors.
>
>
> hello,
>
> use my
> extract from perldoc -f my :
> A "my" declares the listed variables to be local
> (lexically) to the enclosing block, file, or
> "eval".
>
OP understands what 'local' does, as he is using it inside of an
embedded function. Do I like that, no, but it is a "correct" usage of
'local'. Your suggestion of using 'my' changes how the variables must
be used so is not equivalent without suggesting the interface to 'test2'
changes...
sub test1 {
my $aa = 1;
my $aaa = 2;
test2($aa, $aaa);
}
sub test2 {
my ($bb, $bbb) = @_;
print "aa = $bb\n";
print "aaa = $bbb\n";
}
Is the above better, in the court of readability and encapsulation, yes,
in the court of elegance, maybe not...
[url]http://danconia.org[/url]
Wiggins D'Anconia Guest
-
Jan Eden #5
Re: Local Scope Variables
Wiggins d'Anconia wrote:
Elegance in programming should be defined in terms of readability, at leastpartly. Very dense ways of coding a certain goal are smart (and I admit that I like to compress my code as much as possible), but not necessarily very elegant. ;)>sub test1 {
> my $aa = 1;
> my $aaa = 2;
> test2($aa, $aaa);
>}
>
>sub test2 {
> my ($bb, $bbb) = @_;
> print "aa = $bb\n";
> print "aaa = $bbb\n";
>}
>
>Is the above better, in the court of readability and encapsulation,
>yes, in the court of elegance, maybe not...
- Jan
--
There's no place like ~/
Jan Eden Guest



Reply With Quote

