Search code examples
perl

reverse doesn't use $_ implicitly in list context, is this a bug?


@array = reverse; 
and 
@array = reverse $_;

Both are different. @array = reverse doesn't use $_ implicitly. We have to declare $_ explicitly. It's a very strange case where $_ is not being used by default. Is it a bug?


Solution

  • As far as I understand from the reverse documentation, reverse works on arrays, and so it should use @_ rather than the scalar $_?

    The documentation says "Used without arguments in scalar context, reverse() reverses $_." [Emphasis added]

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