Question

Take a look at one of the print subroutines of perl:

print FILEHANDLE LIST

(http://perldoc.perl.org/functions/print.html)

I tried it out and it seems that you can also pass a scalar instead of a list. Is that a general rule in perl that wherever a list is demanded you can pass in a scalar? Is the scalar automatically converted to a list then?

Thank you in advance.

Was it helpful?

Solution 2

You can only pass a list of scalars to some function in perl.

Is that a general rule in perl that wherever a list is demanded you can pass in a scalar?

Yes, in such case you have one element long list.

print() takes ($one, $two, $three) as list in same way as @arr, or map $_, @arr, or $arr[0], $arr[1], $arr[2]

OTHER TIPS

LIST refers to a an expression that returns a list of zero or more scalars. It's basically the same thing as EXPR, except with the implication that it's called in list context.

print($fh ());         # "()" is a placeholder that avoids the default to $_
print($fh $foo);
print($fh $foo, $bar);
print($fh @array);
print($fh map uc, @a);

etc

There is no list data structure. It's just a concept and metaphor.

Within a subroutine, Perl assumes that you called the subroutine with a list of arguments.

That's why, if your subroutine is called

my_subroutine ($a, $b, $c);

within the subroutine, you can do

sub my_subroutine
{
    my $x = shift;
    my $y = shift;
    my $z = shift;
...
    #Or, my ($x, $y, $z) = @_;

By calling shift, you're implicitly calling shift on @_, which is an array of the passed in arguments.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top