On 5/21/06, James E Keenan <[EMAIL PROTECTED]> wrote:

This code is intended to achieve that goal but doesn't DWIM:


This is the right idea, but...

my ($file, $workdir, $destdir);
{
     $workdir = File::Temp::tempdir();
     chdir $workdir or die "Cannot change to $workdir";
     $file = system("touch alpha");
     $destdir = "$workdir/other";
     chmod 0330, ($destdir)
         or die "Cannot change permissions on directory";


You forgot to make $destdir.

    # dies on preceding line
     rename $file, $destdir/$file or die "Cannot move $file";
}


There's other issues with this code.  For one, its not portable.  You're
using a system command (touch) and you're assuming Unix filepath syntax.
Another is your errors do not include $! which is the reason the command
failed (no such file or directory).

Additionally, 0333 is writeable and executable.  You want 0500, executable
and readable (not writable) only by this user.

Finally, as I mentioned in another post on this thread, stay out of the
system temp directory.  Stick to t/.

use strict;
use File::Spec;
use File::Temp qw(tempdir tempfile);

$File::Temp::DEBUG = 1;

# Or use Shell::Command
sub touch { open my $fh, ">$_[0]" or die "Can't touch $_[0]: $!"; }

# CLEANUP tells File::Temp to delete the directory after the program exits.
my $testdir = tempdir( DIR => 't', CLEANUP => 1 );
chdir $testdir or die "Can't chdir into $testdir: $!";

touch "testfile" or die "Can't touch testfile: $!";

mkdir "destdir" or die "Can't mkdir destdir: $!";
chmod 0500, "destdir" or die "Can't chmod destdir: $!";

# It should die here.
rename "testfile", File::Spec->catfile("destdir", "testfile") or die "Can't
rename: $!";

Reply via email to