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

Fellow monks,

I'm tearing my hair out over this and I can't seem to find any answers.

I'm working on an app that is going to be reading from a file that will be made up of PGP encrypted e-mails. I'm going to read these in to an array so that I can process them one at a time.

My problem is that I can't figure out how to get MIME::Decoder to decode anything when I feed it a scalar - even if it's wrapped in a filehandle via IO::Wrap. It all works fine if I'm reading and writing to files, but I don't want to have to write every message to a file, decode it, write it back to a different file, then open and read it again to decode it.

When I run this through the debugger, $decoded is 1 - which according to the docs means it decoded something, but I'm at a loss how to make this work with just scalars.

I'm sure I've got a syntactial error in how I'm trying to do it, but I can't find any examples of what I'm trying to.

use strict; use Crypt::OpenPGP; use Data::Dumper; use MIME::Tools; use MIME::Decoder; use IO::Wrap; $|++; &decode; sub decode { my $decoder = new MIME::Decoder "quoted-printable"; my ($text, $outtext); { local $/; open(my $fh, '<', 'orders.txt') or die "Problem:$!"; $text = <$fh>; } $text = wraphandle($text); $outtext = wraphandle($outtext); my $decoded = $decoder->decode( $text, $outtext);

Any help would be appreciated!

Revolution. Today, 3 O'Clock. Meet behind the monkey bars.

Replies are listed 'Best First'.
Re: Using scalar instead of file handle in MIME::Decoder
by ikegami (Patriarch) on Oct 28, 2006 at 23:52 UTC
    IO::Wrap does not do what you think it does. You want IO::String, IO::Scalar or open(..., \$text). The last one is best, but only works in Perl 5.8.0 and higher.
    my $in_text = '...'; my $out_text; open(my $in_fh, '<', \$in_text) or die("Unable to create input file handle from string: $!\n"); open(my $out_fh, '>', \$out_text) or die("Unable to create output file handle from string: $!\n"); my $decoded = $decoder->decode($in_fh, $out_fh);
      Thank you! That's one problem down... :)

      Revolution. Today, 3 O'Clock. Meet behind the monkey bars.