Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

This line is from the famous 'Learning Perl'.

 there's no corresponding function to force list context. It turns out you never need it. Trust us on this, too.

scalar is the function to list scalar context, and there is no function to force list context ?? And we never need it, does somebody came up on a situation that you required that kind of need ?

Replies are listed 'Best First'.
Re: scenarios to force list context
by Corion (Patriarch) on Aug 18, 2010 at 15:45 UTC

    When I have a function that returns a list in list context and something else in scalar context, and I only want the first element, I use the following:

    my ($field) = $mech->xpath( '//div' );
Re: scenarios to force list context
by pemungkah (Priest) on Aug 18, 2010 at 21:48 UTC
    You can "force list context" by constructing a list.
    my($foo, bar) = ($blah =~ /$blurk/);
    That's not exactly forcing it, but establishing it, I guess.

    The statement might be better written "There's no corresponding function to force list context, as the only place you'd need to 'force' list context would be in scalar context, where it has no meaning."

Re: scenarios to force list context
by choroba (Cardinal) on Aug 19, 2010 at 15:17 UTC