in reply to Truncate a string (news lead?)

sub foo{($_[0]=~/^(.+[!?.]\s)/s)?$1:$_[0]}
Update: Fixed (twice) according to merlyn's correction(s).

Replies are listed 'Best First'.
Re: Re: Truncate a string (news lead?)
by merlyn (Sage) on Sep 12, 2001 at 19:13 UTC
      except my example truncates @ any number of starting characters, not just the first sentence.
Re: Re: Truncate a string (news lead?)
by merlyn (Sage) on Sep 13, 2001 at 12:18 UTC
    ($_[0]=~/^(.+[!?.]\s)/s)&&$1||$_[0]
    Do not use XX && YY || ZZ when you mean XX ? YY : ZZ, or the day your YY returns false, you will be kicking yourself.

    I don't know where this idiom started, but please do not propagate it.

    -- Randal L. Schwartz, Perl hacker

      /me kicks himself.

      You're right, though for a different reason. It seems to me that, in this particular case, YY ($1) cannot be false, as it contains at least one character, then one of !?., then a whitespace character. Which cannot be false. Or, if the match fails, it contains nothing and therefore is false as it is supposed to be. Usually.

      However, it might contain the $1 from a previous match, which makes you right. Again.

        You're right that it probably would not have made any difference in this particular case.

        But that's the problem. I keep seeing that meme pop up, being used by people who don't understand that it is not "if-then-else" but something that acts a bit like it most of the time.

        So, I can't let one like that go by without comment. Since ?: is actually shorter to type, I'd greatly prefer that this other ugly thing be eliminated and the proper operator introduced at every opportunity.

        -- Randal L. Schwartz, Perl hacker

        I was looking at this last night and came to pretty much the same conclusion... the potential for a nasty bug is there (hence the 'XX && YY || ZZ' construct should be avoided), but in this specific case I don't see it happening.

        However, it might contain the $1 from a previous match...

        In that case, the regex failed to match, so you never evaluate the $1 anyway. (i.e. XX is false, so even if YY contains something unexpected, we never actually get tripped up by it)

        -Blake