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

Hello, I have spent considerable amount of time to make the PGP encryption work and thinking that I must be missing something. Here is the back ground.

Perl Version - Default version 5.8.4 on Solaris 10
PGP : PGP Command line freeware 6.5.8 installed on solairs

When I run the pgp encrytion command directly on the Solaris, its working like a charm and doing everything that I ask it to do, but the problem comes when I invoke thru a perl script. Its just creating a empty asc file but not writing anything to that file and cursor is just stuck there.

$dataFile = '/opt/ExportedPGPKeys/SampleData.txt'; $isEncrypted = `pgp -ea +batchmode $dataFile @sys`; print " The output value is $isEncrypted \n";


If I run another command like  ls instead of the PGP command thru the perl script then its working.

Did anyone come across this situation or any pointers will be grateful.

Thanks
newbie

Replies are listed 'Best First'.
Re: PGP encryption command not working thru Perl Backticks
by Corion (Patriarch) on Oct 08, 2015 at 13:10 UTC

    So, what is in @sys? I recommend inspecting the generated command before running it:

    my $cmd = "pgp -ea +batchmode $dataFile @sys"; warn "Running [$cmd]" if $verbose; $isEncrypted = `$cmd`;

    Most likely, things in @sys need double quotes.

      Apologies for earlier saying is did not work with double quotes. After testing it properly it DID work with double quotes. Again many thanks for your promt response which is why I'm loving perl bcos of this community.
Re: PGP encryption command not working thru Perl Backticks
by kennethk (Abbot) on Oct 08, 2015 at 14:50 UTC
    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.

      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.

Re: PGP encryption command not working thru Perl Backticks
by locked_user sundialsvc4 (Abbot) on Oct 08, 2015 at 13:22 UTC

    Would any of the Perl “error variables,” $@, $!, $^E, $? (see “Error Variables” in perldoc perlvar), provide any useful information about what the command-interpreter actually saw, and/or any error messages gpg might have produced in this case?

    /me nods that interpolation or the lack thereof is almost certain to be the problem here, and that separate interpolation into a string that is then executed will be the solution.