in reply to Is this safe to export and import functions this way?

It won't work the first time you do it because My::Package hasn't been loaded at the time that you try to get the list of keys in order to pass in to the 'use' line. The second time you use it, it might work, but it will still be a really bad idea.

use My::Package '/./';

See 'Advanced features' in Exporter.

- tye        

Replies are listed 'Best First'.
Re^2: Is this safe to export and import functions this way? (order)
by tye (Sage) on Aug 19, 2015 at 20:13 UTC

    Ah, the trick is that the 'use' line becomes:

    BEGIN { require My::Package; My::Package->import( keys %My::Package:: ); }

    so the 'keys' expression is evaluated after the (generated) 'require My::Package;' is run.

    (And I had a typo in my test case.)

    - tye        

Re^2: Is this safe to export and import functions this way? (:all)
by Laurent_R (Canon) on Aug 19, 2015 at 19:59 UTC
    Surprisingly, it works at first launch (I used a simplified version to avoid having to create a file to be read):
    $ perl -e ' use strict; use warnings; use My::Package (keys %My::Package::); # test("hello world\n"); test_2("hello this is dog"); ' hello world hello this is dog
    But I am of course not saying it is a good idea to do that.