Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Export methods to subroutines

by ambrus (Abbot)
on Jul 13, 2010 at 21:09 UTC ( #849373=CUFP: print w/replies, xml ) Need Help??

exussum0 asked in the chatterbox whether there's a quick way to wrap the methods of an object such that you can access them as plain functions which are exported to a package. Thus, here's my solution.

Take this module:

package Wrapobj; our $VERSION = 1.000; use Scalar::Util "blessed"; use mro; sub import { my $en = caller; my $ens = $en . "::"; my $ep = \%{(do { no strict "refs"; $ens })}; my($_u, $o) = @_; my $c = blessed($o) || $o; for my $n (@{mro::get_linear_isa($c)}) { my $p = \%{(do { no strict "refs"; $n . "::" })}; for my $s (sort keys %$p) { if (exists(&{$$p{$s}})) { if (!$$ep{$s} || !exists(&{$$ep{$s}})) { *{$ens . $s} = sub { $o->${\$s}(@_) }; } } } } } 1; __END__

Now you can import the methods of an object $obj by saying use Wrapobj $obj;. For example, you could say

use Math::BigInt; use Wrapobj Math::BigInt->new("100"); warn bmul(2); +warn as_hex()

You could even use this on a class to access class methods:

use Math::BigInt; use Wrapobj Math::BigInt::; warn new("0x100");

Of course, this is just a proof of concept module, a real one would allow you to explicitly set which subs you want to import, and frankly, you likely don't want to use this kind of thing in real code anyway.

(See also Foo::Simple.)

Replies are listed 'Best First'.
Re: Export methods to subroutines
by exussum0 (Vicar) on Jul 16, 2010 at 13:43 UTC
    Correct me if I'm wrong, but I also asked for a pony.
      I also asked for a pony

      have one: 𐂃

Re: Export methods to subroutines
by exussum0 (Vicar) on Jul 21, 2010 at 14:05 UTC
    NB: The reason I asked about this is..

    I have an object in some controller-ish/test-classish code. Doing $foo->something every other line became tedious. $foo was my star object for interaction, so wouldn't it be nice, similar to Test::More, to have ok(...) available.

    The hitch is, $foo is a stateful object, so my class would need to be a singleton. So for some cases, it'd be nice to use my class as

    $foo = Foo->new; $foo->bar; $foo->baz;
    And at others...
    use Foo::ExportedOrSomethingClever; bar(); baz(); ... ..
    If the state got in the way and I could not separate things, I could go back to my first example. Ambrus is right, this shouldn't be used all over the place. State plus namespace clashes are a pain.
Re: Export methods to subroutines
by ambrus (Abbot) on Nov 07, 2010 at 13:07 UTC

    I've now turned this to a complete module. Should be available from CPAN at Object::Import soon. Thanks to Corion for the inspiration.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://849373]
Approved by sweetblood
Front-paged by Arunbear
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2019-07-20 19:56 GMT
Find Nodes?
    Voting Booth?
    If you were the first to set foot on the Moon, what would be your epigram?

    Results (5 votes). Check out past polls.