I have recently encountered an interesting gotcha from the use/require functions. I had just added a library function to my general use library when I suddenly started getting a redefined subroutine warning for File::Basename (see below) from a program that had been running warning free until that moment. Both the main program and the new library function specify use File::Basename. It was always my understanding that use and require would not try to load a module a second time if it was already listed in %INC. A likely red herring here is that the main program does not actually import, call or otherwise reference the new library routine in the library module.

The key to the problem is (after a few hours of head scratching) that each routine calls File::Basename slightly differently which defines two seperate entries in %INC yet still loads the same module.

This all happens on a WinXP machine with Activestate 5.6.1. I haven't tried it on my Linux box yet. I'm guessing that this is a DOS/Win32 issue in that file names are not case sensative in that environment. Not groundbreaking, maybe, but an interesting gotcha none-the-less.
# stripped down test code #!/usr/local/ActivePerl-5.6/bin/perl use strict; use warnings; use diagnostics; use File::BaseName; printf("%-45s%-s\n",$_,$INC{$_}) foreach (sort keys %INC); package prob_func; sub secondfunc{ use File::Basename; return; } __OUTPUT__ #(relevent parts only) Subroutine fileparse_set_fstype redefined at C:/Program Files/Perl/lib/File/Basename.pm line 152 (#1) (W redefine) You redefined a subroutine. To suppress this warning +, say { no warnings; eval "sub name { ... }"; } Subroutine fileparse redefined at C:/Program Files/Perl/lib/File/Basen +ame.pm line 166 (#1) Subroutine basename redefined at C:/Program Files/Perl/lib/File/Basena +me.pm line 222 (#1) Subroutine dirname redefined at C:/Program Files/Perl/lib/File/Basenam +e.pm line 235 (#1) File/BaseName.pm C:/Program Files/Perl/lib +/File/BaseName.pm File/Basename.pm C:/Program Files/Perl/lib +/File/Basename.pm

Update:

I forget to include the package statement before the sub prob_func definition. The warning occurs either way but my original problem occurred in seperate namespaces

Update:

Thanks for the great comments. The discussion did raise one or two other questions that I thought better moved to SOPW. Thanks again :o)

PJ
use strict; use warnings; use diagnostics;

In reply to An Interesting Gotcha With use/require by periapt

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.