How to call the standard Exporter::import routine

Discussion in 'Perl Misc' started by Klaus, Jun 24, 2012.

  1. Klaus

    Klaus Guest

    Hi everybody,

    I am trying to write a custom import routine for one of my modules,
    but I am having trouble calling the standard Exporter::import routine
    from inside my custom import routine.

    here is my code:

    ==================
    package XML::Reader;

    use strict;
    use warnings;
    use Carp;

    require Exporter;

    our @ISA = qw(Exporter);
    our %EXPORT_TAGS = ( all => [ qw(slurp_xml) ] );
    our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
    our @EXPORT = qw();
    our $VERSION = '0.40';

    my $use_module;

    sub import {
    my $calling_module = shift;

    my @plist;

    for my $sub (@_) {
    if ($sub eq 'XML::parser' or $sub eq 'XML::parsepp') {
    if (defined $use_module) {
    die "Duplicate module ('$use_module' and '$sub')";
    }
    $use_module = $sub;
    }
    else {
    push @plist, $sub;
    }
    }

    unless (defined $use_module) {
    $use_module = 'XML::parser';
    }

    if ($use_module eq 'XML::parser') {
    require XML::parser;
    }
    elsif ($use_module eq 'XML::parsepp') {
    require XML::parsepp;
    }
    else {
    die "Can't identify use_module = '$use_module'";
    }

    no strict 'refs';

    my ($package, $file, $line) = caller;

    for my $sub (@plist) {
    my $found = 0;
    for my $ex_ok (@EXPORT_OK) {
    if ($sub eq $ex_ok) {
    $found = 1;
    last;
    }
    }
    if ($found) {
    *{$package."::$sub"} = \&$sub;
    }
    else {
    die "Requested invalid subroutine '$sub'";
    }
    }
    }
    ===============

    My program basically works, but I am not happy about the following
    code section

    ===============
    no strict 'refs';

    my ($package, $file, $line) = caller;

    for my $sub (@plist) {
    my $found = 0;
    for my $ex_ok (@EXPORT_OK) {
    if ($sub eq $ex_ok) {
    $found = 1;
    last;
    }
    }
    if ($found) {
    *{$package."::$sub"} = \&$sub;
    }
    else {
    die "Requested invalid subroutine '$sub'";
    }
    }
    ===============

    That part of the code should normally be handled by
    Exoprter::import(), but how do I call it ?
    Klaus, Jun 24, 2012
    #1
    1. Advertising

  2. Klaus

    Klaus Guest

    On 24 juin, 21:03, Ben Morrow <> wrote:
    > Quoth Klaus <>:
    >
    > > Hi everybody,

    >
    > > I am trying to write a custom import routine for one of my modules,
    > > but I am having trouble calling the standard Exporter::import routine
    > > from inside my custom import routine.

    >
    > See Exporter->export_to_level.


    Thanks, works perfectly.

    -- Klaus
    Klaus, Jun 25, 2012
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Olivier Ricou

    RMI, Exporter and Runnable

    Olivier Ricou, Jul 29, 2003, in forum: Java
    Replies:
    3
    Views:
    6,138
  2. Jonathan

    DXL exporter and VB.net

    Jonathan, Jul 28, 2004, in forum: XML
    Replies:
    0
    Views:
    933
    Jonathan
    Jul 28, 2004
  3. mac
    Replies:
    3
    Views:
    500
    LILkillaBEE aka Nikola
    Jun 24, 2005
  4. Peter Michael

    defined(&func) and Exporter

    Peter Michael, Dec 30, 2003, in forum: Perl Misc
    Replies:
    4
    Views:
    145
    Brian McCauley
    Jan 2, 2004
  5. Amanda

    Help: exporter is "internal" class

    Amanda, Apr 9, 2004, in forum: Perl Misc
    Replies:
    0
    Views:
    103
    Amanda
    Apr 9, 2004
Loading...

Share This Page