in reply to (tye)Re: Reference as the only Object element
in thread Reference as the only Object element

> Try $self=[@_];

Where? :-)

What I mean with "pass this reference" is actually "clone a reference". Again assume you have a parser and this parser is filling you a structure (basically a list). You let your parser return a reference to the structure created and you would like your Entry Object to store THAT Reference.

This seems not possible. BTW: You throw up an interesting point:

>Anyway, I wouldn't ever put \@_ into an object as changes...

How do you declare an object whose only element is a reference to a list (or hash for that matter)?

Thanks Richard

Replies are listed 'Best First'.
Re: Re: (tye)Re: Reference as the only Object element
by dragonchild (Archbishop) on Nov 02, 2001 at 21:58 UTC
    You're confusing the implentation of the object and the attributes of that object.

    If you want to declare an object which is implented as a list, I'd do something like:

    sub new { my $class = shift; my $self = [@_]; bless $self, $class; return $self; } sub getList { my $self = shift; if (wantarray) { return @$self; else { return [@$self]; } }

    If you wanted to have a class which had only one attribute and that attribute was a list, I'd do something like:

    sub new { my $class = shift; my $self = {}; bless $self, $class; $self->{LIST} = [@_]; return $self; } sub getList { my $self = shift; if (wantarray) { return @{$self->{LIST}}; else { return [@$self->{LIST}]; } }
    Then, you would add setters as well. However, we already have something like this, in the form of tie and Tie::Array (and its cousin Tie::Hash). Check those out.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

      After chatting with Fatvamp, I think what he wants is something close to this:

      sub new { return bless [], shift; } sub addItems { my $self= shift; unshift @$self, @_; } sub setList { my $self= shift; my( $ref )= @_; if( 1 == @_ && ref($ref) ) { @$self= @$ref; } else { @$self= @_; } } sub getItem { my $self= shift; my $idx= shift; return $self->[$idx]; } sub getList { my $self= shift; if (wantarray) { return @$self; } else { return [@$self]; } }

              - tye (but my friends call me "Tye")
        Basically, he wants some class that acts as an array. That works just fine.

        I'd make one reccomendation - be positive that addItem() is always going to do an unshift. It's more intuitive to add to the end vs. the beginning of a list.

        ------
        We are the carpenters and bricklayers of the Information Age.

        Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.