Encodes image into XML and decodes it back so Tk::Photo can use it. Useful for writing server/client with binary files exchange via XML. Based on MIME::Base64 and XML::Twig. I am using it to read images from my server via TCP.
use MIME::Base64;
my $encoded = MIME::Base64::encode(slurpfile("image.jpg"));
my $oxml = "<?xml version=\"1.0\" standalone=\"yes\"?> <data image=\"$
+encoded\"/>\n";;
#-----------------------------------------------------
#-----------------------------------------------------
use XML::Twig;
my $twig= XML::Twig->new(); # XML Parser
$twig->parse($oxml);
my $root;
$root = $twig->root();
my $xmlencoded = $root->att('image');
#-----------------------------------------------------
#-----------------------------------------------------
use Tk;
use Tk::JPEG;
my $mw = MainWindow->new();
my $image = $mw->Photo(-data=>$xmlencoded);
my $lab = $mw->Label(-image=>$image)->pack;
$mw->update();
MainLoop;
sub slurpfile
{
open(IN, "< $_[0]") or die "can't open $_[0]: $!";
binmode (IN);
seek(IN, 0, 0); sysread (IN, my $slurp, -s IN);
close(IN);
return $slurp;
}