Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > static variable problem

Reply
Thread Tools

static variable problem

 
 
Dave Saville
Guest
Posts: n/a
 
      11-13-2012
Please why does this throw an error?

[T:\tmp]cat try.pl
use strict;
use warnings;

foo("Hello World");
exit;
{
my $bar = 1;

sub foo
{
my $a = shift;
print "$bar $a\n"; # <== line 12
return;
}
}

[T:\tmp]try.pl
Use of uninitialized value in concatenation (.) or string at try.pl
line 12.
Hello World

So $bar is private to foo - but you can't initialise it?

TIA
--
Regards
Dave Saville
 
Reply With Quote
 
 
 
 
Rainer Weikusat
Guest
Posts: n/a
 
      11-13-2012
"Dave Saville" <(E-Mail Removed)> writes:
> Please why does this throw an error?
>
> [T:\tmp]cat try.pl
> use strict;
> use warnings;
>
> foo("Hello World");
> exit;
> {
> my $bar = 1;
>
> sub foo
> {
> my $a = shift;
> print "$bar $a\n"; # <== line 12
> return;
> }
> }
>
> [T:\tmp]try.pl
> Use of uninitialized value in concatenation (.) or string at try.pl
> line 12.
> Hello World


Because $bar = 1 is never executed: The compiler compiles this, then
the program invokes the foo subroutine and exits before the $bar = 1.
You can move the foo("Hello World") below the block or use BEGIN or
INIT to ensure that $bar is actually initialized:

----------
use strict;
use warnings;

foo("Hello World");
exit;
{
my $bar;

INIT {
$bar = 1;
}

sub foo
{
my $a = shift;
print "$bar $a\n"; # <== line 12
return;
}
}
 
Reply With Quote
 
 
 
 
Rainer Weikusat
Guest
Posts: n/a
 
      11-13-2012
Rainer Weikusat <(E-Mail Removed)> writes:
> "Dave Saville" <(E-Mail Removed)> writes:
>> Please why does this throw an error?
>>
>> [T:\tmp]cat try.pl
>> use strict;
>> use warnings;
>>
>> foo("Hello World");
>> exit;
>> {
>> my $bar = 1;
>>
>> sub foo
>> {
>> my $a = shift;
>> print "$bar $a\n"; # <== line 12
>> return;
>> }
>> }
>>
>> [T:\tmp]try.pl
>> Use of uninitialized value in concatenation (.) or string at try.pl
>> line 12.
>> Hello World

>
> Because $bar = 1 is never executed: The compiler compiles this, then
> the program invokes the foo subroutine and exits before the $bar =
> 1.


Additional explanation: Generally, running a Perl program is comprised
of two phases: A compilation step where the Perl compiler reads all
the source code files and transforms the code into some internal
representation (AFAIK called 'an optree') and an execution phase where
the Perl interpreter evaluates/ executes this 'optree'. This means any
statement in a Perl program may have run time or compile time effects
or both and the run time effects aren't visible during compile
time. For this example, this means the compiler creates a compiled
subroutine 'foo' which refers to the $bar defined in the outer block
because 'creating my variables' has the compile-time effect of making
the corresponding names visible to the compiler (so that it can
generated code accessing the proper objects). But the assignment to
$bar does not happen at compile-time, it happens at run-time when the
corresponding statement is executed. An execution of the 'content' of
a file is top-to-bottom: By the time the foo("...:") is executed, a
subroutine referencing the proper $bar has been created but the value
of $bar hasn't yet been set.

So-called 'BEGIN block' can be used to execute Perl code at
compile-time and alternatively, so-called 'INIT blocks' can be used to
run Perl code after the compilation phase has finished and before any
of the 'general' code ran (details are in the perlmod manpage).
 
Reply With Quote
 
C.DeRykus
Guest
Posts: n/a
 
      11-13-2012
On Tuesday, November 13, 2012 5:38:07 AM UTC-8, Dave Saville wrote:
> Please why does this throw an error?
>
>
>
> [T:\tmp]cat try.pl
>
> use strict;
>
> use warnings;
>
>
>
> foo("Hello World");
>
> exit;
>
> {
>
> my $bar = 1;
>
>
>
> sub foo
>
> {
>
> my $a = shift;
>
> print "$bar $a\n"; # <== line 12
>
> return;
>
> }
>
> }
>
>
>
> [T:\tmp]try.pl
>
> Use of uninitialized value in concatenation (.) or string at try.pl
>
> line 12.
>
> Hello World
>
>
>
> So $bar is private to foo - but you can't initialise it?
>
>


