2

Regarding below code, how does $one_sub and $two_sub become coderef's to the anonymous subroutines within named sub "sup"? The named sub isn't 'returning' those two anon subs; or is it? (at least I haven't put such a statement).

sub sup {

          my $neh = sub {
                  say "this is 'neh' subroutine"
          };

          my $hen = sub {
                  say "this is 'hen' subroutine"
         };
          ($neh, $hen);
  }

  my ($one_sub, $two_sub) = ⊃

Using Data::Dumper::Streamer show's :

$CODE1 = sub {
           use warnings;
           use strict;
           no feature;
           use feature ':5.10';
           say q[this is 'neh' subroutine];
         };
$CODE1 = sub {
           use warnings;
           use strict;
           no feature;
           use feature ':5.10';
           say q[this is 'hen' subroutine];
         };
5
  • 4
    perldoc -f return: "In the absence of an explicit return, a subroutine, eval, or do FILE automatically returns the value of the last expression evaluated." Commented Jun 25, 2015 at 14:25
  • 1
    @ThisSuitIsBlackNot should probably be an answer. Commented Jun 25, 2015 at 14:26
  • This is one of the reasons why explicit returns are a good thing :) Commented Jun 25, 2015 at 14:52
  • 2
    Tip: Don't prefix sub calls using &. That means "ignore any prototype on the sub". Unless that's what you meant to do, of course. Commented Jun 25, 2015 at 14:59
  • Also makes some potentially rather interesting unexpected behaviour when you pass through and tamper with @_. Commented Jun 25, 2015 at 21:22

2 Answers 2

8

To quote perlsub:

If no return is found and if the last statement is an expression, its value is returned.

($neh, $hen); is an expression. In list context, its value is the 2 anonymous subs.

Sign up to request clarification or add additional context in comments.

Comments

2

Yes it is returning:

 ($neh, $hen);

In Perl the latest evaluated statement is returned. You don't need to explicitly call:

 return ($neh, $hen);

http://perldoc.perl.org/functions/return.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.