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"


In reply to Re^5: create image via binmode by strat
in thread create image via binmode by Anonymous Monk

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.