[Melbourne-pm] scope and gotos

Simon Taylor simon at unisolve.com.au
Mon Jan 8 17:51:45 PST 2007


G'day Tim,

> I know gotos are bad, mmmkay (actual, Djikstra was going on about
> unconditional gotos, not error handling gotos), but is this the desired
> behaviour of goto jumping out of the scope of something that has been
> declared, falling back into it, and the old value remaining?

As far as I can see, there is only "file scope" in your example. In other
words the @test array has the scope of the whole file. The goto statement 
and the init: label it is branching to are all within the same scope.

Of course, you could *create* a brace-enclosed scope like this to achieve the
effect you want:

-------------
#!/usr/bin/perl
use strict;
use warnings;

print "init\n";
{
init:

my @test;

push @test, int(@test);

print "test=@test\n";
<STDIN>;
}
goto init;
-------------

But no one will talk to you at parties if you do that! ;-)

Regards,

Simon

>
> ###
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> print "init\n";
>
> init:
>
> my @test;
>
> push @test, int(@test);
>
> print "test=@test\n";
> goto init;
>
> ###
>
> init
> test=0
> test=0 1
> test=0 1 2
> ...
>
> Sure, I *can* go and reinitialise all these arrays, but I thought a
> declaration ought to be good enough.

-- 
Unisolve Pty Ltd - Melbourne, Australia
+61 3 9568 2005



More information about the Melbourne-pm mailing list