Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Re: Re: Perl module symbol import versus direct invocation

by JayBonci (Curate)
on Feb 26, 2003 at 09:57 UTC ( #238720=note: print w/replies, xml ) Need Help??


in reply to Re: Re: Perl module symbol import versus direct invocation
in thread Perl module symbol import versus direct invocation

So I guess my next logical question is, why have a Class->meth() call at all, if it's not quite an Object call, and it's still not calling the function with the intended arguments? It uses a weak reference to the classname, and is likely to break things for non-OO modules.

I suppose you could intend for your module to be used that way, but is there a sane way to test for how your module is being used, other than to always check the first parameter to make sure that it's not your classname? Or is this just a matter of "read the documentation before use"?

    --jaybonci
  • Comment on Re: Re: Re: Perl module symbol import versus direct invocation

Replies are listed 'Best First'.
Re: Re: Re: Re: Perl module symbol import versus direct invocation
by bart (Canon) on Feb 26, 2003 at 12:48 UTC
    I agree it's very annoying that on the callee side, i.e. in the sub, it's virtually impossible to distinguish between being called as a function or as a method. Looks at the mess CGI.pm made of it — look into the subs "self_or_default" and "self_or_CGI".

    A reason to have calls as class methods instead of as functions, is the simple fact that now, inheritance works. It doesn't when you call a sub as a function.

    Two rather well known class methods are import, automatically invoked by use — and often inherited :-) from Exporter, and new, a name people often choose for a sub that serves to create a new instance of a class.

      I agree it's very annoying that on the callee side, i.e. in the sub, it's virtually impossible to distinguish between being called as a function or as a method.
      { package Foo; use Devel::Caller qw(called_as_method); sub new { bless {}, shift } sub foo { my $arg = shift; if (called_as_method(0)) { ref($arg) ? "object method" : "class method"; } else { "subroutine"; } } } use Test::More tests => 3; my $foo = Foo->new; is( Foo->foo, "class method" ); is( $foo->foo, "object method" ); is( Foo::foo($foo), "subroutine" );

      (although wanting to do this in real code is nearly always a sign that you need to rethink your design :-)

Re^4: Perl module symbol import versus direct invocation
by adrianh (Chancellor) on Feb 26, 2003 at 10:20 UTC

    You can use Devel::Caller's called_as_method to determine when a particular subroutine is being called as an object/class method or as a normal subroutine.

    However, in my opinion, mixing function/procedural and OO APIs in the same subroutines is almost always a bad idea :-)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2023-01-30 12:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?