in reply to create image via binmode
$image = <IN> ; only reads one line of the file. By default, a line is defined as ending with "\n". Undefining $/ before the read changes this definition and cause the file to be read as one line.
While you're at it, check for errors, and use the safer 3-arg open.
#! /usr/bin/perl open IN, "<", "abc.gif" or die "Unable to open input file: $!\n"; binmode IN ; local $/ ; $image = <IN> ; close IN ; open OUT, ">", "test.jpg" ; or die "Unable to open output file: $!\n"; binmode OUT ; print OUT $image ; close OUT ;
Update: You could also do it in blocks:
#! /usr/bin/perl open IN, "<", "abc.gif" or die "Unable to open input file: $!\n"; binmode IN ; open OUT, ">", "test.jpg" ; or die "Unable to open output file: $!\n"; binmode OUT ; local $/ = \1024; # Read in 1024 byte blocks. print OUT $_ while <IN>; close IN ; close OUT ;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: create image via binmode
by Anonymous Monk on Jan 13, 2006 at 23:28 UTC | |
by ikegami (Patriarch) on Jan 14, 2006 at 02:16 UTC |