0

I am trying to execute a simple strawberry perl script for file copy to get the code from the last executed command. But I get 0/success in all the case.

Code from my test.pl script

use File::Copy;  
use strict;
use warnings;

my $source_file = "D:\\abc\\def\\in\\test\\test1.csv";
my $target_file = "D:\\abc\\def\\in\\test\\test2.csv";

if ( copy( $source_file, $target_file ) == 0 ) {
    print "success";
} 
else { print "fail"; }

Since the path I used D:\\abc\\def\\in\\test\\test1.csv does not exist on the machine so I expect to get fail but I get success no matter what I provide.

Following the execution and output:

D:\pet\common\bin\backup>perl test.pl
success
zdim
  • 64,580
  • 5
  • 52
  • 81

1 Answers1

2

If you look at perldoc File::Copy you will see the following:

RETURN
    All functions return 1 on success, 0 on failure. $! will be set if an
    error was encountered.

Therefore, your code should be exposing what is in $! if there is an error:

if ( copy($source_file, $target_file)) {
    print "success\n";
} 
else { 
    warn "fail: $!\n";
}

Also, as described in the documentation for File::Copy, copy returns 1 on success (a true value), so I removed your == 0 in the success test. With Perl, any true value in the if(COND){...} statement will do; you don't need to explicitly test for 1.

Regarding paths: The / character can be used as a path delimiter, even if you're using Windows except in some cases where you might be sending the path to an external program. This capability allows you to relatively portably write code that expresses paths as foo/bar/baz, and it will work with Windows similarly to how it would work under a *nix operating system. Using the forward slash as a delimiter allows you to avoid escaping every backslash in a path: foo\\bar\\baz.

DavidO
  • 13,812
  • 3
  • 38
  • 66
  • Most external programs will accept `/` as well because `/` is actually fully supported by Windows itself. (Perl doesn't translate `/` to `\ `; it simply passes paths with `/` to the OS.) The problems usually come from `/` being treated as the start of a command line switch, an issue that can usually be circumvented through the use of quotes. (`dir c:/temp` bad; `dir "c:/temp"` ok) – ikegami Oct 16 '19 at 03:21
  • Correct: This is documented in `perldoc perlport` at https://perldoc.perl.org/perlport.html#DOS-and-Derivatives , though your example is good, and lacking in the documentation. – DavidO Oct 16 '19 at 04:57
  • @DavidO Actually I have another script where I am trying to execute Informatica workflow thru pmcmd command using strawberry perl but I am not able to capture the failure of the workflow. I am using if (qx($pmcmd_command)) where $pmcmd_command has the actual command to execute. – Priti Verma Oct 16 '19 at 17:54
  • `qx//` returns STDOUT, but STDERR gets lost unless you redirect it somewhere, which I don't know how to do in Windows. If I were you I would use the Capture::Tiny module, which makes handling STDOUT, STDERR, and the exit code easy, and portable. – DavidO Oct 16 '19 at 22:27