http://www.perlmonks.org?node_id=596816


in reply to Re^4: Module Announcement: Perl-Critic-1.01 (just a scalar)
in thread Module Announcement: Perl-Critic-1.01

Because it's the calling context that matters!

Bare return; does the right thing in both scalar and list contexts. In my mind, that's a good thing.

If you find yourself in an ambiguous situation, then you disambigate by being explicit. It would be nice if there were never ambiguous code, but that's not realistic.

Replies are listed 'Best First'.
Re^6: Module Announcement: Perl-Critic-1.01 ("right")
by tye (Sage) on Jan 27, 2007 at 02:58 UTC
    Bare return; does the right thing in both scalar and list contexts.

    I'm assuming that you are defining "the right thing" as "being false". For many functions, the "right thing" even in a list context is "return one scalar". Next we'll replace NaN with an empty list for the math functions.

    If you aren't assigning the list (return value), then the boolean nature of what is returned doesn't matter (well, if you are just checking the return value, then you aren't in a list context and return undef; works fine). Do you find yourself writing a lot of code where you assign a single scalar to an array and want to test that assignment in a boolean context? I don't, and I think there are good reasons why I don't.

    Do you find yourself constructing lists from several scalar expressions where the size of the resulting list should not change? I do. Quite a bit, actually. It is a natural thing to do in Perl. So things that return a scalar should return a scalar.

    Doing the "right thing" in a case that matters and is natural is much more important to me than doing the "right thing" in a case that I find doesn't matter (because it is unnatural).

    - tye        

      I'm assuming that you are defining "the right thing" as "being false".

      Yes, I am--for those functions where their return value should be unambiguously false when it's false.

      I should have been much more specific about that earlier.