Default parameter for reverse function in list context in Perl?

94 views Asked by At

As below Perl script shows, it seems $_ is not passed as default parameter to reverse in list context. Why doesn't it just accept $_ as default parameter and do the scalar to list conversion? What's the rule for function's default parameters in both context?

I saw this post, which mentioned @_ should be the default parameter for function which operates in list mode (list reverse, sort), but it doesn't seem to be that case after I prepend @_ = somelist to the below code snippet.

$_ = "dlrow ,olleH";
print reverse;                         # No output, list context
print scalar reverse;                  # Hello, world
1

There are 1 answers

4
ysth On BEST ANSWER

$_ would be a strange default for reverse in list context, since it wouldn't actually do anything:

$_ = "dlrow ,olleH";
print reverse $_;  # outputs: dlrow ,olleH

I don't know what you mean by "scalar to list conversion".

There is no general rule for default parameters for functions, just as there is no general rule for what functions to in list vs. scalar context; you must consult the documentation for each function.