yistaaa has asked for the wisdom of the Perl Monks concerning the following question:

I'm quite new to perl and there are some confusing things about it that I can't seem to find any information on For example: sub some_thing { return (); } What does that return statement do? Is it a ref to something?

Replies are listed 'Best First'.
Re: What does return() mean?
by pvaldes (Chaplain) on Oct 27, 2014 at 22:36 UTC

    Well, a more interesting question could be what is the difference between saying 'return ()' or just 'return'.

    If you do not provide a return last line, your function will exit with an empty list in list context: '()'. If you provide a value (or an appropriate expression like: print "2") you exit with this value, examples: return 2 -> 2; return ()-> the empty list

    So your function is the same as: sub myfunc{}; that does... nothing:

    perl -e 'sub myfunc{}; my @var = (1,2,3,4); myfunc(@var); print join(",",@var),"\n"'

    perl -e 'sub myfunc{return}; my @var = (1,2,3,4); myfunc(@var); print join(",",@var),"\n"'

    perl -e 'sub myfunc{return ()}; my @var = (1,2,3,4); myfunc(@var); print join(",",@var),"\n"'

    perl -e 'sub foo{return ()}; my %hash = (1 => 2, 3 => 4); foo(%hash); while (my ($k, $v)=each %hash){print "$k,$v\n"};'

    But, such "nothing" functions can still do something:

    perl -e 'my @var = (1,2,3,4); sub baz {}; @var = baz(); print "po", jo +in(",",@var),"of!\n";' perl -e 'sub foo{return ()}; my %hash = (1 => 2, 3 => 4); %hash = foo( +); while (my ($k, $v)=each %hash){print "$k,$v\n"};'

    you can use undef $var for the same purpose

      I find this a highly confusing explanation. Better yistaaa should read the return doc and perlsub (especially the fourth and fifth paragraphs of the DESCRIPTION section), and then come back and take another crack at this post.

Re: What does return() mean?
by Anonymous Monk on Oct 27, 2014 at 21:22 UTC
Re: What does return() mean?
by hippo (Archbishop) on Oct 27, 2014 at 21:24 UTC

    Like all Perl functions, return is documented. Have a read of that and see if it answers your questions.

      It doesn't answer my question... I want to know what return () does... For example, if I'm correct, return [] returns an array ref right? So, does return () return a subroutine ref?

        It does the same as return;, which is an empty list in list context, and the return value evaluates to undef in scalar context.


        Dave

        If you actually want to return a code/subroutine reference see sub or the  \ operator (see Symbolic Unary Operators in perlop)

        c:\@Work\Perl>perl -wMstrict -le "sub S { return sub { print qq{hiya $_[0]}; }; } ;; my $coderef = S(); $coderef->('sailor') " hiya sailor

        It doesn't answer my question..

        this is because you have not formulated the right question, as the agent Spooner in I robot, dear.

        Consider this other formulation: 'What is the difference between return (); and return [];?' For completness you have to know there is also return {}; that returns a reference to an anonymous hash.

        HtH
        L*
        There are no rules, there are no thumbs..
        Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: What does return() mean? the true question revealed
by Discipulus (Canon) on Oct 28, 2014 at 08:20 UTC
    the only true question here was not formulated by the OP.
    There is something not so easy to spot in our beloved language. At least i needed quite a bit to understand this clearely.

    There are lists. Lists are unordered collections of values themselves. Lists are declared as () or qw(). Sometimes happens that we store lists in arrays, the container that holds an ordered list. (intermediate Perl, Chapter 3).

    so return (); or simply return; means 'please Perl consider i want to return an empty list'. while return [] means something like: 'Dear Perl, i've just spawned a new anonymous array, he kindly give back to me a reference to himself, can you return that reference? Thanks.'

    Just to be sure of the terminolgy i searched here around and i found this old and interesting thread where a bounce of Perl magicians (not the OP) debate this topic in detail: Scalars, Lists, and Arrays.

    HtH
    L*
    update: as dsheroh said i had to say ordered instead of unordered (what my poor english would express was inaccesible via index).
    Correct the sentence. The original is in the post below.

    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
      Lists are unordered collections of values themselves.
      I believe you have a stray "un". Lists are ordered.
      Lists are declared with the comma operator in list context or qw(). The fact that the assignment operator has a higher precedence than the comma operator makes parenthesis hard to avoid when constructing a list.
      TJD