Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Redefined import method and EXPORT not working

by Corion (Patriarch)
on Dec 22, 2014 at 08:11 UTC ( [id://1110998]=note: print w/replies, xml ) Need Help??


in reply to Redefined import method and EXPORT not working

@EXPORT is not magical. It's just a variable. All the magic must happen in import.

The easiest approach would be to use Exporter::import, and stash that away and at the end, pass control to it:

use vars qw(@EXPORT $orig_import); use Exporter 'import'; sub my_import { print "Imported\n"; goto &$orig_import; }; BEGIN { # Install our own import routine over import: $orig_import= \&import; *import= \&my_import; };

Replies are listed 'Best First'.
Re^2: Redefined import method and EXPORT not working
by tobyink (Canon) on Dec 22, 2014 at 11:03 UTC

    That seems like an over-complicated way of doing:

    use Exporter (); sub import { print "Imported\n"; goto \&Exporter::import; }
      Great!
Re^2: Redefined import method and EXPORT not working
by Anonymous Monk on Dec 22, 2014 at 09:44 UTC

    That worked

    package M1; use parent qw(Exporter); @EXPORT=qw(func); sub func{print "Hello"} sub import{ print "Imported"; print @_; M1->export_to_level(1,@_)} 1;
    perl -I. -we 'use M1 qw(func);func()'

      Please, don't inherit from Exporter. Just use use Exporter 'import';. Inheritance as a way of getting access to methods of a module is not what I consider a clean approach.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-04-19 22:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found