in reply to Re^3: Script using File::Spec fails...
in thread Script using File::Spec fails...

Perhaps my wording has been too vague. The "Llama book" (in Section 15.3) provides an example to demonstrate usage of File::Basename:
#!/usr/bin/perl -w use strict; use File::Basename; 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 = "$dirname/$basename"; rename ($old_name, $new_name) or warn "Can't rename '$old_name' to '$new_name': $!";
Then a 2nd example, to show additional functionality provided by File::Spec:
#!/usr/bin/perl -w use strict; use File::Basename; 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': $!";
My question is... What additional functionality has the second script provided? Both scripts run effectively on both unix and windows systems. I'm missunderstanding the advantage that File::Spec should be adding. Also, thanks for the tip about turning module names into clickable links!

Replies are listed 'Best First'.
Re^5: Script using File::Spec fails...
by syphilis (Archbishop) on May 26, 2007 at 16:03 UTC
    What additional functionality has the second script provided?

    With the second script you don't have to know what the path separator is. That is, the main thing that File::Spec provides is portability. You can use File::Spec->catfile(); and it will work on whatever platform the script is run on.

    A more useful example for comparison between unix and windows would be the return value of File::Spec->devnull().

    Check out the File::Spec documentation (for which you provided a link :-) for more details.

    Cheers,
    Rob

      I think the point is that both scripts run effectively on both unix and windows. That means that the explicit forward slash works as a path separator on both platforms. So I think cgmd's question is valid, and not yet satisfactorily answered.

      That said, File::Spec is really about portability. It's supposed to free you from caring about such things as path separators on many platforms, such as Mac and VMS.

      A word spoken in Mind will reach its own level, in the objective world, by its own weight
Re^5: Script using File::Spec fails...
by blazar (Canon) on May 26, 2007 at 19:01 UTC
    Both scripts run effectively on both unix and windows systems.

    Others answered your question, but the answer is also in your own words, the point being that *NIX and Windows do not exhaust the whole world. Granted: forward slashes work mostly everywhere, but if you're really concerned about portability, go the File::Spec (or File::Spec::Functions) route.