On Saturday, Nov 22, 2003, at 08:17 US/Pacific, James Edward Gray II wrote:


On Nov 21, 2003, at 3:19 PM, Rajesh Dorairajan wrote:

I've a class (blessed, of course :)) that has variables like:

$self->a = "1";
$self->b = "2";
$self->c = [ '1', '2', '3', '4' ];

I think/hope you meant:


$self->{a} = '1';            # any reason we're quoting integers?
$self->{b} = '2';
$self->{c} = [ qw(1 2 3 4) ];

Now, I want to write a foreach loop to iterate through $self->c and print
the values. However:


foreach my $foo ( $self->c ) {
        print $foo;
}

for my $foo ( @{ $self->{c} } ) { print $foo; }

Hope that helps.

There is the other idea that we might want to think about here - and that would be an 'accessor' approach.

        sub c
        {
                return( (wantarray) ? @{$_[0]->{c}} : $_[0]->{c});
        }

this would allow us code like

        my $obj = new Foo::Bar;
        
        foreach my $item ( $obj->c ) # or $obj->c() if you wish.
        {
                print "$item \n";
        }
        
        my $array_ref = $obj->c;
        
        foreach (@$array_ref )
        {
                print "we see: $_\n";
        }
        

the alternative of course is to use a more
expressive name for 'c' so that the

$obj->c_value()

would READ as to what one was getting out of the obj.


ciao drieux

---


-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to