|
|
| P is for Practical | |
| PerlMonks |
Re: Syntax error when trying to use a hash value as a file stream specifierby kcott (Archbishop) |
| on Sep 02, 2022 at 21:08 UTC ( [id://11146645]=note: 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:
Probably the first thing to note is that filehandles are globrefs. Here's an example (along with a hashref for comparison).
Had you used the strict pragma, you would have seen:
Always start your code with the following:
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.
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).
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 Section
Seekers of Perl Wisdom
|
|
||||||||||||||||||||||||||||||