in reply to Re^2: create image via binmode
in thread create image via binmode

If the image is rather small, you could also try something like:

my $imageContent = do { open (my $IN, "<", $image) or die "Error in reading '$image': $!"; binmode($IN); local $/ = undef; my $content = <$IN>; close($IN); return $content; }; # do open (my $OUT, ">", $outfile) or die "Error: couldn't write '$outfile': $!\n"; } # unless binmode($OUT); # thanks to pKai print $OUT $imageContent; close($OUT) or die "Error: couldn't close '$outfile': $!";
or shorter:
my $imageContent = do { open (my $IN, "<", $image) or die "Error in reading '$image': $!"; binmode($IN); local $/; # defaults to undef <$IN>; # close happens automatically at end of block if # you use a lexical variable as filehandle }; # do open (my $OUT, ">", $outfile) or die "Error: couldn't write '$outfile': $!\n"; binmode($OUT); # thanks to pKai print $OUT $imageContent; close($OUT) or die "Error: couldn't close '$outfile': $!";

Best regards,
perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8.8.8.32.11.32"

Replies are listed 'Best First'.
Re^4: create image via binmode
by pKai (Priest) on Jan 15, 2006 at 12:48 UTC
    There is a binmode $OUT; missing, which will lead to rendering the output unusable under Win.
Re^4: create image via binmode
by ikegami (Patriarch) on Jan 14, 2006 at 18:56 UTC
    That's the same as my first post, except you added do which doubles the memory usage.

      It's similar, but there are some differences (which may or may not be important):

      1. the local $/ has a very tight scope which may be important if you read additional files and don't want to get them in slurp mode.
      2. no global filehandles.
      3. a minimum of error handling
      4. filenames hardcoded only once

      well, you can also get point 1 with an ordinary block, e.g.

      my ($content, $IN); unless (open ($IN, "<", $file)) { die "Error in reading '$file': $!\n"; } # unless else { local $/ = undef; # valid only until end of block binmode($IN); $content = <$IN>; close ($IN); } # else
      or the like.

      Global filehandles have a problem: they are global, and if you open(IN, ...) and another IN is already opened, the first IN is closed. In some cases (e.g. recursions, reading filenames out of a file and opening them in a subroutine) this may be fatal.

      point 4 is in my eyes important that it is not good to write something like:

      unless (open ($IN, "<", "file1.txt")) { die "Error in reading 'file1.txt': $!\n"; } # unless
      because if the filename changes to something different, there is the danger you forget to change the filename in the die-message (or just do a stupid typo) and you or somebody else searches for the wrong problem

      Best regards,
      perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8.8.8.32.11.32"

        I agree with everything, and follow those practices myself. I just stopped reading after I read what I thought was the point of your post. ++'ed.