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

In reply to "scalar found where operator expected" while printing to a glob ref by seki

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.