Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Do and Die

by jimbus (Friar)
on Aug 15, 2005 at 18:42 UTC ( #483941=perlquestion: print w/replies, xml ) Need Help??
jimbus has asked for the wisdom of the Perl Monks concerning the following question:


As you grow to know me, you'll come to know that most of the issues I have are typoes or stupid newbie tricks, but I can't see this one. yet.

Why is the following code tripping the or condition if it it successfully moving the file? I've printed out the string and the variables are expanding coreectly, cutting an pasting the output to the command line works without an error... I'm stumped.

system("mv $filename $localpath/archives/") or Die ("data load","YTS +MSC","High","Could not move file: $filename for successful exi t: $@");

"Die", BTW is from a package I've created that writes to a log file and DB table before exiting.

Never moon a werewolf!

Replies are listed 'Best First'.
Re: Do and Die
by japhy (Canon) on Aug 15, 2005 at 18:54 UTC
    The system() function returns zero on success and non-zero on failure (see the perlfunc documentation).

    And there's no reason to make a system call to 'mv' when you can use the rename() function or the move() function from File::Copy.

    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
Re: Do and Die
by jhourcle (Prior) on Aug 15, 2005 at 18:56 UTC

    system returns the status of the command returned by wait. Typical unix commands and C functions return '0' on success (ie, there was no error)

    See the following example:

    $ perl -e '$var = system("true"); print "[$var]\n"' [0] $ perl -e '$var = system("false"); print "[$var]\n"' [256]

    Update: oops... shift 8 places to get the status of the command. 'false' should have returned '1':

    $ false; echo $? 1
Re: Do and Die
by Transient (Hermit) on Aug 15, 2005 at 18:57 UTC
    system will return the return code from the OS. This will be 0 if it is successful, thus a successful "move" will return a false value.

    You want something like...
    Die ("data load","YTSMSC","High","Could not move file: $filename for s +uccessful exit: $@") if system("mv $filename $localpath/archives/");
    or more readable
    system("mv $filename $localpath/archives/"); Die ("data load","YTSMSC","High","Could not move file: $filename for s +uccessful exit: $@") if $?;
Re: Do and Die
by Old_Gray_Bear (Bishop) on Aug 15, 2005 at 19:06 UTC
    Something odd is happening to the command in the sys-call, and it passed the information back via the exit-code. You might want to consider a slight modification to your routine to capture the return-code and plug it into the string you pass to Die(), don't forget to divide by 256 ....

    Something like:

    my $return_code = system(...); if ($return_code) { $return_code /= 256; Die(... "Return Code from system() was $return_code" ...); }
    (Not tested, usual disclaimers apply.)

    I Go Back to Sleep, Now.


Re: Do and Die
by sk (Curate) on Aug 15, 2005 at 19:04 UTC
    #!/usr/bin/perl -w # system ('echo', 'hi 0') or die $@; # INCORRECT WAY TO CHECK FOR F +AILURES my $ret = system ('echo', 'hi 1'); die $@ if $ret; die $@ if system ('echo', 'hi 2'); # Works but i don't think it i +s clean __END__ hi 1 hi 2
      To expand - you might want a special case for -1 (unable to start the program)

      my $retval = system( 'cmd' ); if ( $retval == -1 ) { die "Unable to start cmd!\n$!\n"; } elsif ( $? ) { die "Error returned from cmd! (".($?>>8).")\n$@\n"; }
Re: Do and Die
by jimbus (Friar) on Aug 15, 2005 at 19:25 UTC
    So I guess this falls under the stupid newbie trick catagory... I swear this was working at one point *grimaces as his head disemplodes* This is what I'm talking about when I said in my last post, that I'm trying to write mature code when I'm still trying to learn all the little things that all you guys are matter of fact about.

    Anyhow, thanks for all the good input!

    Madman Jimbus howls, "Never moon a werewolf!"
Re: Do and Die
by BaldPenguin (Friar) on Aug 15, 2005 at 18:52 UTC
    Is it possible that your $filename or $localpath could contain a space or othr illeagl character? I think I would try something like:
    system('mv', "'$filename'", "'$localpath/archives/'") or Die ("data lo +ad","YTSMSC","High","Could not move file: $filename for successful ex +it: $@");

    Everything I've learned in life can be summed up in a small perl script!

      I tried the following to test for spaces and try your suggestion:

      print "=$filename==$localpath/archives/=\n"; system('mv', "'$filename'", "'$localpath/archives/'") or Die ("data +load","YTSMSC","High","Could not move file: $filename for succ essful exit: $@");

      Running the code I got complaints from mv, but the or didn't kick in. but the files didn't move, wither. I did an ls with a cut and paste of one of the file names to proven that part is sound.

      [reports@clarkkent]/home/reports/ftp/YTSMSC50/test(11): ./ >/home/reports/ftp/YTSMSC50/test/SMSC_COUNTERS_SMSC.0507200200<>/home/ +reports/ftp/YTSMSC50/test/archives/< mv: cannot access '/home/reports/ftp/YTSMSC50/test/SMSC_COUNTERS_SMSC. +0507200200' >/home/reports/ftp/YTSMSC50/test/SMSC_COUNTERS_SMSC.0507200215<>/home/ +reports/ftp/YTSMSC50/test/archives/< mv: cannot access '/home/reports/ftp/YTSMSC50/test/SMSC_COUNTERS_SMSC. +0507200215' >/home/reports/ftp/YTSMSC50/test/SMSC_COUNTERS_SMSC.0507200230<>/home/ +reports/ftp/YTSMSC50/test/archives/< mv: cannot access '/home/reports/ftp/YTSMSC50/test/SMSC_COUNTERS_SMSC. +0507200230' >/home/reports/ftp/YTSMSC50/test/SMSC_COUNTERS_SMSC.0507200245<>/home/ +reports/ftp/YTSMSC50/test/archives/< mv: cannot access '/home/reports/ftp/YTSMSC50/test/SMSC_COUNTERS_SMSC. +0507200245' [reports@clarkkent]/home/reports/ftp/YTSMSC50/test(12): ls archives SMSC_COU +NTERS_SMSC.0507200230 errors SMSC_COUNTERS_SMSC.0507200200 SMSC_COU +NTERS_SMSC.0507200245 SMSC_COUNTERS_SMSC.0507200215 [reports@clarkkent]/home/reports/ftp/YTSMSC50/test(13): ls /home/repor +ts/ftp/YTSMSC50/test/SMSC_COUNTERS_SMSC.0507200230 /home/reports/ftp/YTSMSC50/test/SMSC_COUNTERS_SMSC.0507200230 [reports@clarkkent]/home/reports/ftp/YTSMSC50/test(14): ls -l /home/re +ports/ftp/YTSMSC50/test/SMSC_COUNTERS_SMSC.0507200230 -rw-r--r-- 1 reports staff 47472 Aug 12 12:08 /home/reports/ft +p/YTSMSC50/test/SMSC_COUNTERS_SMSC.0507200230 [reports@clarkkent]/home/reports/ftp/YTSMSC50/test(15):
      Crazy Uncle Jimbus says, "Never moon a werewolf!"

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://483941]
Approved by Old_Gray_Bear
[1nickt]: LanX You flatter yourself. I downvote only posts that I believe do not support and promote Perl. These would include threads berating P5P volunteers and others.
[marto]: fortunately my bad jokes and puns don't fall into those categories :P

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2018-04-26 10:35 GMT
Find Nodes?
    Voting Booth?