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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |