Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

G'day Fireblood,

In the examples below, I've used a common alias of mine which picks up many problems:

$ alias perle alias perle='perl -Mstrict -Mwarnings -Mautodie=:all -MCarp::Always -E +'

Probably the first thing to note is that filehandles are globrefs. Here's an example (along with a hashref for comparison).

$ perle 'open my $fh, ">", "not_a_file"; say "$fh"; unlink "not_a_file +";' GLOB(0x8000c42e0) $ perle 'my $hashref = {}; say "$hashref";' HASH(0x800003bc8)

Had you used the strict pragma, you would have seen:

Bareword "STDERR" not allowed while "strict subs" in use ...

Always start your code with the following:

use strict; use warnings;

or code that gives you one or both of those (e.g. use v5.12 gives you strict; use v5.36 gives you strict and warnings; various modules have similar effects):

The next question might be, what should you use instead of STDERR? I'd argue that \*STDERR is strictly correct as it's a globref; however, *STDERR will also work.

$ perle 'my $x = \*STDERR; say "$x";' GLOB(0x80008fcd0) $ perle 'my $x = *STDERR; say "$x";' *main::STDERR

That finally brings us to how to use the hash value without an intermediate scalar value. You have two main options (perhaps others will suggest additional ones).

  • As already mentioned by ++AnomalousMonk, continue to use "Indirect Object Syntax" and wrap the hash value in braces:
    $ perle 'my %fh = (err => \*STDERR); print {$fh{err}} "errmsg\n";' errmsg $ perle 'my %fh = (err => *STDERR); print {$fh{err}} "errmsg\n";' errmsg
  • Use the hash value with the filehandle, and invoke print, say, and so on, as methods:
    $ perle 'my %fh = (err => \*STDERR); $fh{err}->print("errmsg\n");' errmsg $ perle 'my %fh = (err => *STDERR); $fh{err}->print("errmsg\n");' errmsg

Side note: All of the above examples were run using Perl v5.36. This version disables the indirect feature by default (see "perl5360delta: use v5.36"). Obviously, the filehandle case is still allowed; however, if you do use that syntax elsewhere (e.g. my $obj = new Class ...), you may want to consider getting out of the habit of doing so.

— Ken


In reply to Re: Syntax error when trying to use a hash value as a file stream specifier by kcott
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2024-04-23 20:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found