[Petal] Repeat object and Safe compartment

William McKee william at knowmad.com
Tue Aug 12 15:33:42 BST 2003


On Tue, Aug 12, 2003 at 05:22:11PM +0100, Fergal Daly wrote:
> 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.

Trying the above resulted in a syntax error -- "Can't modify glob elem
in scalar assignment". I changed the * to a $ which got us back to the
problem with $MAX and $CUR not being initialized.

I feel like I'm dancing all around the (intermediate) solution but that
what we need is just out of reach! But that shouldn't matter if
Petal::Hash_Repeat becomes a true object with a constructor that allows
independent properties.



> the problem is that with Petal at the moment $outer->number and $inner->number 
> both see the same value for $CUR.

In OO terminology, this would be a class attribute, right?


> The code should look more like this

Ok, I've taken a stab at updating Petal::Hash_Repeat to use private
variables. The diff is attached. I came across an issue with displaying
a '0' value while running my tests. Is this a known problem?


William

-- 
Knowmad Services Inc.
http://www.knowmad.com
-------------- next part --------------
12,21c12,21
< our $CUR = undef;
< our $MAX = undef;
< 
< sub number { $CUR + 1 }
< sub index  { $CUR }
< sub even   { not $CUR % 2 }
< sub odd    { $CUR % 2 }
< sub start  { not $CUR }
< sub end    { $CUR == $MAX }
< sub inner  { $CUR and $CUR < $MAX }
---
> sub new { bless {'_MAX' => undef, '_CUR' => undef}, $_[0]; }
> sub set_max { $_[0]->{'_MAX'} = $_[1] }
> sub set_cur { $_[0]->{'_CUR'} = $_[1] }
> sub number { $_[0]->{'_CUR'} + 1 }
> sub index  { $_[0]->{'_CUR'} }
> sub even   { not $_[0]->{'_CUR'} % 2 }
> sub odd    { $_[0]->{'_CUR'} % 2 }
> sub start  { not $_[0]->{'_CUR'} }
> sub end    { $_[0]->{'_CUR'} == $_[0]->{'_MAX'} }
> sub inner  { $_[0]->{'_CUR'} and $_[0]->{'_CUR'} < $_[0]->{'_MAX'} }
120c120,121
<     $self->{repeat} = bless {}, 'Petal::Hash_Repeat';
---
> 	#$self->{repeat} = bless {}, 'Petal::Hash_Repeat';
>     $self->{repeat} = Petal::Hash_Repeat->new();
-------------- next part --------------
17a18,20
> # makes taint mode happy if set to 1
> our $TAINT = undef;
> 
119a123
> 	$TAINT = $petal_object->taint();
382,384c386
<     
<     $class->add_code ("local \$Petal::Hash_Repeat::MAX = \@array - 1;");
<     $class->add_code ("local \$Petal::Hash_Repeat::CUR = undef;");
---
> 
388c390,391
<     $class->add_code ("\$Petal::Hash_Repeat::CUR = \$i;");
---
> 	$class->add_code ("\$hash->{repeat}->set_cur(\$i);");
> 	$class->add_code ("\$hash->{repeat}->set_max(\@array - 1);");


More information about the Petal mailing list