Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > More controlled Module loading - Faking output from caller

Reply
Thread Tools

More controlled Module loading - Faking output from caller

 
 
Stumo
Guest
Posts: n/a
 
      04-04-2007
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:ackage 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

 
Reply With Quote
 
 
 
 
Robert 'phaylon' Sedlacek
Guest
Posts: n/a
 
      04-04-2007
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:ackage 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
 
Reply With Quote
 
 
 
 
anno4000@radom.zrz.tu-berlin.de
Guest
Posts: n/a
 
      04-04-2007
Stumo <(E-Mail Removed)> 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:ackage 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:ackage->can( 'import') ||
die "'File:ackage" can't import"

Anno
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how can a module tell if its caller is using some other module russ.jones2@boeing.com Perl Misc 4 02-15-2006 10:45 PM
Faking Network Admission Control Agents - Is possible? orazon@gmail.com Cisco 0 12-13-2004 09:23 PM
Re: Faking HttpContext for unit testing =?Utf-8?B?S2lrbw==?= ASP .Net 0 06-18-2004 05:49 AM
Can't get function caller if the caller is from a function within a popup window Mark Javascript 2 04-03-2004 07:57 AM
"faking" an HTTPContext when running unit tests ASP .Net 3 02-05-2004 12:58 PM



Advertisments