in reply to System(), writing to a new file

This might be system dependent, but backticks work for me:

#!/usr/bin/perl use strict; use warnings; my $file = 'somefile.txt'; my @args = ("sort", $file); open (my $out, ">", "outputfile.txt"); print $out `@args` or die "system @args failed: $?";

-Michael

Replies are listed 'Best First'.
Re^2: System(), writing to a new file
by Anonymous Monk on Jul 18, 2013 at 08:42 UTC
    Load all of the output of sort(1) to perl's memory, just to write it out to the file right after? Hardly efficient.

    As mentioned below, system(@list) doesn't invoke the shell. This is usually a good thing, but in this case the shell redirection the OP is expecting won't work. system($string) invokes the shell. Hence, system("sort $infile > $outfile") will work.

    However, there's a better approach: Use the -o flag to sort(1), like this: @args = 'sort', '-o', $outfile, $infile; system(@args);

      Perfect. Thank you
        (Please ignore. I replied to the wrong node.)