Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Object Oriented packages - classes and subroutines

by jplindstrom (Monsignor)
on Sep 28, 2008 at 22:11 UTC ( [id://714221]=note: print w/replies, xml ) Need Help??


in reply to Object Oriented packages - classes and subroutines

I would make them methods. Reasons for this:

Treat them like other code. There is no reason not to. In a class, letting the routines be methods rather than functions (or whatever you want to call it) are the idiomatic norm, not the exception.

Consistency is good on a more pragmatic level too. By having all routines being methods, you don't need to keep track of whether $self / $class should be shifted off @_ or not. You don't need to worry about which routines are called in a particular way -- they're all $self->whatever(). I find myself making this specific mistake surprisingly often when some things were made subroutines.

The problem with not passing around $self (i.e. treating them like methods) is that sooner or later you'll need it in a routine in order to call a method on $self, and then you'll need to rewrite all of the things that call this routine, and everything that calls those routines, etc.

If a routine is clearly not related to the object instance ($self), make it a class method (shift off $class, call the method like this: My::Class->whatever() ) instead, because...

You almost certainly will want to override one of them in sub classes sooner or later, for the same reason you might want to override the rest of your methods. If you write a CPAN library, this is even more frustrating for other people who have needs to tweak the code that you didn't anticipate at the time.

/J

Replies are listed 'Best First'.
Re^2: Object Oriented packages - classes and subroutines
by educated_foo (Vicar) on Sep 30, 2008 at 11:55 UTC
    If a routine is clearly not related to the object instance ($self), make it a class method (shift off $class, call the method like this: My::Class->whatever() ) instead, because.. You almost certainly will want to override one of them
    This is actually really annoying as an interface, because it means that whatever can't be exported. And in my experience, for every time you want to override a class method/function, there are hundreds of times you just want to call one.
      Sorry, I don't get it. Call it like this:

      my $whatever = My::Class->whatever();

      Is it the verbosity of the class name that bothers you or something else?

      /J

        Yes, it's the totally unnecessary verbosity that's extremely annoying to get rid of. That doesn't bother you?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://714221]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-03-29 15:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found