in reply to Regex Or Die!

I'm not supposed to use unless.
I'd say you can use it whenever you want :) Even if I mildly agree with PBP for this issue, I loved Perl also because of unless (even if I use it only as statement modifier, another thing that PBP discourages). Continuing on PBP, why don't you simply negate the if?
if (! /^(foo|bar)/) { die "in pain"; }
Or use the statement modifier (suitable for this one-instruction case)?
die "whatever" unless /^(foo|bar)/;

Flavio
perl -ple'$_=reverse' <<<ti.xittelop@oivalf

Don't fool yourself.

Replies are listed 'Best First'.
Re^2: Regex Or Die!
by ChrisCantrall (Sexton) on Sep 27, 2006 at 00:48 UTC
    die "whatever" unless /^(foo|bar)/;

    I like that it's hard to miss a die with this method. It's the first word on the line, the die jumps out and screams "This condition is important!"

    Which also means that this style can get distracting if you're doing a lot of sanity checks inline. So, that's a good reason to encapsulate your sanity checks in subroutines.

    $user_number = &validate_user_number($input)

    is cleaner than

    die "Numbers only please." unless ($input =~ /[0-9]+/); $user_number = $input;

    Plus, with a subroutine, you can add more checks without making the usage of that sub any harder to understand.

Re^2: Regex Or Die!
by bart (Canon) on Sep 30, 2006 at 15:00 UTC
    I agree with frodo72 in favour of unless, using !~ just because you want to avoid unless, is a most hidious practice.

    !~ is even far more obscure than unless, and you should most definitely not start using it for frivolous reasons. Is there something in PBP about it? If it isn't, I'd add a Best Practices rule of my own:

    Don't use !~