And just for a spaghetti western


goto BLOCK;

FOO: foo("Hello World");
exit;

BLOCK: {
my $bar = 1;

sub foo
{
my $a = shift;
print "$bar $a\n";
return;
}
goto FOO;
};
__END__

[ duck ] C.DeRykus
 
Reply With Quote
 
Rainer Weikusat
Guest
Posts: n/a
 
      11-14-2012
"C.DeRykus" <(E-Mail Removed)> writes:

[...]

> goto BLOCK;
>
> FOO: foo("Hello World");
> exit;
>
> BLOCK: {
> my $bar = 1;
>
> sub foo
> {
> my $a = shift;
> print "$bar $a\n";
> return;
> }
> goto FOO;
> };


In contrast to 'other languages', the destination of a Perl goto is
determined at runtime, by 'outward' searching (IIRC, term used by the
Camel book) for a matching label in all currently active lexical
scopes. This implies that, compared to other means for performing flow
control, it is a very expensive operation.

--------------
use Benchmark;

timethese(-5,
{
goto => sub {
goto out;

print "ha!";

out:
return 3;
},

return => sub {
return 3;
}});
 
Reply With Quote
 
C.DeRykus
Guest
Posts: n/a
 
      11-14-2012
On Wednesday, November 14, 2012 5:23:49 AM UTC-8, Rainer Weikusat wrote:
> "C.DeRykus" <(E-Mail Removed)> writes:
>
>
>
> [...]
>
>
>
> > goto BLOCK;

>
> >

>
> > FOO: foo("Hello World");

>
> > exit;

>
> >

>
> > BLOCK: {

>
> > my $bar = 1;

>
> >

>
> > sub foo

>
> > {

>
> > my $a = shift;

>
> > print "$bar $a\n";

>
> > return;

>
> > }

>
> > goto FOO;

>
> > };

>
>
>
> In contrast to 'other languages', the destination of a Perl goto is
>
> determined at runtime, by 'outward' searching (IIRC, term used by the
>
> Camel book) for a matching label in all currently active lexical
>
> scopes. This implies that, compared to other means for performing flow
>
> control, it is a very expensive operation.
>
>
>
> --------------
>
> use Benchmark;
>
>
>
> timethese(-5,
>
> {
>
> goto => sub {
>
> goto out;
>
>
>
> print "ha!";
>
>
>
> out:
>
> return 3;
>
> },
>
>
>
> return => sub {
>
> return 3;
>
> }});


Thanks, that's interesting. I would have
thought that it'd be much cheaper. Of course,
spaghetti code is super bad anyway for all
the reasons we know about. You gotta have
Clint Eastwood's steely gaze and grit to
follow the crooked trails and dodge bullets
that might ricochet anywhere.

That's why I said "duck".

--
Charles DeRykus
 
Reply With Quote
 
dave@deezee.org
Guest
Posts: n/a
 
      11-15-2012
Thanks all. I was a bit confused because my test was pasted straight out of perldocs.

Sorry for the late response but I am having problems with my newsreader and never saw my own post nor any of the replies. Had to log into Google to send this - Yuk I hate web groups.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      11-15-2012
http://www.velocityreviews.com/forums/(E-Mail Removed) writes:
> Thanks all. I was a bit confused because my test was pasted straight
> out of perldocs.
>
> Sorry for the late response but I am having problems with my
> newsreader and never saw my own post nor any of the replies. Had to
> log into Google to send this - Yuk I hate web groups.


Where exactly in the Perl docs did you find that code?

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Usage of Static variable name in static context Vikram Kalra Java 23 09-07-2007 08:51 AM
non-static variable this cannot be referenced from a static context kookey Java 3 08-20-2005 07:43 PM
Does a static variable in a class's member fn always remain static? Sam C++ 4 01-13-2004 11:05 PM
Global static variable vs static method Marcin Vorbrodt C++ 3 09-05-2003 07:52 AM
a static local variable in a static method is thread local storage? Patrick Hoffmann C++ 3 08-08-2003 02:37 PM



Advertisments