Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Images into MySQL database

by Gilimanjaro (Hermit)
on Jan 23, 2003 at 01:43 UTC ( #229194=note: print w/replies, xml ) Need Help??


in reply to Images into MySQL database

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!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://229194]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2023-05-31 10:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?