in reply to Elegant way to parse an optional prefix with default?

$name =~ s/^[$@%&<*]//; my $sigil= $1 || '&';
Actually, that doesn't work. You don't have a $1 there. If you added the parens to make it work, I still don't quite see how to get it into one line. Nor would I worry about that.

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

Replies are listed 'Best First'.
Re: &bull;Re: Elegant way to parse an optional prefix with default?
by John M. Dlugosz (Monsignor) on Nov 08, 2002 at 05:32 UTC
    Hmm, it looks like (with the parens, sorry about the typo) $1 is not undef but left to an old value if there is no match?!

    So I do need to put something on the first line to test for success failure, at least. (or update the regex to make $1 success but blank)

    My ugly one line is:

    my $sigil= ($name =~ s/^([$@%&<*])//) ? $1 : '&';
    as I couldn't get it to work with my ($sigil)= ... in list context to return matches. I guess that doesn't work with replacements as opposed to matches.

    —John