in reply to Re: "use Foo.pm" twice from inside different packages...
in thread "use Foo.pm" twice from inside different packages...

Just a minor note on the difference in our solutions with respect to Exporter: Perl 5.6 and earlier require Exporter to be subclassed, not just imported.

use Exporter qw(import); # Perl >= 5.8 use base 'Exporter'; # Perl <= 5.6

Even my solution isn't backwards-compatible prior to Perl 5.6 because I included our and warnings. For personal use, this may not matter, but module authors who plan to publish should keep it in mind.

Update: For Perl >=5.8 I changed the example to note that you need to specify import.

-xdg

Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Replies are listed 'Best First'.
Re^3: "use Foo.pm" twice from inside different packages...
by japhy (Canon) on Mar 02, 2006 at 22:15 UTC
    D'oh, right, oops. (Editor's note: I said those words out loud as I typed them.)

    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
Re^3: "use Foo.pm" twice from inside different packages...
by ikegami (Patriarch) on Mar 02, 2006 at 23:12 UTC
    uh, our and use warnings work great in 5.6. I missed "prior to"
Re^3: "use Foo.pm" twice from inside different packages...
by wazoox (Prior) on Mar 03, 2006 at 12:04 UTC
    Is there any reason to prefer "use Exporter qw( import)" instead of the backward-compatible "use base 'Exporter'" when running perl 5.8 anyway ?

    updated:we should use Exporter qw( import ) to get it to work :)

      I don't think there's much of a practical difference. It's shorter and it doesn't add anything to the @ISA array, meaning that there's a shorter inheritance tree for method resolution if you're doing a lot of multiple inheritance.

      I think the original subclassing approach was just a bad design choice, but now we're stuck with it if we want to be backwards compatible. But since that isn't an issue for some people (e.g. people writing specifically for desireable features of Perl 5.8, like Unicode), they have the option of moving to the better design.

      -xdg

      Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

        Arg. I just tried with my real code running perl 5.8.8, and it does actually a difference : use base 'Exporter' works, use Exporter doesn't (??!?!?). The module is absolutely trivial :
        package DBtools; use strict; use warnings; use DBI; use Exporter; our @EXPORT=qw( dbconnect ); #sub definition sub dbconnect { my $config = shift; my $dbh = DBI->connect( 'DBI:' . $config->param('dbdriver') . ':dbname=' . $config->param('dbname') . ';host=' . $config->param('dbhost'), $config->param('dbuser'), $config->param('dbpassword'), { PrintError => 0, RaiseError => 1, AutoCommit => 0 } ); return ($dbh); } 1;
        I'm going to stick with "use base" for now :/