2

Hi I have a Unix Shell Script call Food.sh ; I have a Perl Script call Track.pl. Is there a way where I can put Track.pl's code in to Food.sh code and still have it work ? Track.pl requires one arugement to label a name of a folder.

Basically it will run like this.

Unix Shell Script codes RUN

step into 

Perl Script codes RUN
Put in name of folder for Perl script
Rest of script runs

exit out.

2 Answers 2

14

You have a few options.

  1. You can pass the code to Perl using -e/-E.

     ...
     perl -e'
    
        print "Hello, World!\n";
    
     '
     ...
    

    Con: Escaping can be a pain.[1]

  2. You can pass the code via STDIN.

     ...
     perl <<'END_OF_PERL'
    
        print "Hello, World!\n";
    
     END_OF_PERL
     ...
    

    Con: The script can't use STDIN.

  3. You can create a virtual file.

     ...
     perl <(
     cat <<'END_OF_PERL'
    
        print "Hello, World!\n";
    
     END_OF_PERL
     )
     ...
    

    Con: Wordy.

  4. You can take advantage of perl's -x option.

     ...
     perl -x -- "$0"
     ...
     exit
    
     #!perl
     print "Hello, World!\n";
    

    Con: Can only have one snippet.

    $0 is the path to the shell script being executed. It's passed to perl as the program to run. The -x tells Perl to start executing at the #!perl line rather than the first line.

Ref: perlrun


  1. Instances of ' in the program needs to escaped using '\''.

    You could also rewrite the program to avoid using '. For example, you could use double-quoted string literals instead of single-quoted string literals. Or replace the delimiter of single-quoted string literals (e.g. q{...} instead of '...'). As for single-quoted inside of double-quoted and regex literals, these can be replaced with \x27, which you might find nicer than '\''.

Sign up to request clarification or add additional context in comments.

4 Comments

From the linked document: -x tells Perl that the program is embedded in a larger chunk of unrelated text, such as in a mail message. Leading garbage will be discarded until the first line that starts with #! and contains the string "perl". Any meaningful switches on that line will be applied.
When I came back from reading perlrun in preparation for answering, I found two perfectly good answers waiting. You folks are just too fast...
Thanks, I thought it might be useful to include that information here to keep your answer self-contained. So the exit belongs to the shell script and the "$0" is an argument passed to the the perl script.
@TomFenech $0 is the shell script's own file name. It's passed to perl itself as the program to run. The -x limits that running to stuff after a line starting with "#!" and containing "perl"
1

(I'm assuming your goal is just to have all of the code in a single file so that you don't have multiple files to install)

Sure, there's a way to do this, but it's cumbersome. You might want to consider converting the shell script entirely to Perl (or the Perl script entirely to shell).

So ... A way to do this might be:

#!/bin/sh

echo "shell"

perl -E '
say "perl with arg=$ARGV[0]"
' fred

echo "shell again"

Of course, you'd have to be careful with your quotes within the Perl part of the program.

You might also be able to use a heredoc for the Perl part to avoid quoting issues, but I'm not sure about that.

4 Comments

I think that should be -e, not -E.
@Tom Fenech, -E is -e plus an implied use feature ":".substr($^V,1);.
So it enables the feature bundle for the version of Perl running the one-liner.
@TomFenech Yep. It was introduced in 5.10.0 so you won't find it in older versions of perl.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.