Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Windows pitfall: Subroutine redefined with use Modul

by Brutha (Friar)
on Oct 29, 2003 at 09:54 UTC ( #302959=perlquestion: print w/replies, xml ) Need Help??

Brutha has asked for the wisdom of the Perl Monks concerning the following question:

Hello fellows,

this is not a question, but a warning about an issue, which cost me a day. This is a Windows specific problem. Is that hint enough to solve the following?

After boiling down the needle-in-haystack problem and dropping all but the necessary lines, I have the following three files:

File Baseclass.pm

package Baseclass; use strict; use subclass1; print "load Baseclass\n"; sub new { my $class = shift; return bless {},$class; } 1;
File Subclass1.pm

package Subclass1; use strict; use warnings; use base( "Baseclass" ); sub new { my $class = shift; return $self = $class->SUPER::new(@_); } print "load Subclass1\n"; 1;
File Bla.pl

use strict; use warnings; use Subclass1; my $obj = Subclass1->new; 1;
Now run perl bla.pl and you get the following output:
Load Subclass1 load Baseclass Subroutine new redefined at Subclass1.pm line 11. Load Subclass1
Now, here comes the solution.

It is simple but not really obvious in the sense of visible. It is the letter 's' or should I say 'S'?
In file Baseclass.pm line 3 uses 'subclass1' instead of 'Subclass1' with capital letter. Perl sees two modules use Subclass1; and use subclass1;. Windows does not care about case and gives Perl the file Subclass1.pm both times.

Hope this helps somebody, save your day for better things.

And it came to pass that in time the Great God Om spake unto Brutha, the Chosen One: "Psst!"
(Terry Pratchett, Small Gods)

Replies are listed 'Best First'.
Re: Windows pitfall: Subroutine redefined with use Modul
by dada (Chaplain) on Oct 29, 2003 at 11:30 UTC
    yes, this is a nasty one indeed. as we all know, Perl is case sensitive, but the Windows filesystem not. what happens here is that Perl stores information about the modules it has loaded in its %INC hash, and it ends up having two entries for Subclass1. if you add these lines at the end of the bla.pl script:
    use Data::Dumper; print Dumper(\%INC);
    you'll see something like:
    $VAR1 = { [...] 'subclass1.pm' => 'subclass1.pm', [...] 'Subclass1.pm' => 'Subclass1.pm', [...] };
    so, the file is one but Perl sees (and loads) two modules. this could also pose serious problems with imported symbols, because when the case of the package name doesn't match, Perl loads the .pm file but doesn't export anything, as in:
    use fIlE::SpEc qw(catfile); # note the wrong case $x = File::Spec->catfile('a', 'b', 'c'); # this works $x = catfile('a', 'b', 'c'); # this croaks
    this is even more unobvious, because the error goes unnoticed until you try to use something that's exported from the module.

    unfortunately, Perl can't possibly catch this sort of errors, so you have to watch your case. one possible solution (for the typo-prone individuals :-) could be to always cut & paste the use line from the SYNOPSIS found in the module documentation.

    cheers,
    Aldo

    King of Laziness, Wizard of Impatience, Lord of Hubris

Re: Windows pitfall: Subroutine redefined with use Modul
by bart (Canon) on Oct 29, 2003 at 11:14 UTC
      Thanks for that great hint. tye's patch (Universally unimportant and overused) looks like a must for W32 Perl

      My node was really intended as a reminder. The examples are simple to analyze, but a real life project with dozens of files does not shout "Hello, it's Windows!". When you have forks, file permissions, shell escapes etc you immediatly grab your printout of perlport, but not if you search for coding, cut and paste etc mistakes.

      And it came to pass that in time the Great God Om spake unto Brutha, the Chosen One: "Psst!"
      (Terry Pratchett, Small Gods)

Re: Windows pitfall: Subroutine redefined with use Modul
by Aragorn (Curate) on Oct 29, 2003 at 11:04 UTC
    This is described in perlport: Don't use filenames which are different only in case.

    Arjen

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://302959]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2022-05-16 15:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (63 votes). Check out past polls.

    Notices?