I am trying to build a "self sufficient" perl script that will run from generic webservers. The idea is to make installation as easy as humanly possible by placing the file in the cgi directory and chmoding it, period. Since the script uses a few images, I decided to "embed" them into the script itself, and this is where I'm having problems...

I have never dealt directly with binary data, so I am not absolutely sure that I know what I'm doing most of the time, please bear that in mind. After doing some reading, it seemed to me that the easiest way to embed these images into the script itself would be to produce text versions of them by using unpack, assigning the text to variables and then printing them back with pack, when requested by a query string (ie: foo.pl?img=bar). If anyone has a better idea, please feel free to speak up.

The "trouble" I'm having is when I unpack the image file. According to perlfunc, unpack should return an list, and all I'm getting is a variable.
Unpack() does the reverse of pack(): it takes a string representing a +structure and expands it out into a list value, returning the array value. (In scala +r context, it returns merely the first value produced.) The TEMPLATE has the same fo +rmat as in the pack() function. (...)
So this is (a snippet) what I've been up to:
open(IMG,"foo.gif"); foreach (<IMG>) { # something wrong here I suppose @hex = unpack("H*",$_); foreach $b (@hex) { # this array on has one item print "$b "; # which gets printed here } # shouldn't it be a list? print "\n"; } close(IMG);
The examples from perlfunc didn't help much. Anyone have any ideas of what I'm doing wrong? Or maybe, I'm not doing anything wrong per say, but the result isn't quite what I had expected. I was expecting a list of paired hex values, and I got a few BIG hexed strings instead.

Thank you again my perl brethren.

In reply to Files, unpack and text values by BBQ

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.