This is me without docs, just my first impression...

You're *almost* there... You've either got a knack for coding, or for copying examples, which is actually pretty much the same knack... :)

On your very first line, you assign the value of <$img> to a scalar. This means the <> brackets are used in a scalar context, which in turn means they read the handle up to (and including) the first 'input record separator', which by default on a unix system should be a line-feed (\n or ascii 10).

GIF (or JPG, or PNG) images can contain an ascii 10. In fact, the odds of any character being an ascii 10 are 1 in 256. Which gives you pretty good odds of not getting 'the big picture' if it's anything over 1kb....

What would probably be the easiest way to handle this, is using the <> operator in a list context. But you still want the entir file in one string:

$img = join '', <$img>;

That should work. The other way around it is to set the input record separator to 'undef' before using the <> operator.

The drawback of that one is that it could mess up other code. The drawback of the join, is that for a brief moment, the content will have to exist as one big (anonymous) array, because of the list context of the <> operator. Immediately afterwards it will be joined, so there will be 2 copies of the file present in memory (one as a string, and one as an array of lines). The array will get garbage-collected as soon as the statement ends (I think; I'm no perl-internals kind-a-guy), so it shouldn't be a problem for any but the hugest of files.

Having to convert the array to a big string will also have a slight performance hit, but again, not much if the file isn't huge.

In my opinion Perl's power lies in efficient programming. And the efficiency that matters IMHO is the results / time spent equation, not the memory used / memory available one...

Happy coding!

Update:
blokhead beat me to it! He got both the input records separator right, and the way to keep it local to the context at hand... And he's a faster typist!


In reply to Re: Images into MySQL database by Gilimanjaro
in thread Images into MySQL database by cidaris

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.