The single biggest STUPIDITY in Perl ...

J

Jim Gibson

Uri Guttman said:
BC> } but the number of differences are more common:
BC> }
BC> } arrays lists
BC> } ------ -----

BC> You left out:
BC> evaluates to # of elements evaluates to last element
BC> in scalar context in scalar context

incorrect. there is no such thing as a list in scalar context. it is
just a series of comma ops in that situation, no list is ever created.

Here is something to ponder:

#!/usr/bin/perl

use strict;
use warnings;

sub returns_a_list
{
return ('a', 'b', 'c');
}

sub returns_an_array
{
my @r = ('d', 'e', 'f');
return @r;
}

my @a = returns_a_list();
print "a=@a\n";
my $x = returns_a_list();
print "x=$x\n";
my @b = returns_an_array();
print "b=@b\n";
my $y = returns_an_array();
print "y=$y\n";

__OUTPUT__

a=a b c
x=c
b=d e f
y=3

Note the difference between the values assigned to x and y!
 
U

Uri Guttman

BC> You left out:
BC> evaluates to # of elements evaluates to last element
BC> in scalar context in scalar context
JG> Here is something to ponder:

JG> sub returns_a_list
JG> {
JG> return ('a', 'b', 'c');
JG> }

JG> sub returns_an_array
JG> {
JG> my @r = ('d', 'e', 'f');
JG> return @r;
JG> }

JG> my @a = returns_a_list();
JG> print "a=@a\n";
JG> my $x = returns_a_list();
JG> print "x=$x\n";
JG> my @b = returns_an_array();
JG> print "b=@b\n";
JG> my $y = returns_an_array();
JG> print "y=$y\n";

JG> a=a b c
JG> x=c
JG> b=d e f
JG> y=3

JG> Note the difference between the values assigned to x and y!

sure. and that defends my point. the context of the call determines what
the sub returns. the context says list or scalar and that propogates to
the return code. no different than saying @x = (1, 2, 3 ) vs $x =
(1,2,3). this is documented behavior.

uri
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top