in reply to Re: PGP encryption command not working thru Perl Backticks
in thread PGP encryption command not working thru Perl Backticks

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.

Replies are listed 'Best First'.
Re^3: PGP encryption command not working thru Perl Backticks
by kennethk (Abbot) on Oct 08, 2015 at 15:12 UTC
    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?
        There are a few ways to implement this. The classic solution is to fork, set an alarm, and then kill the child thread if it hasn't responded yet; but then you need to get into interprocess communication.

        Probably the most straightforward approach is documented here: Perl 5.8.8 threads, clean exit You would run your backticks in the child thread, and sleep then reap in the parent.


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