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

Hi,

How will you change this long line into something like a 2 line code?
my $test = qx! testing testing testing ... testing testing !; into something like... my $test = qx! testing testing ! . qx! ... testing testing !;

Replies are listed 'Best First'.
Re: Formatting Long String in Backticks
by toolic (Bishop) on Mar 09, 2010 at 23:46 UTC
    Your 1st code will execute a single external command, but your 2nd will execute two external commands.

    I bet you really want something like this:

    my $cmd = qq! testing testing ! . qq! ... testing testing !; my $test = qx! $cmd !;
      I am trying to avoid to do what you just did. If this can be written elegantly like this.
      my $cmd = qq! testing testing ! . qq! ... testing testing !;
      then why can't this code be re-written elegantly as well?
      my $test = qx! testing testing ! . qx! ... testing !;
      There must be a way to write the qx where I do not need to use $cmd anymore.
        then why can't this code be re-written elegantly as well?
        Because the dot (.) is defined as catenating strings. Hence,

        qx(a).qx(b)
        is valid, but is defined as catenating the result of qx(a) and the result of qx(b), which is of course different from qx(a b).

        -- 
        Ronald Fischer <ynnor@mm.st>

        Stupid Array Interpolation Trick #158:

        >perl -wMstrict -le "my $test = qx{ @{[ qw(echo testing ...)[0, 1, 1, 1, 2, 1] ]} }; print qq{'$test'}; " 'testing testing testing ... testing '
Re: Formatting Long String in Backticks
by ahmad (Hermit) on Mar 09, 2010 at 23:46 UTC
    Something like this ?
    my $test = q! testing testing ! . q! ... testing testing !; my $cmd = `$test`;
      Keep in mind that q!! will not interpolate, but qx!! will. This is importatnt if testing actually contains variables.
Re: Formatting Long String in Backticks
by johngg (Canon) on Mar 10, 2010 at 23:07 UTC

    If you don't need interpolation, put your long command string into an array instead using the quote-words (qw{ ... } in Quote Like Operators) operator which allows you to break the word list over multiple lines.

    knoppix@Microknoppix:~$ perl -E ' > @cmd = qw{ > ps > -ef > | > grep > gnome > | > grep > -v > grep > }; > $res = qx{ @cmd }; > print $res;' knoppix 3829 1 0 09:40 ? 00:00:01 gnome-terminal knoppix 3834 3829 0 09:41 ? 00:00:00 gnome-pty-helper knoppix 3856 1 0 09:41 ? 00:00:30 gnome-terminal knoppix 3858 3856 0 09:41 ? 00:00:00 gnome-pty-helper knoppix 4006 4005 0 09:43 ? 00:00:00 gnome-pty-helper knoppix@Microknoppix:~$

    Even if you do need interpolation you can use this method by using push and breaking your word list up a bit so that interpolated variables go in the gaps.

    knoppix@Microknoppix:~$ perl -E ' > $lookFor = q{gnome}; > push @cmd, > qw{ > ps > -ef > | > grep > }, > $lookFor, > qw{ > | > grep > -v > grep > }; > $res = qx{ @cmd }; > print $res;' knoppix 3829 1 0 09:40 ? 00:00:02 gnome-terminal knoppix 3834 3829 0 09:41 ? 00:00:00 gnome-pty-helper knoppix 3856 1 0 09:41 ? 00:00:30 gnome-terminal knoppix 3858 3856 0 09:41 ? 00:00:00 gnome-pty-helper knoppix 4006 4005 0 09:43 ? 00:00:00 gnome-pty-helper knoppix@Microknoppix:~$

    I hope this is of use.

    Cheers,

    JohnGG