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

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.

  • Comment on Re: "scalar found where operator expected" while printing to a glob ref

Replies are listed 'Best First'.
Re^2: "scalar found where operator expected" while printing to a glob ref
by seki (Monk) on Mar 01, 2016 at 17:56 UTC
    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:  <%-{-{-{-<

        Yes, the block returning the dereferenced file handle is doing the job.

        Even if it is not very explicit in the documentation, it is suggested in the last paragraph.

      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.

        Well, I've just checked and it is not the actual package code.
        I remember now that I found the example of custom ConsumerInterface in the documentation of XML::SAX::Writer
        sub output { my $self = shift; $$self .= uc shift; }
        and I have adapted it to my needs: replacing the string scalar to store the data by a file handle. Thus I am responsible for print $$self $data;. Actually I was surprised for not having the warning while using the stock module... Mea culpa.