I have not used bareword filehandles since lexical filehandles were made available. While upgrading a Perl program from bareword filehandles to lexical filehandles I noticed an inconsistency using heredocs. Bareword filehandles in heredocs work fine with a space following the "<<", but lexical filehandles in heredocs generate an error with a space following the "<<".
Since bareword filehandles are due to be deprecated, this may not be a pressing issue. I'm posting this so that others who may run into the same situation do not have to spend the debugging time tracking down this inconsistency.
The following code shows the inconsistency:
#!/usr/bin/perl
use strict;
use warnings;
my $HFILE;
my $hfile = 'foo.txt';
# GOOD
#####################################################################
# Bareword filehandle works with space following heredoc "<<".
#####################################################################
open(HFILE, '>', $hfile) or die "Error opening help file $hfile: $!\n"
+;
print HFILE << "EOT";
Usage: foo.pl
Options:
--------
-h Display help information.
EOT
# BAD
#####################################################################
# Lexical filehandle does NOT work with space following heredoc "<<".
#####################################################################
open($HFILE, '>', $hfile) or die "Error opening help file $hfile: $!\n
+";
print $HFILE << "EOT";
Usage: foo.pl
Options:
--------
-h Display help information.
EOT
"It's not how hard you work, it's how much you get done."
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.