in reply to PGP encryption command not working thru Perl Backticks

As Corion says, this feels like it's probably an escaping issue. It requires a few more lines, but a multi-argument piped open should escape your arguments. So:
$dataFile = '/opt/ExportedPGPKeys/SampleData.txt'; $isEncrypted = do{ open my $exec, '-|', qw'pgp -ea +batchmode', $dataFile, @sys or di +e "PGP pipe failed: $!"; binmode $exec; <$exec>; }; print " The output value is $isEncrypted \n";
I say should because your perl version is very old -- Perl 5.10 fixed a host of inconsistent behaviors and I'm not certain if 5.8.4 handles system call escaping. The oldest docs I checked (5.8.8) were ambiguous on the point (link).

#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Replies are listed 'Best First'.
Re^2: PGP encryption command not working thru Perl Backticks
by newperlmonkey (Novice) on Oct 08, 2015 at 15:03 UTC
    Normally if backticks are supposed to grab whether the command returns the success output or error out put, then why my earlier code did not return the error to me in  $isEncrypted variable. Is my understanding wrong?

    Btw, I'm testing this DEV env, but from QA we have perl 5.10 so might have some flexibility there.
      Backticks return the output of the code, not the exit status. If you want that, you probably want system, where
      The return value is the exit status of the program as returned by the wait call. To get the actual exit value, shift right by eight (see below). See also exec. This is not what you want to use to capture the output from a command; for that you should use merely backticks or qx//, as described in `STRING` in perlop. Return value of -1 indicates a failure to start the program or an error of the wait(2) system call (inspect $! for the reason).
      $dataFile = '/opt/ExportedPGPKeys/SampleData.txt'; $isEncrypted = system(qw'pgp -ea +batchmode', $dataFile, @sys); print " The output value is $isEncrypted \n";

      You should note the most current Perl is 5.22, and I think 5.14 is already EOL'd. Not that old versions don't work just fine, and not that old versions aren't still bundled in modern OS's.

      Your lack of my makes me concerned that you are learning old-style Perl. See for example Use strict warnings and diagnostics or die or Modern Perl.


      #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

        Thanks kennethk. I have modified my code to below
        my $dataFile = '/opt/ExportedPGPKeys/SampleData.txt'; my $isEncrypted = `pgp -ea +batchmode $dataFile 0xc33721`; #0xc33721 is the public key ID. print " The output value is $isEncrypted \n";

        I wanted just the output of the command so sticking to Backticks, but I wanted to address a scenario where during the execution of the PGP command for some reason if the cursor dont return to the perl code and stuck during the command execution itself say for 10 seconds, then I want to terminate(die) or force exit this perl run and log an error.
        Is there a possibility to achieve this?