[tpm] private method call nuances

Madison Kelly linux at alteeve.com
Mon Jul 20 19:48:44 PDT 2009


Uri Guttman wrote:
>>>>>> "AB" == Alex Beamish <talexb at gmail.com> writes:
> 
>   AB> On Mon, Jul 20, 2009 at 7:57 PM, Madison Kelly<linux at alteeve.com> wrote:
>   >> Hi all,
>   >> 
>   >>  I was wondering what, if anything, would be different between calling a
>   >> module's (private) method in these two ways:
>   >> 
>   >> $self->_count_module();
>   >> - vs -
>   >> &_count_module($self);
>   >> 
>   >>  Of course, '$self' is a (hash) reference 'bless'ed into the current module.
>   >> Functionally I don't see a difference (yet).
> 
>   AB> Functionally, I don't see a difference either, but I have to admit I
>   AB> squirm whenever I see & in front of a function/method call.
> 
> there is a major difference between those. massive in fact.
> 
>   AB> Unless you're calling a reference to a sub, it's unnecessary (and
>   AB> possibly mis-leading, because @_ is used as parameters if a function
>   AB> is called with & and no parameters).
> 
> that is one very important issue. calling subs with & is bad in general.
> 
>   AB> Second, calling a method by passing $self in as the first parameter is
>   AB> correct, but not OO-Perlish. The method _count_module is a method
>   AB> (albeit a private one), and so should be called in an OO way.
> 
> actually it is incorrect since it bypasses any inheritance. even private
> methods can inherit from parent classes. if you want to use a
> local/private sub then you can pass parts of the object as you would in
> any other call. you can even pass the object as OP did but it is just
> bad coding to pass the object as the first arg.
> 
> uri
> 

Thanks both for replying.

I guess I should have re-worded my question some... I'm working on the 
intro to (oo) modules talk and was trying to understand why using 
'$self->_some_method' was better than '&_some_method($self)'. The latter 
might be more comfortable to a coder just getting into modules, and I 
really don't like saying "just because". :)

Can you explain, or point me somewhere to better understand, how 
inheritance breaks using the '&' method? Also, I was a little unclear... 
if you pass in '$self', does that restore the inheritance? I just want 
to be clear so that I can best explain it in my paper.

Thanks!

Madi


More information about the toronto-pm mailing list