[Israel.pm] Confused about "my $class = ref($self) || $self;"

Mikhael Goikhman migo at homemail.com
Wed Nov 2 02:40:46 PST 2005

On 02 Nov 2005 10:57:10 +0200, Offer Kaye wrote:
> I'm confused about the exact meaning and usage of the construct:
>    my $class = ref($self) || $self;

I think others explained that this is supposed to be [additionally] used
to create another instance of the same class given an existing object.
It's up to you whether to allow such usage of constractor or not. You may
disallow it and only implement $obj->clone method for copying purposes.

> As a further annoyance, the two POD pages use different variable names
> for what is essentially the same example ($this vs. $self).

I use the convention suggested in one of these pages:

  $self is always an object reference
  $class is always a class name
  $this may be either an object reference or a class name

Then you may have this or similar code in any method:

  my $this = shift;
  my $class = ref($this) || $this;
  my $self = ref($this) && $this || die "No reference given";


perl -e 'print+chr(64+hex)for+split//,d9b815c07f9b8d1e'

More information about the Perl mailing list