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 | [reply] [d/l] [select] |
| [reply] |
| [reply] |
Like all Perl functions, return is documented. Have a read of that and see if it answers your questions.
| [reply] |
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?
| [reply] |
| [reply] [d/l] |
c:\@Work\Perl>perl -wMstrict -le
"sub S { return sub { print qq{hiya $_[0]}; }; }
;;
my $coderef = S();
$coderef->('sailor')
"
hiya sailor
| [reply] [d/l] [select] |
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? Yes, [ ] is an array ref, and no () is not a reference of any kind (not a subroutine ref) References are explained in perlreftut#Making References , references quick reference
These are all the same (as documente in return, they return nothing (empty list) or undef , depending on context (list or scalar) )
sub roy { return; }
sub roy { return }
sub roy { return(); }
sub roy { return() }
sub roy { return () }
These are all the same (return an array ref)
sub roy { return []; }
sub roy { return [] }
sub roy { return([]); }
sub roy { return([]) }
sub roy { return ([]) }
See Tutorials: Context in Perl: Context tutorial, "List" Is a Four-Letter Word, Easy Reference for Contexts | [reply] [d/l] [select] |
| [reply] [d/l] [select] |
| [reply] [d/l] [select] |
Lists are unordered collections of values themselves.
I believe you have a stray "un". Lists are ordered.
| [reply] |
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
| [reply] |