Perl OO Tip of the Day

chromatic on 2007-03-15T19:24:13

Methods are not functions. If you call a method as a function, you might get the right results and you might get the wrong results. If the documentation shows you how to call a method as a method and you call it as a function and get the wrong results, that's your fault and you should stop doing it if you care about getting the right results consistently and reliably.

You should especially not file bug reports about getting the wrong results if you use the code incorrectly.

That is all.


Visuals...

Matts on 2007-03-15T19:40:21

This post sends visions of CGI.pm whistling through my head :-)

Re:Visuals...

chromatic on 2007-03-15T20:51:45

At least the documentation supports that operation... but a cleaner way to perform the same behavior is to export functions that operate on an automatically-generated singleton, not to try to detect a valid invocant.

It's funny how you mention that though, as I've written patches for CGI.pm to prevent it from breaking Liskov's substitutability principle.

Response to bug report

jdavidb on 2007-03-15T19:49:25

This is not a bug. The documentation clearly indicates that calling $obj->routine() will return a scalar. However, calling Package::routine() is intended to throw a fatal error. This is exactly the results you are getting, and so it is not a bug.

:)

It's where you look over the three items of the bug report formula and verify that when the action described in point 1 is taken, the results described in point 3 are correct, and that the expectation described in point 2 is wrong.

Eek! The memories!

drhyde on 2007-03-16T11:24:17

One of the biggest mistakes I've made in my stuff on the CPAN is that in Number::Phone and Number::Phone::UK everything can be called as either an object method, a class method, or as a function. Yuck. Add to that that most of the functions (being almost identical, just lookups in a database or calls through to another module) are auto-generated at run-time, and it's a screaming nightmare.

They really need a re-write.