in reply to system call with variable interpolation
When I come across strange behaviour such as this, there are a number of things I do automatically to determine why perl's interpration of what to do ( or the shell's interpretation ) differs from mine. They're supposed to DWIM!!
I start by adding use warnings; use strict at the top, which in a short stub such as this I might not have. On the other hand, presumably the stub was created to decipher odd behaviour in a larger program, in which case the strictures would be the first thing I type.
Calling system() with strings generated in place invites surprises. I always break it out into the definition of the string, assigned to a variable; logging the variable; and then invoking the command.
System calls need to have their return status checks. Of course program invocation returns notification of errors, rather than success, so the logic is reversed from what you would like.
my $cmd = "echo $abc >> log.txt"; print "cmd is $cmd\n"; system( $cmd ) == 0 or die( "Error in system() call: $?.\n" );
Which prints out:
echo Fri Jul 15 12:41:31 EDT 2011 >> log.txt
At this point I noticed the date needed to have the newline chomped off, after which program succeeded.
As Occam said: Entia non sunt multiplicanda praeter necessitatem.
|
|---|