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

Oron Peled oron at actcom.co.il
Wed Nov 2 01:53:21 PST 2005

On Wednesday, 2 בNovember 2005 10:57, Offer Kaye wrote:
> or even this if you'd like it to be used as either a static or a
> virtual method.

In object-oriented terms, static methods relate to the whole class
while non-static methods relate to a specific object. The name
static is an unfortunate relic from the C++ (and later Java) syntax.
Better names (which I try to use when I can) are "Class method" and
"Object method".

Example in C++ syntax:
  class Foo {
    static void bar();	// A static (Class) method
    void baz(int x);	// An object method
    static int number_of_foos;	// Static data
    int my_x;			// Object data

  f = new Foo;	// Let's ignore construction details...
  Foo::bar();	// Calls a static (Class) method
  f->baz(5);	// Calls an object method
  f->bar();	// Equivalent to Foo::bar() (just bad syntactic sugar).

In perl both static and non-static methods are defined the same way.
The only difference is the calling syntax:

  $f = new Foo;	# Assume some constructor was written
  Foo->bar();	# Class method call (first arg to bar is "Foo")
  $f->baz(5);	# Object method call (first arg to baz is $f, second is 5)

Now what if we want to enable:
Easy, only now the first arg to bar is $f and no "Foo".

Now, since in perl constructors are just plain class methods
and not language keywords (as new is in C++, Java, etc.)
we can apply this knowledge as follows:
>  sub new {
>      my $self  = shift;
>      my $class = ref($self) || $self;
>      return bless {}, $class;
>  }

Use examples:
  $f = Foo->new();	# First arg ($self) is "Foo", ref($self) is undef
  $g = $f->new();	# Now $self is $f, ref($f) is "Foo"

So our improved new can bless correctly the two cases:
  1. When explicitly given a class name (the common case)
  2. When given an object of that class (give me another of *that* type)

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

This is to show to OOP people that the name is arbitrary and not
hardcoded into the language. C++ has the keyword 'this' while SmallTalk
used the famous 'self' for the same purpose.

> I'd like to understand exactly...

The docs were obviously designed for OOP people migrating from other
OOP languages to perl and looking for "how this OO feature is done in perl".

Your question shows that it is not good for people who have perl
as their first OO language.

> Hopefully I will get a good enough answer that I will understand this
> issue and be able to sumbit a patch to p5p for the above two PODs (I
> hate broken docs)... :)

Hope it pointed you in the right direction.

Oron Peled                             Voice/Fax: +972-4-8228492
oron at actcom.co.il                  http://www.actcom.co.il/~oron
ICQ UIN: 16527398

UNIX is user friendly. It's just selective about who its friends are.

More information about the Perl mailing list