Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Embedding a bash funtion in perl script

by choroba (Canon)
on Oct 21, 2013 at 23:53 UTC ( #1059178=note: print w/ replies, xml ) Need Help??


in reply to Embedding a bash funtion in perl script

Shell functions cannot be trully embedded into Perl code. There are several reasons why your code does not work:

  1. Function definition must contain parentheses in a shell.
  2. There is a missing do after the for.
  3. There is a missing semicolon after the first backquotes.
  4. The main problem: each backquotes are run in a separate subshell. You cannot define a function in one subshell and run it in a different one (if it is not its subshell).

You can store the definition in a Perl variable, though, and prepend it to any code that needs to use it (note that backquotes interpolate variables):

my $f = 'forloop () { for ((a=1;a<=5;a++)) ; do var="$a" echo "$var" done }'; my $result=`$f; forloop`; print $result;

Nevertheless, usually implementing the whole logic in Perl might get you the clearest code.

لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ


Comment on Re: Embedding a bash funtion in perl script
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1059178]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2015-07-31 06:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (274 votes), past polls