ivanatora has asked for the wisdom of the Perl Monks concerning the following question:

Hello,
I like to execute commands with backticks - it is short, and tidy, and can capture return text. Problem occures when the command has too many parameters and I decide to split it at multiple lines. In bash-like shells you can do that with adding the "\" sign at the end of the line and continue on the next line. However the backticks don't like that and they think every line is a separate command. Could that behavior be fixed in some way?

Replies are listed 'Best First'.
Re: Backtics execution with multiple lines
by ikegami (Patriarch) on Feb 04, 2009 at 17:52 UTC

    However the backticks don't like that

    Really? Works for me.

    $ perl -e'print `echo foo \\ bar`' foo bar
Re: Backtics execution with multiple lines
by moritz (Cardinal) on Feb 04, 2009 at 17:54 UTC
    It's not the backticks that interpret it as two commands, but your shell. This works for me:
    $ cat foo.pl print ` echo foo \\ bar`; $ perl foo.pl foo bar
      That's curious, becouse I can do command line splitting with "\" in my shell.
      And here backticks are not working:
      $ cat backticks.pl #!/usr/bin/perl `ls \ -l *.pl`; $ perl backticks.pl sh: line 1: -l: command not found
        You're not passing a slash to the shell. Backticks are like "". If you want the resulting string to contain a slash, you need to escape it.
        $ perl -le'print "foo bar"' foo bar $ perl -le'print "foo\ bar"' foo bar $ perl -le'print "foo\\ bar"' foo\ bar
Re: Backtics execution with multiple lines
by johngg (Canon) on Feb 04, 2009 at 19:11 UTC

    You can also use a HEREDOC but interpolation still occurs so the backslash needs escaping.

    $ cat > testfile line 1 line 2 line 3 line 4 line 5 $ perl -e ' > $result = <<`EOC`; > tail -2 \\ > testfile > EOC > print $result;' line 4 line 5 $

    I hope this is of interest.

    Cheers,

    JohnGG

Re: Backtics execution with multiple lines
by kennethk (Abbot) on Feb 04, 2009 at 17:54 UTC

    Since backticks interpolate variables, one straight-forward solution would be to store your desired system command in a string and then execute it. Perhaps something like this:

    my $string = 'This will all be '; $string .= 'one string.'; `$string`;

    You could also use the system command, which would let you split your command and arguments into terms in an array, which is then passed to the OS. You should also take a look at exec. Missed the return string requirement - sorry.

      Backticks capture output, but system doesn't. Using exec directly is too much work. open '|-' would be the appropriate choice here.

      $ perl -e' > my @cmd = ( echo => "foo\nbar" ); > open(my $fh, "|-", @cmd); > print <$fh>; > ' foo bar

      Bonus: The shell isn't involved if the command has an argument.

Re: Backtics execution with multiple lines
by leocharre (Priest) on Feb 04, 2009 at 19:20 UTC

    When I have situations like this, that I want to run various bash commands, I start questioning if my perl calling bash, shouldn't be turned into bash calling perl.

    When I get here, I want to step back, break down my perl into smaller scripts, and use bash creatively to call them up.