in reply to Re: Validating Regular Expression
in thread Validating Regular Expression

I think we can both agree that the interface you specify should depend on how you are going to use it.

That's entirely my point. One would use the result of validate as a boolean, so it should return a boolean. You suggested that it should return something that needs to be converted into a boolean first.

Use a bare return to return failure

Return an empty list can be useful to signal failure where a false value could be returned.

sub get { $engine->connect() or return (); return $engine->get(); } my $ok = ($data) = get();

That's not relevant here.

Replies are listed 'Best First'.
Re^3: Validating Regular Expression
by kennethk (Abbot) on Feb 15, 2011 at 17:53 UTC
    It only needs to be converted to a boolean first if it is used in a list assignment. Any usage of the sub in scalar context will behave as expected and as the original spec. We are both making assumptions about how "[o]ne would use the result", and any conclusions are subjective and academic.

      We are both making assumptions about how "[o]ne would use the result",

      Not at all. I'm saying you shouldn't introduce bugs in the design of a function based on the assumption that someone might misuse it.

      If you can handle cases of misuse without compromising the design, that's great, but that's not possible here.

      Any usage of the sub in scalar context will behave as expected

      Correct. «return ();» in scalar context functions identically to «return undef;». That makes the behaviour in list context the deciding factor.

      It only needs to be converted to a boolean first if it is used in a list assignment.

      No. Here's an example without an assignment:

      { foo => validate($foo), bar => validate($bar) }

      When using «return ();», the result needs to be converted to a boolean whenever validate is called in list context. That makes no sense for a function that's suppose to return a boolean.