> I am (admitedly) unfamiliar with OO Perl.  I understand enough to grok
> what you are saying, Wiggins, but I have a question.
> 
> Does a sub (like the one above) have a problem with being called with
> & as opposed to not being called with an & with OO Perl?  That
> questions was worded weird.  Let me try again.  As I understand it, if
> you call a sub with '&subname', the sub's @_ variable will share the
> calling scope's @_ variable, BUT, if you call the sub with 'subname()'
> it will get it's own, fresh @_.  is that true?  And if it is, does
> this affect the subs being used with OO Perl?
> 
> --Errin

Solid question, which took some hunting in the docs.... but they are
solid docs, from perldoc perltoot, 

"From the C++ perspective, all methods in Perl are virtual.  This, by
the way, is why they are never checked for function prototypes in the
argument list as regular builtin and user-defined functions can be."

This confirmed my hunch.  The C<&> has a number of purposes beyond just
passing the current @_ in recursive like functions, such as
dereferencing subroutines, etc.   See perldoc perlsub for lots about
subroutines, prototypes, and their behaviours.  My hunch was that since
you invoke a method on an object/class, such as

My::Class->method();
or 
$object->method();

You would have to figure out where to put the sigil, 

&$object->method();

Would either cause confusion wrt trying to dereference a subroutine, or
trying to chain a method on the return value of a dereferenced
subroutine, which would be pretty cool, though not terribly readable. Or,

$object->&method();

Which I suspected could work, but had never seen it before (not that I
am an expert) but it strikes me as something difficult to parse.

Now to the more important part, and the more literal meaning of your
question, in Perl it doesn't affect anything!  Why? because the method,
when invoked as a regular sub call will run just fine!  The danger is
that you (may) have broken the interface, so if the sub is really a
method *expecting* the first argument to be an object/class, then it may
fail to function as documented. This is where the beauty of Perl comes
in though, why can't you have both!  For instance many subs can check
their first argument to see if it is a reference, in that case it treats
the call like a method, if the first arg isn't, then it treats it like a
regular sub call.  This is similar to other areas of Perl that function
based on the *context*.  Many modules that provide a functional and OOP
interface are operating under this principal.  So whether or not Perl
will let you, it will, whether or not you can/should depends on the
underlying implementation and whether it is documented.  Calling what is
normally a method with C<&> as a regular sub call will pass @_
unmangled, the question becomes does @_ already have the object/class
attached, or is the sub smart enough to recognize that it doesn't.

http://danconia.org

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to