Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Syntax error when trying to use a hash value as a file stream specifier

by LanX (Saint)
on Sep 03, 2022 at 11:50 UTC ( [id://11146656]=note: print w/replies, xml ) Need Help??


in reply to Syntax error when trying to use a hash value as a file stream specifier

classic filehandles° and indirect object syntax are a PITA constantly causing trouble.

may I suggest you use the approach to call print as a direct method?

> perl -de0 ... DB<108> open my $x, ">", \$out; $h{k}=$x DB<109> $h{k}->print("bla") DB<110> p $out bla DB<111> open FH, ">", \$out2; $h{k2}=FH DB<112> $h{k2}->print("bla") DB<113> p $out2 bla DB<114>

NB: I directed the output to $out vars only for demo purpose, it will work with all destinations.

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

°) bareword FH, glob *FH, glob-ref \*FH, ...

  • Comment on Re: Syntax error when trying to use a hash value as a file stream specifier
  • Download Code

Replies are listed 'Best First'.
Re^2: Syntax error when trying to use a hash value as a file stream specifier
by LanX (Saint) on Sep 03, 2022 at 12:47 UTC
    Sorry, this

    > $h{k2}=FH

    will only work in no strict scenarios, here corrected versions

    use v5.12; use warnings; use Data::Dump; my %h; open my $fh, ">>", \ my $out; $h{k}=$fh; $h{k}->print("bla"); dd $out; open FH2, ">>", \ my $out2; $h{k2}=*FH2; $h{k2}->print("bla2"); dd $out2; open FH3, ">>", \ my $out3; $h{k3}=\*FH3; $h{k3}->print("bla3"); dd $out3; # open FH4, ">>", \ my $out4; # $h{k4}=FH4; # $h{k4}->print("bla4"); # dd $out4; # Bareword "FH4" not allowed while "strict subs" in use at c:/tmp/pm/f +h.pl line 27. # Execution of c:/tmp/pm/fh.pl aborted due to compilation errors.

    "bla" "bla2" "bla3"

    FWIW: I prefer the 3rd $h{k3}=\*FH3 over the 2nd variant

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11146656]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2026-05-12 01:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.