The book "Perl Best Practices" (ISBN 0596001738) recommends that you always use braces around the file handle in a print statements to make it clear that you did not forget a comma. This practice would eliminate your problem as well.

Did you consider redirecting your default output with select rather than passing it as an argument?

UPDATE: I believe that the OP is passing a file-handle to a subroutine using the method that his reference (Cookbook recipe 10.7) calls 'Named Parameters'. Using this method, the file-handle is stored as a value in a hash. He as asking for the syntax to use that value in a print statement. AnomalousMonk has provided the answer. My suggestion to use select to pass the file-handle to the subroutine would work, but I no longer recommend it.

use strict; use warnings; myfunction1( handle => \*STDERR); sub myfunction1{ my %hash = ( handle => \*STDOUT, @_ ); print "something will happen\n"; print {$hash{handle}} "anything from myfunction1\n"; # ^ ^ # braces added here }
Bill

In reply to Re: Syntax error when trying to use a hash value as a file stream specifier by BillKSmith
in thread Syntax error when trying to use a hash value as a file stream specifier by fireblood

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.