[Petal] Repeat object and Safe compartment

Fergal Daly fergal at esatclear.ie
Tue Aug 12 18:22:11 BST 2003


On Tuesday 12 August 2003 17:00, William McKee wrote:
> On Tue, Aug 12, 2003 at 03:25:55PM +0100, Fergal Daly wrote:
> > I think to use them all you need to do this somewhere before entering
> > safe mode
> >
> > *Petal::CPT::Petal::Hash_Repeat::CUR = \$Petal::Hash_Repeat::CUR;
>
> Thanks for the pointer Fergal. I tried this solution and found that I
> was getting a SCALAR reference back when trying to access the index and
> other functions. However, if I simply set the glob to
> $Petal::Hash_Repeat::CUR, then I'd get 'use of unitialized value'
> errors when trying to access the index and other functions.

Strange, you could try

*Petal::CPT::Petal::Hash_Repeat::CUR{SCALAR} = \$Petal::Hash_Repeat::CUR;

but you shouldn't have to. Setting the glob to $Petal::Hash_Repeat::CUR 
definitely won't work.


> I guess we could check to see if $CUR is a reference and dereference it
> but it sounds like you want to move us away from the use of globals
> anyhow.

Using globals means that there can only be 1 _visible_ repeat counter at any 
time. Well actually it's even stranger than that, if you set some variable to 
hold a the repeat counter for your outer loop, when you look into that 
variable later on it might hold a totally different repeat counter.

> >> Again these variables are scoped, you can safely nest loops, ifs etc...
> >> as much as you like and everything should be fine.
>
> And looking at the template code produced by CodeGenerator.pm, the CUR
> and MAX vars are scoped. However, the example you gave does not even
> work for me. I can get the index when I'm in the outer repeat loop.
> However, I can get neither rep1 nor repeat index when inside the inner
> loop.

They are scoped, but there's only 1 counter. It's like this piece of Perl

my $repeat = Petal::Hash_Repeat->new;
local $CUR = 0;
while ($CUR <= 10)
{
	$CUR++;
	$outer = $repeat;

	local $CUR = 0;
	while ($CUR <= 10)
	{
		print "inner = ".$repeat->number."\n";
		print "outer = ".$outer->number."\n"; # ****
	}
}

the problem is that with Petal at the moment $outer->number and $inner->number 
both see the same value for $CUR. The code should look more like this

my $repeat = Petal::Hash_Repeat->new;
while ($repeat->number <= 10)
{
	$outer = $repeat;

	local $repeat = Petal::Hash_Repeat->new;
	while ($repeat->number <= 10)
	{
		print "inner = ".$repeat->number."\n";
		print "outer = ".$outer->number."\n"; # ****
	}
}

each Petal::Hash_Repeat object keeps it's own private $CUR and there's no 
interference,

F



More information about the Petal mailing list