return ()

----ALT--zVCoa2Sn1297387309
Content-Type: text/plain; charset=koi8-r
Content-Transfer-Encoding: 8bit

does "return ()" mean return an empty list, or just mean "return" since "()" is optional in Perl?

thanks!

----ALT--zVCoa2Sn1297387309--
terry peng [ Fr, 11 Februar 2011 02:21 ] [ ID #2055009 ]

Re: return ()

2011/2/10 terry peng <terry.peng [at] mail.ru>:
> does "return ()" mean return an empty list, or just mean "return" since "()" is optional in Perl?

I would have guessed an empty list, but the best way to find out is to
check. :) According to my tests, it's returning according to context:

#!/usr/bin/env perl

use strict;
use warnings;

use Data::Dumper;

sub test1
{
return ();
}

sub test2
{
return;
}

sub test3
{
my [at] empty_array = ();

return [at] empty_array;
}

my $scalar1 = test1;
my $scalar2 = test2;
my $scalar3 = test3;

my [at] array1 = test1;
my [at] array2 = test2;
my [at] array3 = test3;

print "\$scalar1: ", Dumper \$scalar1;
print "\$scalar2: ", Dumper \$scalar2;
print "\$scalar3: ", Dumper \$scalar3;

print "\ [at] array1: ", Dumper \ [at] array1;
print "\ [at] array2: ", Dumper \ [at] array2;
print "\ [at] array3: ", Dumper \ [at] array3;

__END__

Results:

$scalar1: $VAR1 = \undef;
$scalar2: $VAR1 = \undef;
$scalar3: $VAR1 = \0;
[at] array1: $VAR1 = [];
[at] array2: $VAR1 = [];
[at] array3: $VAR1 = [];

So it seems the only time an empty list is returned in scalar context
is when I returned an empty array. I think that `return ();` is
returning undef in scalar context, just as `return;` does.

--
Brandon McCaig <http://www.bamccaig.com/> <bamccaig [at] gmail.com>
V zrna gur orfg jvgu jung V fnl. Vg qbrfa'g nyjnlf fbhaq gung jnl.
Castopulence Software <http://www.castopulence.org/> <bamccaig [at] castopulence.org>

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Brandon McCaig [ Fr, 11 Februar 2011 02:44 ] [ ID #2055010 ]

Re: return ()

>>>>> "BM" == Brandon McCaig <bamccaig [at] gmail.com> writes:

BM> I would have guessed an empty list, but the best way to find out is to
BM> check. :) According to my tests, it's returning according to context:

this is all well documented. and i posted an explanation earlier in the
thread.


BM> sub test1
BM> {
BM> return ();
BM> }

BM> sub test2
BM> {
BM> return;
BM> }

those two are the same. period. the parens are only to group an
expression if any. there is NO forming of a list by those parens.

BM> sub test3
BM> {
BM> my [at] empty_array = ();

BM> return [at] empty_array;
BM> }

that is very different than the above two.

BM> Results:

BM> $scalar1: $VAR1 = \undef;
BM> $scalar2: $VAR1 = \undef;
BM> $scalar3: $VAR1 = \0;
BM> [at] array1: $VAR1 = [];
BM> [at] array2: $VAR1 = [];
BM> [at] array3: $VAR1 = [];

BM> So it seems the only time an empty list is returned in scalar context
BM> is when I returned an empty array. I think that `return ();` is
BM> returning undef in scalar context, just as `return;` does.

nope. there is no such thing as a list in scalar context. you just can't
have it by definition. the array is returned in scalar context and it
has zero elements so the return value is 0. the rule is very simple: the
return expression is evaluated in the caller's context. it is just as if
you took the return expression and put it in an assignment.

these do the same thing:

$scalar = test1() ;
$scalar = () ;

and so do these:

$scalar = test3() ;
$scalar = [at] empty_array ;

uri

