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

I am trying to neaten up a code smippet in the following style:
my $str = "a load of constant stuff..."; $str .= "optional bit in the middle " if $option; $str .= "loads more stuff";
now this goes on in the same vein for a while, with a whole heap of variable interpolation and $str is in fact a messy looking array dereference.

In the process of making this more readable, I have written

my $str = "a load of constant stuff..."; . ($option ? "optional bit in the middle " : undef); . "loads more stuff";
I don't like the look of that 'undef' in the middle of there, but if I change the bit in parentheses for  ( "optional bit in the middle" if $option ) I get a syntax error.

What idiom is there to make this clearer and more readable?

-- iakobski

Replies are listed 'Best First'.
Re: string manipulation question
by how do i know if the string is regular expression (Initiate) on May 18, 2001 at 17:13 UTC
    Actually, you're second code block is bad... but I'm guessing that was just a mistake on entering it here. You have too many ';'s.

    But onto the real question... I don't see any better looking way other then replacing the undef with ''. Like so:

    my $str = "a load of constant stuff..." . ($option ? "optional bit in the middle " : '') . "loads more stuff";
    In fact, if you're using #!/usr/bin/perl -w and use the undef method, you'll get the following warning:
    Use of uninitialized value in concatenation (.) or string at ./pm.pl l +ine 7.
    If $option returns false.

    - FrankG

Re: string manipulation question
by dempa (Friar) on May 18, 2001 at 17:22 UTC
    I dunno if this more readable or not, but anyway:
    my $str = "a load of constant stuff..." . ($option && "optional bit in the middle ") . "loads more stuff";

    update: Ofcourse davorg is correct. Why do I never learn to test my code before posting! :)

    $option = 0; # Prints out the 0, not good. $option = ""; # This works. $option = undef; # Warnings with -w
    /dempa

      Depends what you set $option when you don't want the text included. Try it with 0 and see what happens.

      --
      <http://www.dave.org.uk>

      "Perl makes the fun jobs fun
      and the boring jobs bearable" - me

      of course you could write
      my $str = "a load of constant stuff..." . (defined($option) && "optional bit in the middle ") . "loads more stuff";
        !! does wonders too.
        my $str = "a load of constant stuff..." . (!!$option && "optional bit in the middle ") . "loads more stuff";
Re: string manipulation question
by grinder (Bishop) on May 18, 2001 at 18:09 UTC

    Interpolate the optional bit via a code reference @{[ ... ]} .

    my $str = <<FOO; a load of constant stuff... @{["optional bit in the middle " if defined $option]} loads more stuff FOO

    Beware that as this example stands, you'll get a newline regardless of whether $option is defined or not, but I'm sure you can figure out how to make it do The Right Thing.


    --
    g r i n d e r
      for instance do the newlinine in the ref
      my $str = <<FOO; a load of constant stuff...@{[ "optional bit in the middle " if defined $option ]}loads more stuff FOO