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

Hi. I have a simple question which probably has a simple answer as well.

I'm fetching the output from a shell command using backticks, but the command argument is too long to prettily fit on one line of my Perl script. How can I conveniently break it into multiple lines?

As a workaround, I can do this:

$query = "command argumentpart1" . "argumentpart2"; $result = `$query`;
However, that's two commands - I'd like to do it in one. I could do this:
$result = `command argumentpart1\\ argumentpart2`;
But that breaks the indentation! (Assume that spaces are not allowed between argumentpart1 and argumentpart2).

Perhaps I could build something using string concatenation in the shell, but that's pretty ugly. I assume there is some neat way to solve this that I just haven't been able to figure out.

Grateful for ideas.

Replies are listed 'Best First'.
Re: Line breaks within backticks
by Arunbear (Prior) on May 08, 2008 at 11:23 UTC

      Excellent! Thank you. I tried similar constructs, but didn't find the "${\(...)}" combination. So what we're doing is creating a reference and then dereferencing it, right? It seems a bit backward, but... if it works, it works.

      And I agree, the two-step solution is probably prettier, but I'm trying to write compact code for a coding contest, so fewer lines is more important in this case.

Re: Line breaks within backticks
by casiano (Pilgrim) on May 08, 2008 at 11:12 UTC
    This solution allows indentation and "syntactically" is still one statement

    lhp@nereida:~/Lperl/src/testing$ cat -n qx.pl 1 print ( 2 $c = 'ls -l '. 3 join(' ', qw{ 4 context1.pl 5 decomposed_char_utf8_2.pl 6 file1.txt 7 es.log} 8 ) 9 and qx{$c} 10 );

    substitute join ' ' by join '' to assume the restriction that spaces aren't allowed between args.

    Casiano

      Better yet, use the multiple argument form to avoid (lack of) escaping problems and to simplify the code.

      my @cmd = ( 'ls', '-l', 'file1', 'file2', 'file with space', ); my $results = do { open(my $fh, '-|', @cmd) or die("Unable to run $cmd[0]: $!\n"); local $/; <$fh> };

      PS - Why did you use and there instead of making two statements?!

      Update: Fixed wrong variable name.

      Nice solution, but it turns into even more code, which kind of negates the point. :)
Re: Line breaks within backticks
by rovf (Priest) on May 09, 2008 at 10:04 UTC
    You could also use a backtick'ed HERE document:
    my $result=<<`END`; command arg1 arg2 END
    As for keeping the structure intended properly, the spaces in front of the command usually provide no problem, as long as one argument fits on one line. Less nice is the fact that you have to use the END starting in column 1 (how I wished that Perl would have an option which would ignore the spaces in front of the terminating line). There is a workaround to this, but I consider it ugly:
    my $result=<<` END`; # 4 spaces in front of the END command arg1 arg2 END
    -- 
    Ronald Fischer <ynnor@mm.st>