--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Uri Guttman [ Fr, 11 Februar 2011 04:20 ] [ ID #2055012 ]

Re: return ()

On Thu, Feb 10, 2011 at 10:20 PM, Uri Guttman <uri [at] stemsystems.com> wrote:
> this is all well documented. and i posted an explanation earlier in the
> thread.

I didn't realize this was an existing thread. In Google Mail it
appeared as a new thread.

> =C2=A0BM> sub test3
> =C2=A0BM> {
> =C2=A0BM> =C2=A0 =C2=A0 my [at] empty_array =3D ();
>
> =C2=A0BM> =C2=A0 =C2=A0 return [at] empty_array;
> =C2=A0BM> }
>
> that is very different than the above two.

The point was to compare the results. It wasn't supposed to be the same. :-=
/

> have it by definition. the array is returned in scalar context and it
> has zero elements so the return value is 0. the rule is very simple: the
> return expression is evaluated in the caller's context. it is just as if
> you took the return expression and put it in an assignment.

I thought that Perl couldn't return arrays. I thought it could only
return scalars and lists. And so I believed that returning an empty
array would in fact return an empty list. The point was for that empty
list to become 0 in scalar context, which could be compared to the
subroutine that returned with `return ();`. The fact that the `return
();` example returned undef instead indicated to me that `return ();`
was equivalent to `return;`.

> these do the same thing:
>
> $scalar =3D test1() ;
> $scalar =3D () ;

I would have expected 0 from the latter of the two. :-/ I guess I
don't understand how Perl works after all. Better I find out this way
though. :P


--
Brandon McCaig <http://www.bamccaig.com/> <bamccaig [at] gmail.com>
V zrna gur orfg jvgu jung V fnl. Vg qbrfa'g nyjnlf fbhaq gung jnl.
Castopulence Software <http://www.castopulence.org/> <bamccaig [at] castopulence=
..org>

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Brandon McCaig [ Fr, 11 Februar 2011 04:57 ] [ ID #2055013 ]

Re: return ()

>>>>> "BM" =3D=3D Brandon McCaig <bamccaig [at] gmail.com> writes:

BM> On Thu, Feb 10, 2011 at 10:20 PM, Uri Guttman <uri [at] stemsystems.com> w=
rote:
>> this is all well documented. and i posted an explanation earlier in the
>> thread.

BM> I didn't realize this was an existing thread. In Google Mail it
BM> appeared as a new thread.

>> =A0BM> sub test3
>> =A0BM> {
>> =A0BM> =A0 =A0 my [at] empty_array =3D ();
>>
>> =A0BM> =A0 =A0 return [at] empty_array;
>> =A0BM> }
>>
>> that is very different than the above two.

BM> The point was to compare the results. It wasn't supposed to be the sa=
me. :-/

but returning an array was never mentioned before. it is a very
different beast.

>> have it by definition. the array is returned in scalar context and it
>> has zero elements so the return value is 0. the rule is very simple: t=
he
>> return expression is evaluated in the caller's context. it is just as =
if
>> you took the return expression and put it in an assignment.

BM> I thought that Perl couldn't return arrays. I thought it could only
BM> return scalars and lists. And so I believed that returning an empty
BM> array would in fact return an empty list. The point was for that empty
BM> list to become 0 in scalar context, which could be compared to the
BM> subroutine that returned with `return ();`. The fact that the `return
BM> ();` example returned undef instead indicated to me that `return ();`
BM> was equivalent to `return;`.

as is documented and as i said. the () are meaningless there. they just
group. as i keep saying parens in general in perl are for grouping and
syntax and not much else (to most newbie's surprise). they don't make
lists. they are useful to delineate where a list is (that is grouping).

>> these do the same thing:
>>
>> $scalar =3D test1() ;
>> $scalar =3D () ;

BM> I would have expected 0 from the latter of the two. :-/ I guess I
BM> don't understand how Perl works after all. Better I find out this way
BM> though. :P

undef is not 0.

uri

--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com =
--
----- Perl Code Review , Architecture, Development, Training, Support ----=
--
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com -------=
--

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Uri Guttman [ Fr, 11 Februar 2011 05:46 ] [ ID #2055014 ]
Perl » gmane.comp.lang.perl.beginners » return ()

Vorheriges Thema: print $myhash{key} gets SCALAR(0x1b8db540) instead of "0"
Nächstes Thema: about return