in reply to Re^2: Is there a -e test for directories, like there is for files?
in thread Is there a -e test for directories, like there is for files?

chomp(my $filename = <STDIN>); my $dir = getcwd; my $backupdir = 'backup_files'; my $back = $filename . ".bak"; my $backful = $dir . "/" . $backupdir . "/" . $back;

May I suggest slightly cleaner alternatives?

use File::Basename qw( basename ); use File::Spec::Functions qw( catfile ); chomp(my $filenameful = <STDIN>); my $filename = basename($filenameful); my $backupdir = 'backup_files'; my $back = $filename . ".bak"; my $backful = catfile($backupdir, $back);
use File::Basename qw( basename ); use File::Spec::Functions qw( catfile rel2abs ); chomp(my $filenameful = <STDIN>); $filenameful = rel2abs($filenameful); my $filename = basename($filenameful); my $backupdir = 'backup_files'; my $back = $filename . ".bak"; my $backful = rel2abs(catfile($backupdir, $back));

They both allow a path to be attached to the input.
They both use the right directory seperator for the platform being used.
They both cannonize the path, so foo//bar would become foo/bar.
The first creates a relative path while the second creates an absolute one.

Replies are listed 'Best First'.
Re^4: Is there a -e test for directories, like there is for files?
by mdunnbass (Monk) on Feb 08, 2007 at 21:06 UTC
    Voondibah!

    Thank you!