Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Capture response from backticks to a file.

by Anonymous Monk
on Nov 13, 2013 at 19:09 UTC ( #1062434=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!
I am trying to run this code and capture its output to a file, the code runs well, but cant get output to a file to work, here is what I am testing with:
#!/usr/bin/perl use strict; use warnings; use File::Slurp; use Data::Dumper; $| = 1; # Encrypt file. my $gpg_command = `gpg --trust-model always -er 12345678 my_file.txt`; print defined($gpg_command) ? $gpg_command : "Command failed\n"; # Capture STDOUT and save it to a file. open(READ, "$gpg_command 2>&1 |") or die "Failure on open $!\n"; open( my $out, ">", "encrypted.pgp" ) or die "Could not open file for + encrypted data - $!"; while (<READ>) { my $output .= $_; print $out $output; } close $out or die "1 Failure on close $!\n";; close (READ) or die "2 Failure on close $!\n"; print "\nDone.\n";
Thanks for looking!

Replies are listed 'Best First'.
Re: Capture response from backticks to a file.
by kcott (Bishop) on Nov 13, 2013 at 20:04 UTC

    On my system, gpg returns 0 if there were no problems, 1 otherwise. There's no undef (or equivalent) returned so checking the return value with defined() isn't doing what you appear to think it is.

    Again, on my system, both "-o file" and "--output file" are documented as "Write output to file.". Perhaps that's what you want.

    Type "man gpg" at your command line to see how this is implemented on your system.

    -- Ken

Re: Capture response from backticks to a file.
by dsb (Chaplain) on Nov 13, 2013 at 19:26 UTC
    I think you're misusing the backticks in your code.

    this bit:

    my $gpg_command = `gpg --trust-model always -er 12345678 my_file.txt`; print defined($gpg_command) ? $gpg_command : "Command failed\n"
    IS capturing the output from the command and then printing it to STDOUT.

    But in your open command, you're effectively trying to open a pipe to a command that matches the output from your original backtick command, which should be failing I'd think.

    remember that backticks do execute your command, and things get a little simpler. For example:

    use strict; use FileHandle; my $fh = FileHandle->new('capture.out',"w"); print $fh `dir`;


    dsb
    This @ISA my( $cool ) %SIG
      It doesn't fail, the backticks executes fine but I can't capture the result to a file still.
        I agree with dsb. You've already run the pgp command and have capured it's output in $gpg_command. Sorry to be dense, but what are you hoping to achieve from the rest of your script?
Re: Capture response from backticks to a file.
by marinersk (Priest) on Nov 13, 2013 at 21:17 UTC
    I would change:
    # Encrypt file. my $gpg_command = `gpg --trust-model always -er 12345678 my_file.txt`; print defined($gpg_command) ? $gpg_command : "Command failed\n"; # Capture STDOUT and save it to a file. open(READ, "$gpg_command 2>&1 |") or die "Failure on open $!\n"; open( my $out, ">", "encrypted.pgp" ) or die "Could not open file for + encrypted data - $!";

    to:

    # Build encryption command my $gpg_command = "gpg --trust-model always -er 12345678 my_file.txt"; # Execute command and capture STDOUT. my $gpg_results = `$gpg_command`; print defined($gpg_results) ? $gpg_results : "Command failed\n"; # Save captured STDOUT to a file. open( my $out, ">", "encrypted.pgp" ) or die "Could not open file for +encrypted data - $!";

    Then:

    • Correct any errant assumptions about what happens when the command fails;
    • Adjust what you're writing to the output file to reflect what you actually want there.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1062434]
Approved by dsb
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2020-12-03 17:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How often do you use taint mode?





    Results (57 votes). Check out past polls.

    Notices?