I think the examples given are a bit misleading; the key part is the definition of the PassphraseCallback argument in the docs, in particular the last part:

    In either case, the callback routine should return the
    passphrase, a scalar string.

The various examples sometimes leave off the final call to _prompt(), and since it is an undocumented internal routine it isn't obvious that this is doing the guts of the work: showing a prompt, setting noecho, getting the text from the user's input, and returning the resulting string.

I'd suggest looking at the code for _prompt() in the module (it's in the top-level Crypt/OpenPGP.pm) to see what needs doing, and then (probably) avoid using ths undocumented interface directly by copying what you need out of there.

Here's some similar code I use in a less critical situation, which copes with the possibility that Term::Readkey isn't installed by letting the password be entered unhidden:

sub getpass { my $prompt = shift; local $| = 1; print "$prompt: "; eval { require Term::ReadKey }; my $haveterm = !$@; Term::ReadKey::ReadMode(2) if $haveterm; # turn on noecho my $pass = <STDIN>; chomp $pass; Term::ReadKey::ReadMode(0) if $haveterm; # restore print "\n"; return $pass; }

Hugo


In reply to Re: Re: Re: Crypt::OpenPGP PassphraseCallback example please by hv
in thread Crypt::OpenPGP PassphraseCallback example please by boat73

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.