Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-04-25 12:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found