#!/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 () { 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";