in reply to Are dynamic 'use' statements possible?

use Foo::Bar qw(foo bar baz);
is equal to
BEGIN { require 'Foo/Bar.pm'; Foo::Bar->import(qw(foo bar baz)); }
This is documented in perlfunc, item use.

Another example:

BEGIN { my ($module, @imports) = qw(Foo::Bar foo bar baz); (my $file = "$module.pm") =~ s[::][/]g; require $file; $module->import(@imports); }
Update - fixed typo. Thank you, Matts.

- Yes, I reinvent wheels.
- Spam: Visit eurotraQ.

Replies are listed 'Best First'.
Re: Re: Are dynamic 'use' statements possible?
by Matts (Deacon) on Jul 01, 2002 at 20:43 UTC
    Typo:

    (my $file = "$module.pm") =~ s[::][/]g;

    Note the "=~", not "=". The worst thing about this is it will compile it just fine with it as an equals sign {1}, and simply do the s/// on the $_ variable, and then assign it's return value to $file. Not fun trying to locate that sort of bug.

    {1} Yes, even with use strict and warnings on.

Re: Re: Are dynamic 'use' statements possible?
by tosh (Scribe) on Jul 02, 2002 at 21:54 UTC
    But wouldn't the problem with this be that $module is only a STRING which would prevent the import() call, no?

    I think Aristotle has the solution below...