On Mon, 18 Jun 2001, Nick Transier wrote:

> Here is a basic attribute definition from an O'Reilly Book:
>
> sub new {
>
>       my $invocant = shift;
>       my $class = ref($invocant) || $invocant;
>       my $self = {
>                    color => "bay",
>                    legs => 4,
>                    @_ ,
>         };
>       return bless $self,$class;
>
> }
>
> What I do not understand is why $self is not defined as a hash %self.

It is defined as a hash -- a hash reference that is blessed into the
package (for all methods, $self is passed implicitly as the first
argument).  You can certainly create a hash %self and bless a reference to
it into the class.  $self can be a reference to just about anything you
want -- the technical term to what $self is pointing to is 'thingy' --
could be an array, a scalar, etc.

> Also, if you were to only include @_, would you need a comma after it
> -- in other words my $self = {@_}; or {@_,};

Technically, the comma isn't needed the way $self is defined above.  Perl
won't complain one way or the other (unlike other languages... :-) ).

-- Brett
                                   http://www.chapelperilous.net/btfwk/
------------------------------------------------------------------------
More people are flattered into virtue than bullied out of vice.
                -- R.S. Surtees

Reply via email to