seki has asked for the wisdom of the Perl Monks concerning the following question:
while working on a bigger piece of code I stumbled on this warning while printing on a GLOB reference:
Scalar found where operator expected at test_fhref.pl line 17, near "$self $data" (Missing operator before $data?)
Could you tell why? The code looks simple, by even a short piece of code can be dangerous for a Perl beginer ;)
use strict; use warnings; use feature 'say'; { package Test; sub new { my $class = shift; my $ref = shift; return bless \$ref, ref $class || $class; } sub send { my $self = shift; my $data = shift; use Data::Dumper; say Dumper $$self; # ==> $VAR1 = \*{'::$file'}; so it is a GLO +B ref print $$self $data; #the following does not warn... #~ my $fh = $$self; #~ print $fh $data; } } open my $file, '>', 'test.txt' or die $!; my $t = new Test($file); $t->send("foo"); close $file;
for those who may ask themselves "why" using such indirect access, I copy/pasted from XML::SAX::Writer:ConsumerInterface to make my own Consumer class, and the interpreter seems irritated by a simple
sub output { my ($self, $data) = @_; print $$self $data; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: "scalar found where operator expected" while printing to a glob ref
by Eily (Monsignor) on Mar 01, 2016 at 16:51 UTC | |
by seki (Monk) on Mar 01, 2016 at 17:56 UTC | |
by AnomalousMonk (Archbishop) on Mar 01, 2016 at 18:25 UTC | |
by seki (Monk) on Mar 01, 2016 at 21:36 UTC | |
by Eily (Monsignor) on Mar 02, 2016 at 08:16 UTC | |
by seki (Monk) on Mar 02, 2016 at 13:18 UTC | |
by MidLifeXis (Monsignor) on Mar 03, 2016 at 18:30 UTC |