Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

"scalar found where operator expected" while printing to a glob ref

by seki (Monk)
on Mar 01, 2016 at 16:26 UTC ( [id://1156541]=perlquestion: print w/replies, xml ) Need Help??

seki has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

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; }

The best programs are the ones written when the programmer is supposed to be working on something else. - Melinda Varian

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

    Hello seki. print doesn't take an arbitrary expression as a filehandle, it's either a scalar variable, a direct filehandle (bareword) or a block. To quote the doc:

    If you're storing handles in an array or hash, or in general whenever you're using any expression more complex than a bareword handle or a plain, unsubscripted scalar variable to retrieve it, you will have to use a block returning the filehandle value instead, in which case the LIST may not be omitted.

      I see... So it seems that I picked the wrong piece of code as an example to make my own xml consumer :p

        Further to Eily's post: Instead of
            print $$self $data;
        try
            print { $$self } $data;
        (untested).


        Give a man a fish:  <%-{-{-{-<

        It does seem so, but I couldn't find that piece of code in XML::SAX::Writer, which is not really surprising since this is a syntax error, not just a warning.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1156541]
Approved by Paladin
Front-paged by Tanktalus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-25 17:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found