SPUG:Object property question

Michael R. Wolf MichaelRunningWolf at att.net
Fri May 2 02:10:04 CDT 2003


Sanford Morton <smorton at pobox.com> writes:

[...]

> sub new {        # constructor     ==> changes and prints first name
>     my $class = shift;
>     my $self = {};
>     $self->_init(@_);

NOPE!!!! Can't call a member method on an unblessed reference. Well,
you can, but it doesn't do the magic of adding the referent as the
first argument as you assume in the code below. But if you reshuffle
the code, you could do it.

>     return bless $self, $class;
> }
> 

sub new {
    my $class = shift;
    my $self = bless {}, $class;
    return $self->_init(@_);
}

[...]

> sub _init {      # initialization, private
>     my $self = shift;  
>     $self->{'_firstname'} = $_[0];
>     $self->{'_lastname'} = $_[1];
>     $self->{'_id'} = ++$lastid;
      return $self;   # A handy trick for chaining as above.
> }


Golf anyone?  

    sub new{(bless{},shift)->_init(@_);}


[[N.B. Untested. Uck. I wouldn't even try to test something like this
because I wouldn't even write it. It *is* syntactically correct. I
think it's semantically correct.]]


-- 
Michael R. Wolf
    All mammals learn by playing!
        MichaelRunningWolf at att.net




More information about the spug-list mailing list