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

Hi,

I have been trying to get the code below working for ages - but it just won't play!

$tmpfile = tmpFileName($clientattachment); @filespec = split(/\\/,$clientattachment); $filname = @filespec[$#filespec]; $att_file = $current_thread_id.$filename; $att_file =~ s/ /_/g; $att_file =~ s/\//_/g; system("cp $tmpfile $copypath/$att_file");

If I try die($tmpfile) after, it has no value. Is that correct?

If I try $tmpfile = tmpFileName($clientattachment) || die("Failed");, it always dies.

Anyway, under all circumstances which I have tried, when I go to look for the file in the location specifed (linux box), it is not there.

Cany anyone shed some light?

Replies are listed 'Best First'.
Re: File::Copy - not working!
by Zaxo (Archbishop) on Jun 19, 2003 at 10:45 UTC

    Apparantly tmpFileName() is broken. That would be the code to post. Be careful with this, there is a race if two of these get the same $tmpfile.

    You're not really using File::Copy. Why not mv instead of cp? That would save cleanup.

    After Compline,
    Zaxo

Re: File::Copy - not working!
by TomDLux (Vicar) on Jun 19, 2003 at 11:12 UTC

    Why not use File::Basename instead of split?

    $att_file won't have a slash, and if $current_thread_id is $$, it won't contain a slash either, so there's no need for s/\//_/g/. But for those days when you do need somethign like that, take advantage of the fact that you can use any character as the delimiters: s|/|_|g.

    What's the code in tmpFileName()?

    --
    TTTATCGGTCGTTATATAGATGTTTGCA

Re: File::Copy - not working!
by helgi (Hermit) on Jun 19, 2003 at 12:57 UTC
    1 - As the others have said, you are not using File::Copy at all, so your subject heading is wrong.

    2 - No warnings or strictures.

    3 - As the others have said, you should use File::Basename to get the file basename, not this kludgery.

    4 - System commands return errors, but you're not even looking at it. Errors from daughter processes are in the variable $?. Use it.
    system("cp $tmpfile $copypath/$att_file") == 0 or die "Cannot copy $tmpfile to $copypath:$?\n";

    5 - How can we know if $tmpfile has a value? You don't show us the tmpFileName subroutine code nor what is contained in the $clientattachment variable.

    6- If I try $tmpfile = tmpFileName($clientattachment) || die("Failed");, it always dies. .
    It does? Do you even have a tmpFileName sub?


    --
    Regards,
    Helgi Briem
    helgi DOT briem AT decode DOT is

      What syntax would I use if I were to use File::Basename?

      I presumed that tmpFileName() was a function in File::Copy. Was obviously mistaken.

      Thanks for the advice and comments.

        Since your incoming path seems to have Windows type backslashes as delimiters, I am using that below.

        However, you should always use forward slashe, even under Windows, and get rid of them as soon as possible with something like: $path =~ s.\\.\/.g;

        use warnings; use strict; use File::Basename; my $path_to_file = 'bla\bla\full\path\to\file.ext'; $path_to_file =~ s.\\.\/.g; my ($file,$path) = fileparse($path_to_file); print "$file is in $path\n";

        For copying files using File::Copy, you do:

        use File::Copy; copy $file,$newpath or die "Cannot copy $file to $newpath:$!\n";

        --
        Regards,
        Helgi Briem
        helgi DOT briem AT decode DOT is
Re: File::Copy - not working!
by vek (Prior) on Jun 19, 2003 at 12:59 UTC

    I agree that you should be looking at what tmpFileName is doing. I did notice a couple of other things though. First of all you appear to have a typo:

    $filname = @filespec[$#filespec];
    Should be:
    $filename = @filespec[$#filespec];
    Secondly, always, always, always check the return value from system calls (0 on success, 1 on failure):
    system("cp $tmpfile $copypath/$att_file") && die "Could not cp $tmpfile to $copypath/$att_file - $!\n";
    -- vek --