in reply to Re: Export again
in thread Export again

This way appeared the simplest, and works, tho I'm not 100% sure on what...

eval "package $dest_pkg; Time::HiRes->import('gettimeofday');";

Is actually doing? I've not seen an eval block being a simple string before?

Replies are listed 'Best First'.
Re^3: Export again
by Eliya (Vicar) on Feb 22, 2011 at 09:56 UTC

    eval has two forms, eval EXPR (aka "string eval") and eval BLOCK.

    In this case, a string eval is needed because $dest_pkg isn't known before runtime of the import function.  And this is also the reason the eval is required at all.  In other words, the package statement is a compile-time thing, i.e. you can't say

    package $dest_pkg;

    So what the eval does is shift the compile-time of that particular code fragment to runtime.  This means that when you say use MyStandardModules; from within a package "Foo", $dest_pkg will be "Foo", and the following snippet will be compiled (and executed):

    package Foo; Time::HiRes->import('gettimeofday');

    which has the effect that when Exporter then determines the calling package in its import method (via caller), it will see package "Foo", which is what it then exports the requested routines or variables to.

      Got it! Cheers, makes perfect sense now.