G'day loris,

The reason you're getting that error is because the heredoc (in your module) is being interpolated.

In your module, you've probably either coded <<"PROLOG" or <<PROLOG.

See perlop: Quote-Like Operators for an explanation of how explicit single- and double-quoted terminating strings (i.e. PROLOG in your example) are interpolated; also note that the absence of explicit quotes assumes double-quotes.

There are a variety of hoops you have to jump through to get similar code working in a one-liner. These have mostly been pointed out in various replies. You could embed newlines in your one-liner and do something like this:

$ perl -Mstrict -Mwarnings -e 'package test; my $text = <<\TEST; test@heredoc TEST print $text' test@heredoc

That's not using the module as such; just running the code in a package other than main. You're also using a different (albeit equivalent) method of quoting the terminator (i.e. <<\TEST). What you'd really want is something closer to this:

$ perl -Mstrict -Mwarnings -e 'package test; my $text = <<\TEST; test@heredoc TEST sub test_print { print $text } package main; use parent -norequire => "test"; test::test_print()' test@heredoc

That's a lot of effort; it's not really comparing apples with apples; and, given you could have copied most of the code you already had, probably takes a similar amount of time as coding this test script:

#!/usr/bin/env perl use strict; use warnings; use pm_example_module; pm_example_module::test_print();

and this test module:

package pm_example_module; use strict; use warnings; my %text_blocks; $text_blocks{en}{prolog} =<<'PROLOG'; You have received this email because something happened. Please send feedback and suggestions to tldr@nohelp.org. PROLOG sub test_print { print $text_blocks{en}{prolog}; } 1;

Running that script produces:

You have received this email because something happened. Please send feedback and suggestions to tldr@nohelp.org.

Changing <<'PROLOG' to either <<"PROLOG" or <<PROLOG, produces (in both cases):

Possible unintended interpolation of @nohelp in string at pm_example_m +odule.pm line 8. Global symbol "@nohelp" requires explicit package name at pm_example_m +odule.pm line 8. Compilation failed in require at ./pm_example.pl line 6. BEGIN failed--compilation aborted at ./pm_example.pl line 6.

You now have a valid test which reproduces what you were attempting to test and, I suspect, would have taken less time than writing and posting your OP.

-- Ken


In reply to Re: Heredoc in one-liner? by kcott
in thread Heredoc in one-liner? by loris

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.