in reply to string manipulation question

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

Replies are listed 'Best First'.
Re: Re: string manipulation question
by davorg (Chancellor) on May 18, 2001 at 17:36 UTC

    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

Re: Re: string manipulation question
by Anonymous Monk on May 18, 2001 at 19:06 UTC
    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";
        Aaah! I didn't know that trick. It's been said before and I'll say it again: You learn something new every day on PerlMonks.org. :)