Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

RFC Using PERL HEREDOC script within bash

by dcronin135 (Acolyte)
on Aug 27, 2014 at 03:29 UTC ( #1098695=perlmeditation: print w/replies, xml ) Need Help??

This submission is in response to others asking how to embedded a PERL within a bash or ksh script. Though it may not be a common practice, it does illustrate a couple of examples as to how this would be accomplished.

#!/bin/sh # If you are not passing bash var's into the PERL HEREDOC, # then single quote the HEREDOC tag perl -le "$(cat <<'MYPL' # Best to build your out vars rather than writing directly # to the pipe until the end. my $STDERRdata="", $STDOUTdata=""; while ($i=<STDIN>){ chomp $i; $STDOUTdata .= "To stdout\n"; $STDERRdata .= "Write from within the heredoc\n"; MYPL print $STDOUTdata; # Doing the pipe write at the end will save you warn $STDERRdata; # a lot of frustration. )" <myInputFile 1>prints.txt 2>warns.txt


#!/bin/sh set WRITEWHAT="bash vars" # If you want to include your bash var's # Escape the $'s that are not bash vars. perl -le "$(cat <<MYPL my $STDERRdata="", $STDOUTdata=""; while (\$i=<STDIN>){ chomp \$i; \$STDOUTdata .= "To stdout\n"; \$STDERRdata .= "Write $WRITEWHAT from within the heredoc\n"; MYPL print \$STDOUTdata; # Doing the pipe write at the end will save you warn \$STDERRdata; # a lot of frustration. )" <myInputFile 1>prints.txt 2>warns.txt

If you wanted to pass command line arguments, insert them before the < indirect for STDIN.

Replies are listed 'Best First'.
Re: RFC Using PERL HEREDOC script within bash
by roboticus (Chancellor) on Aug 27, 2014 at 13:07 UTC


    What is that buying you? Those scripts could just be perl scripts for what it's worth. Why the extra rigamarole to put bash/ksh into the process. If you've got a particular use case that shows why you'd actually *want* to do that, it would help your post.


    When your only tool is a hammer, all problems look like your thumb.

      Maybe when forced to incrementally "port" a legacy bash script to Perl. I have re-implemented some bash scripts in the past.

      One legacy bash scripts that stands out in my memory, had several embedded AWK and Sed scripts. Management was very sceptical, so I had to do it incrementally. Phase 0, I replaced the AWK and Sed scripts with Perl equivalents. In some cases, I was able combine adjacent AWK/Sed scripts into a single Perl script. This was fortuitous because it gave a small but measurable performance boost. After demonstrating the new version worked correctly, I moved to phase 1 to re-implement the rest of the script, which was straight forward. The end result was much faster and much less fragile, therefor much easier to maintain and improve.

      If it is primarily a shell script with some trivial Perl that had been in a separate file embedding one in the other can slightly ease deployment. Reimplementing it all in Perl is probably better but might take much longer to get right.

      Just illustrating and example of how this might be done when a bash or ksh script needs a little more scripting muscle. It merely illustrates a method for leveraging PERL within bash or shell of choice, when OS utilities fall a little short. No one tool can do it all, though PERL comes very close, but, when it can't... the illustration offers by example an enhancement to your existing tools out of the box.

      roboticus, Sorry we don't share the same out-of-box thinking. I have seen other requests for this type of method. Merely fulfilling a need to know, whether you agree or not.

        No one tool can do it all

        Well, Perl can! :-) Especially with the appropriate modules, plus it's pretty portable. (With what kind of tasks did you have portability problems with Perl?)

        You did post on a Perl website after all, so of course everyone says, why not do it all in Perl? ;-)

Re: RFC Using PERL HEREDOC script within bash
by ikegami (Pope) on Aug 27, 2014 at 19:17 UTC
    Other solutions here
Re: RFC Using PERL HEREDOC script within bash
by trippledubs (Deacon) on Aug 28, 2014 at 13:27 UTC
    I think in general, Inception* Scripting is frowned on so you would have to have a convincing argument of why you would need to do that opposed to how.

    Alternatively, go at least three-five scripts deep to really be impressive.

    *Inception is a movie reference
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://1098695]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2020-10-22 23:34 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (232 votes). Check out past polls.