I'm having issues with file handles stored as a scalar element within a anonymous hash. Example #1 below is a working beginning point and has a scalar $fh holding the opened file handle... It works, no problem.
EXAMPLE #1
use strict; MAIN: { my $fh; open($fh, ">hdt.html") or die("Unable to open output file!"); my $title = 'MY TTILE'; print $fh <<EOF; <html> <head> <title>$title</title> </head> <body> Blah, Blah... </body> </html> EOF close $fh; }
Example #2 below now opens the file handle into a scalar value of an anonymous hash referenced by $args...
EXAMPLE #2
use strict; MAIN: { my $args = {}; open($args->{fh}, ">hdt.html") or die("Unable to open output file! +"); my $title = 'MY TTILE'; print $args->{fh} <<EOF; <html> <head> <title>$title</title> </head> <body> Blah, Blah... </body> </html> EOF close $fh; }
And the following compile time error results!
D:\Development\Perl\HereDocTest>hdt2.pl<br> Scalar found where operator expected at D:\Development\Perl\HereDocTes +t\hdt2.pl line 10, near "<title>$title"<br> (Missing operator before $title?)<br> Bareword found where operator expected at D:\Development\Perl\HereDocT +est\hdt2.pl line 11, near "</head"<br> (Might be a runaway multi-line // string starting on line 10)<br> (Missing operator before head?)<br> Bareword found where operator expected at D:\Development\Perl\HereDocT +est\hdt2.pl line 13, near "Blah"<br> (Missing semicolon on previous line?)<br> syntax error at D:\Development\Perl\HereDocTest\hdt2.pl line 9, near " +head>"<br> Search pattern not terminated at D:\Development\Perl\HereDocTest\hdt2. +pl line 15<br>

BUT WHY DOES print screw up on $args->{fh} ????


Example #3 shows the work around I've been using.
EXAMPLE #3
use strict; MAIN: { my $args = {}; open($args->{fh}, ">hdt.html") or die("Unable to open output file! +"); my $title = 'MY TTILE'; my $fh = $args->{fh}; print $fh <<EOF; <html> <head> <title>$title</title> </head> <body> Blah, Blah... </body> </html> EOF close $fh; }
This is not a major issue, but I've tried:
print ($args->{fh}) <<EOF; print +$args->{fh} <<EOF; print +($args->{fh}) <<EOF;
And nothing results in differing errors. The behavior here is consistent using either UNIX & Win32 Perls. TIA, MJD

In reply to FileHandles as members of anon hash & Here docs??? by apostate1100

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.