More controlled Module loading - Faking output from caller

Discussion in 'Perl Misc' started by Stumo, Apr 4, 2007.

  1. Stumo

    Stumo Guest

    Hi

    I'm trying to write my own function that works like use but outputs a
    more user friendly error message if the module isn't present.

    My current tactic is to have my function in it's own package, and it
    uses File::package to do the loading - however this doesn't import
    things into the correct namespace (neither does calling import()
    directly for some packages that have redefined it).

    Is there any way I can change what caller() returns, so my function
    does not appear and everything behaves as if it was called from the
    namespace that called my function?

    Alternatively, are there any other solutions?

    Stuart Moore
    Stumo, Apr 4, 2007
    #1
    1. Advertising

  2. Stumo wrote:
    > I'm trying to write my own function that works like use but outputs a
    > more user friendly error message if the module isn't present.


    'use' is a construct that is parsed by perl as soon as it's seen. IIRC
    you can't make your own 'use' functions at the moment. You'd need to
    have to call your function in a BEGIN { } block. Otherwise you will run
    into problems with imported symbols that aren't available at compile time.

    I would propose making it a regular module with an import() method. For
    example, like this syntax:

    use Module::OrDieWithNiceMessage 'Module::To::Load', \@imports,
    { message => q(The module 'Module::To::Load couldn't be found) };

    > My current tactic is to have my function in it's own package, and it
    > uses File::package to do the loading - however this doesn't import
    > things into the correct namespace (neither does calling import()
    > directly for some packages that have redefined it).


    Personally I prefer Class::Inspector to do such things:

    use Class::Inspector;
    my $module = 'Foo::Bar';
    eval { require Class::Inspector->filename($module) };
    if ($@) {
    # handle module loading error
    }

    > Is there any way I can change what caller() returns, so my function
    > does not appear and everything behaves as if it was called from the
    > namespace that called my function?


    You could either use $module->can('import') to get at the code reference
    for the original import() method and use goto to "replace" the current
    subroutine with the imported one (although I don't know how you'd have
    to work with @_) or use something like Sub::Uplevel (see CPAN).

    > Alternatively, are there any other solutions?


    See the above for a few solutions. I personally never needed such a
    thing, but I'm wondering if there's nothing on CPAN yet to do this kind
    of stuff.

    ..phaylon
    Robert 'phaylon' Sedlacek, Apr 4, 2007
    #2
    1. Advertising

  3. Stumo

    -berlin.de Guest

    Stumo <> wrote in comp.lang.perl.misc:
    > Hi
    >
    > I'm trying to write my own function that works like use but outputs a
    > more user friendly error message if the module isn't present.
    >
    > My current tactic is to have my function in it's own package, and it
    > uses File::package to do the loading - however this doesn't import
    > things into the correct namespace (neither does calling import()
    > directly for some packages that have redefined it).
    >
    > Is there any way I can change what caller() returns, so my function
    > does not appear and everything behaves as if it was called from the
    > namespace that called my function?
    >
    > Alternatively, are there any other solutions?


    Don't call import, goto it. That preserves the calling package so
    ->import won't be confused. Make sure, @_ is what you need it to be,
    and then (untested)

    goto File::package->can( 'import') ||
    die "'File::package" can't import"

    Anno
    -berlin.de, Apr 4, 2007
    #3
    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. Guest
    Replies:
    3
    Views:
    753
    Guest
    Feb 5, 2004
  2. =?Utf-8?B?S2lrbw==?=

    Re: Faking HttpContext for unit testing

    =?Utf-8?B?S2lrbw==?=, Jun 18, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    423
    =?Utf-8?B?S2lrbw==?=
    Jun 18, 2004
  3. Replies:
    3
    Views:
    464
    Juan T. Llibre
    Jul 15, 2005
  4. Mark
    Replies:
    2
    Views:
    386
  5. Replies:
    4
    Views:
    96
    John W. Krahn
    Feb 15, 2006
Loading...

Share This Page