Chris Nehren on 19 Apr 2011 12:55:43 -0700 |
[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]
Re: [PLUG] perl -e with system() |
On Apr 19, 2011, at 11:01 , Walt Mankowski wrote: > On Tue, Apr 19, 2011 at 10:39:58AM -0400, Chris Nehren wrote: >>> perl -n -e '($a, $b, $c) = split("\t"); chomp $a; chomp $b; chomp $c; if ($c) {system ("echo $c >> $a")} >>> else {system ("echo $b >> $a")}' < data.file >> >> The problem is that you're not quoting things properly. The LIST >> form of system does this for you, but it also doesn't invoke a >> subshell (so you can't do >>). > > That's not correct. If system() is passed a single parameter, that's > checked for shell metacharacters, and if any are found, the argument > is passed to "/bin/sh -c". At least on unix. Different things will > happen on other platforms. Um, I never said anything about the single argument form of system. I can read the docs, too. >> You really should be using open instead: >> >> open my $fh, '>>', $file or die "open($file): $!"; >> print $fh $content; >> close $fh; > > That would certainly be a more robust solution, but it's also making > it a pretty big one-liner... Despite perl's reputation otherwise, not everything should be a one-liner. >> Note also that $a and $b are special variables used by perl for >> sort() and you shouldn't use them. See perldoc perlopentut for more >> info, and please consider reading Beginning Perl so you don't make a >> mess of things. > > True, you definitely don't want to do that in a real program. But it > hardly matters in a one-liner where he's clearly not doing any > sorting. Better to develop good habits for writing real code. -- Thanks and best regards, Chris Nehren ___________________________________________________________________________ Philadelphia Linux Users Group -- http://www.phillylinux.org Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce General Discussion -- http://lists.phillylinux.org/mailman/listinfo/plug