#!/usr/bin/perl
use Modern::Perl;
while (<>)
{ chomp;
say reverse;
}
The above code doesn’t work but when I change 2nd last line to say scalar reverse; then it works fine. Why do I need to force it to be a scalar explicitly? Can’t Perl DWIM?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If I understand the documentation right,
reversenormally operates on lists. In a list context used without arguments, it returns an empty list and by default doesn’t assign it anywhere. In your example, say outputs the unchanged$_;Forcing
reverseinto scalar context changes its behaviour and makes it reverse character strings, and use$_by default. Becausesaycan be used to print lists as well as scalars, it doesn’t force its arguments into scalar context.Perl probably does DWIM, just for given values of “I”.
A breakdown of what reverse does when: