There is nothing returning by the subroutine

Yes, in your first example there is - it's the return value of the print, as per the piece of documentation you quoted ("If no return is found and if the last statement is an expression, its value is returned.").

Note that Perl::Critic / PBP has this to say about the issue:

Subroutines without explicit return statements at their ends can be confusing. It can be challenging to deduce what the return value will be.

Furthermore, if the programmer did not mean for there to be a significant return value, and omits a return statement, some of the subroutine's inner data can leak to the outside. Consider this case:

package Password; # every time the user guesses the password wrong, its value # is rotated by one character my $password; sub set_password { $password = shift; } sub check_password { my $guess = shift; if ($guess eq $password) { unlock_secrets(); } else { $password = (substr $password, 1).(substr $password, 0, 1); } } 1;

In this case, the last statement in check_password() is the assignment. The result of that assignment is the implicit return value, so a wrong guess returns the right password! Adding a `return;' at the end of that subroutine solves the problem.

The example is a little contrived, but another argument I have read about implicit returns is this: If you don't specify an explicit return, the users of your module's API may get used to your sub returning a value, for example they may assume that like many Perl functions a true value indicates success, and if their code starts relying on that, they may be surprised when your code, which never intended to return any value, suddenly changes its behavior. Another viewpoint: An explicit return tells whomever is reading your code that yes, you did intend to return exactly that value.

A common counter-argument is "but I know I'm only ever calling this sub in void context", which certainly can be true - until someone else starts working with your code :-)

Having said all that, I've used the implicit return plenty of times, but usually only for brevity, as in when I'm writing a 1-to-3 line sub that I want to keep short. Otherwise I tend to agree with Perl::Critic that it's better to be explicit, and that includes return; for when I don't want my sub to return anything; I can always update the API later and add a useful return value.


In reply to Re: Return statement in subroutine(s) is necessary when not returning anything? by Anonymous Monk
in thread [SOLVED] Return statement in subroutine(s) is necessary when not returning anything? by thanos1983

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.