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

Consider the following program:

#!/usr/bin/perl -W use Cwd; print "You're in '", getcwd(), "'\n";

I just moved up to the latest Redhat distribution, and perl is now generating the following error when I run this program:

Subroutine Cwd::fastcwd redefined at /usr/lib/perl5/5.8.5/i386-linux-thread-multi/XSLoader.pm line 92.

What happened? This is a virgin Redhat distribution, so I wouldn't expect this kind of error out of the box. Where's the problem? In Cwd? Or in XSLoader.pm?

Perl never used to generate this error. And, as you might expect, if I change -W to -w, the error goes away. How can I continue to use -W in my script and still keep perl quiet about this redefined routine. It does NOT go away if I merely precede the "use Cwd" with "no warnings 'redefine'" (though it seems it should). So, the question is, where is the real error, and what should I be doing?

PS: I did a Google search on this error message, and everyone else that's reported the same error seems to be on a windows box, and it was deemed the version of perl they were using was the culprit.

Replies are listed 'Best First'.
Re: perl reports fastcwd 'redefined' when using Cwd and -W
by ikegami (Patriarch) on Jan 20, 2007 at 03:55 UTC

    Cwd has various implementation of the same function for different OSs. It redefines some functions *intentionally* in order to optimize operations on OSs where certain paradigms are supported.

    It attempts to disable warnings if they are enabled, but -W (as opposed to -w) forces the warnings to remain on.

    From Cwd:

    local $^W = 0; # assignments trigger 'subroutine redefined' warning *{$name} = \&{$map->{$name}};
Re: perl reports fastcwd 'redefined' when using Cwd and -W
by Anonymous Monk on Jan 20, 2007 at 00:44 UTC
    Why do you have -W?
    -W   Enables all warnings regardless of "no warnings" or $^W. See
         perllexwarn.
    
      > Why do you have -W?

      Because this was an old script, written at a time when I apparently swapped my interpretation of -w and -W. I have since used -w in all my scripts, but forgot to see it in this case. thanks for pointing it out. In any event, it does seem that upgrading perl is the thing to do. I'm surprised that the "latest" version of Redhat doesn't have the most up-to-date version.

Re: perl reports fastcwd 'redefined' when using Cwd and -W
by almut (Canon) on Jan 19, 2007 at 21:14 UTC

    On Linux, it also seems to depend on the Perl version. Just tried it: with v5.8.4, I can reproduce the error, but not with v5.8.8.

    So, if upgrading is an option, that's probably the easiest route...

    Update: no warnings 'redefine', as mentioned below, doesn't seem to help - i.e. I do get the same error with v5.8.4

      I ran it on FC6. It worked fine. You might try this:

      #!/usr/bin/perl use Cwd; use strict; use warnings; no warnings 'redefine'; my $cwd = getcwd(); print "You're in '", getcwd(), "'\n";

        That won't work for two reasons.

        • no warnings 'redefine'; won't affect Cwd, just the block in which it appears. It's lexically scoped.
        • The -W used by the OP overrides no warnings.