Anti-nuisance lawsuit warning: The purpose of these notes is to remind me, Zoegond, of stuff or to help me work stuff out. They may contain mistakes.

Quick

  • ($a, $b....) = unpack("A2A7...", $packed)
  • push( array, list )

Friday, November 16, 2012

Perl :: and class methods

If you call an instance method as a static method - ie if you usually use it thus

$dalek->say("Exterminate");

and call it thus

Dalek::say("Exterminate");

then the method will NOT get the class name as its first argument, as I always assumed it would. It will only have the arguments explicitly supplied, which will probably mean it won't work, because in our example "Exterminate" will end up in the $inst or $self position in say.

To get the expected result, put

Dalek->say("Exterminate");

which I didn't think was allowed. This will supply "Dalek" as $_[0] as expected.

Or you could put

Dalek::say("Dalek", "Exterminate");

which I think is not as clear.

Actually, I now realise Perl doesn't recognise any difference between instance and static methods. What I should be saying above is that

Dalek::say("Exterminate")

means simply 'say("Exterminate") using the say subroutine in package Dalek', whereas

$dalek->say("Exterminate")

means 'Check that $dalek is a reference to something blessed, and if it is, call say with the name of the class it was blessed into as the first argument, and "Exterminate" as the second.
And for completeness, presumably the other example above

Dalek->say("Exterminate")

means 'Enquote the bareword to the left of ->, that gives us "Dalek", now call Dalek::say with "Dalek" as first argument and "Exterminate" as second.

Followers

Blog Archive