Aaah, sorry - I misread/misunderstood what you wanted to do.

A global version of what you seem to want to do is Object::Import, which exports some or all methods of a (default) object and makes them available as functions:

use Object::Import $object; foo(@bar); # now means $object->foo(@bar);

The module does not act on a lexical scope though, and I'm not sure what a good way would be to save/restore this lexical scope. I would expect parsing mishaps / parsing confusion for Perl if you redefine subroutine bindings based on the dynamic scope, and I don't see any better way to implement something like this in Perl, unless you implement something like the Javascript version of with as well. Importing dynamically is easy:

{ my $object = Foo::Bar->new(); import Object::Import $object; # Make all methods of $object avail +able as functions }

The hard part is unimporting stuff, and you could do that by localizing your namespace every time you import an object, but that will be weird if you actually try to change your namespace:

#!perl use strict; use warnings; package Foo::Bar; sub frobnitz { print "frobnitz in " . __PACKAGE__ . "\n"; }; package Foo::Whatever; use strict; use warnings; require Object::Import; sub frobnitz { print "frobnitz in " . __PACKAGE__ ."\n"; }; frobnitz(); { local *Foo::Whatever::frobnitz; my $object = {}; bless $object => "Foo::Bar"; Object::Import->import( $object, list => ['frobnitz'], nowarn_rede +fine => 1 ); # Make all methods of $object available as functions frobnitz(); # call $object->frobnitz(); } frobnitz(); __END__ frobnitz in Foo::Whatever frobnitz in Foo::Bar frobnitz in Foo::Whatever

In the above, I localize frobnitz(), but the hard part will be to make that localization non-local when putting all of that in a subroutine with. Maybe namespace::autoclean has a hint on how to implement something like this.

If you don't care that much about restoring the dynamic namespace/scope again, Object::Import should already work.


In reply to Re^3: "importing" methods? by Corion
in thread "importing" methods? by LanX

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.