J
J. Romano
Hi,
I am posting this message because I am a little confused on what
the scalar() function returns when called with functions that return
lists and/or arrays.
I know that calling scalar() on a list returns the last element of
a list, and that calling scalar() on an array returns the number of
elements in the array, but what if scalar() is called on a list that
ends with an array?
To clarify, consider the following code:
#!/usr/bin/perl -w
use strict;
# f1() returns a list
sub f1
{
return ('red', 'green', 'blue', 'yellow');
}
# f2() returns an array
sub f2
{
my @array = ('cat', 'dog', 'bird');
return @array;
}
# f3() returns a list with an array
sub f3
{
my @array = ('cat', 'dog', 'bird');
return ('red', 'green', 'blue', 'yellow', @array);
}
print "f1() returns ", scalar( f1() ), "\n";
print "f2() returns ", scalar( f2() ), "\n";
print "f3() returns ", scalar( f3() ), "\n";
__END__
It's easy to see that scalar( f1() ) returns "yellow" (the last
element of the list), and that scalar( f2() ) returns 3 (the number of
elements in its array. But what does scalar( f3() ) return?
You might think it either returns "bird" (the last element of the
array) or 7 (the number of elements in the list), but it returns
neither. In fact, it returns 3, apparently the number of elements in
the @array. This seems odd to me because it looks like that the
scalar() call is being called twice: Once to single out the @array,
and the next time to count the number of elements in the @array.
Does this sound logical, or is what I'm doing considered bad coding
practice and should never be used? If someone can clarify why what
I'm getting is logical, that would be appreciated.
-- Jean-Luc
I am posting this message because I am a little confused on what
the scalar() function returns when called with functions that return
lists and/or arrays.
I know that calling scalar() on a list returns the last element of
a list, and that calling scalar() on an array returns the number of
elements in the array, but what if scalar() is called on a list that
ends with an array?
To clarify, consider the following code:
#!/usr/bin/perl -w
use strict;
# f1() returns a list
sub f1
{
return ('red', 'green', 'blue', 'yellow');
}
# f2() returns an array
sub f2
{
my @array = ('cat', 'dog', 'bird');
return @array;
}
# f3() returns a list with an array
sub f3
{
my @array = ('cat', 'dog', 'bird');
return ('red', 'green', 'blue', 'yellow', @array);
}
print "f1() returns ", scalar( f1() ), "\n";
print "f2() returns ", scalar( f2() ), "\n";
print "f3() returns ", scalar( f3() ), "\n";
__END__
It's easy to see that scalar( f1() ) returns "yellow" (the last
element of the list), and that scalar( f2() ) returns 3 (the number of
elements in its array. But what does scalar( f3() ) return?
You might think it either returns "bird" (the last element of the
array) or 7 (the number of elements in the list), but it returns
neither. In fact, it returns 3, apparently the number of elements in
the @array. This seems odd to me because it looks like that the
scalar() call is being called twice: Once to single out the @array,
and the next time to count the number of elements in the @array.
Does this sound logical, or is what I'm doing considered bad coding
practice and should never be used? If someone can clarify why what
I'm getting is logical, that would be appreciated.
-- Jean-Luc