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

The following test script (if I understand it correctly) is taken from the Llama book and fails to execute properly:
#!/usr/bin/perl -w use strict; use File::Spec; print "Please enter a filename: "; chomp(my $old_name = <STDIN>); my $dirname = dirname $old_name; my $basename = basename $old_name; $basename =~ s/^/not/; my $new_name = File::Spec->catfile($dirname, $basename); rename ($old_name, $new_name) or warn "Can't rename '$old_name' to '$new_name': $!";
The outcome from attempting this script is:
Please enter a filename: testX Can't locate object method "dirname" via package "testX" (perhaps you +forgot to load "testX"?) at /home/cgmd/bin/learning_perl/test7.pl lin +e 9, <STDIN> line 1.
What error have I made in structuring this code? Thanks!

Replies are listed 'Best First'.
Re: Script using File::Spec fails...
by Joost (Canon) on May 26, 2007 at 02:53 UTC
      Yes, that change (adding "use File::Basename") corrects the error. I'm simply not fully understanding the distinction between implementing File::Basename and File::Spec, as presented by the text. Thanks!
        Yes, that change (adding "use File::Basename") corrects the error. I'm simply not fully understanding the distinction between implementing File::Basename and File::Spec, as presented by the text. Thanks!

        They're just two modules with different albeit somewhat related functionality. What do you mean with "implementing" anyway? BTW: you have a special and convenient shortcut to make the name of a module into a clickable link, if you like. Just write [mod://File::Basename] and it will render like File::Basename.