in reply to What happens when you load the same module twice?

use is really the following code:
use My::Module @args; # This becomes ... BEGIN { require My::Module; My::Module->import( @args ); }

require is approximately the following code:

require My::Module; # This becomes ... sub require { my ($name) = @_; (my $filename = $name) =~ s!::!/!g; $filename .= '.pm' if $name =~ /::/; unless ( exists $INC{ $filename } ) { # Load the file into memory, figuring out which of @INC to use +. # Put the full pathname into $full_path_name $INC{ $filename } = $full_path_name; } }

So, if a module has already been loaded, it won't be loaded again, but its import() will be called again (as tye noted in the original thread).

Your confusion is arising because you're attempting to load File::BaseName and File::Basename on case-insensitive operating system. Because it's case-insensitive, the check against %INC is subverted. On case-insensitive operating systems, the following version of require may be more appropriate:

sub require { my ($name) = @_; ########## THIS IS ADDED $name = lc $name; ########## TO HERE (my $filename = $name) =~ s!::!/!g; $filename .= '.pm' if $name =~ /::/; unless ( exists $INC{ $filename } ) { # Load the file into memory, figuring out which of @INC to use +. # Put the full pathname into $full_path_name $INC{ $filename } = $full_path_name; } }

That should appropriately fix things. (But, it won't catch things like use Strict; - that lc fix would have to be part of use, which may not be a bad thing.)

Being right, does not endow the right to be rude; politeness costs nothing.
Being unknowing, is not the same as being stupid.
Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Replies are listed 'Best First'.
Re^2: What happens when you load the same module twice?
by Anonymous Monk on Feb 10, 2005 at 15:12 UTC
    That should appropriately fix things.
    Will it? Sure, you don't get warnings if you do:
    use File::Basename; use File::BaseName;
    as the second use will not do anything. But if you remove the first one, or if you swap the two statements, File::Basename->import() won't be called, as Perl will try to call File::BaseName->import(), which doesn't exists so Perl will silently go on.

    This will lead to mysterious behaviour, that may be hard to debug.

      File::Basename->import() won't be called, as Perl will try to call File::BaseName->import(), which doesn't exists so Perl will silently go on

      You're describing the current behavior. His change, whatever other implications it may have, would not affect that.

      Update: to clarify my statement for Anonymous Monk, I meant that adding the lc $name doesn't change whether use File::BaseName would call the right import routine, or not.

        His suggested change is to do a lowercase of the name, before doing the require. Which means that 'use File::Basename' and 'use File::BaseName' both reduce to requiring 'file::basename'. This means that duplicate checking in %INC gets captured.

        His change however didn't involve anything with changing the package name. He didn't suggest that 'use My::Module' should be equivalent to:

        BEGIN { require My::Module; # require does an lc of the name. (lc "My::Module") -> import; }
        Not that this would work. Because "file::basename::import" doesn't exist. Or are you suggesting that in dragonchilds "fix" package names are automatically lowercased as well? And that Perl's rule of "variable names are case sensitive" is to be changed to "lexical variable names are case senstive, but package variables are only case sensitive after the list colon"? I certainly didn't read that in his post.
Re^2: What happens when you load the same module twice?
by eric256 (Parson) on Feb 10, 2005 at 15:29 UTC

    I thought his question was why do the warnings go away when you add () to one of the use statments. I'm not sure how your answer answers that.


    ___________
    Eric Hodges
      Because calling 'use File::Basename();' tells the compiler to import an empty list -- in other words, do not import anything. The issue at hand is that File::Basename and File::Basename are both trying to import subroutines into the main:: namespace by default.

        So the import is the part that is creating redifined errors not the processing of the module file?


        ___________
        Eric Hodges
        No. It has nothing at all to do with importing names twice. Not convinced? Run this code:
        #!/usr/bin/perl use strict; use warnings; use File::Basename (); # Not calling import! BEGIN {delete $INC{"File/Basename.pm"}}; use File::Basename (); # Not calling import! __END__ Subroutine fileparse_set_fstype redefined at /usr/lib/perl5/lib/5.8.6/ +File/Basename.pm line 157. Subroutine fileparse redefined at /usr/lib/perl5/lib/5.8.6/File/Basena +me.pm line 171. Subroutine basename redefined at /usr/lib/perl5/lib/5.8.6/File/Basenam +e.pm line 238. Subroutine dirname redefined at /usr/lib/perl5/lib/5.8.6/File/Basename +.pm line 251.
        Can we now please stop this myth that redefining subroutines has anything to do with import?