Yes, I know, we don't typecast in Perl. But I got to thinking about the issue of context imposition.
#!/usr/bin/perl use strict; use warnings; use Perl6::Say; # print with trailing newline my $lunch ; my @lunch ; my %lunch ; sub lunch { return ( 'Beans', 'Burger' ); }; say '1: ', lunch(); $lunch = lunch(); say '2: ', $lunch; $lunch = ( 'Pork', 'Salad' ); say '3: ', $lunch; @lunch = ( 'Rice', 'Noodles' ); say '4: ', @lunch; $lunch = @lunch; say '5: ', $lunch; %lunch = ( 'Chicken', 'Steak'); say '6: ', %lunch; $lunch = %lunch; say '7: ', $lunch; @lunch = %lunch; say '8: ', @lunch; $lunch = @lunch; say '9: ', $lunch; __DATA__ Output: Useless use of a constant in void context at ./return-demo.pl line 20. 1: BeansBurger 2: Burger 3: Salad 4: RiceNoodles 5: 2 6: ChickenSteak 7: 1/8 8: ChickenSteak 9: 2 __END__
My explanation for most of that output is:
  1. lunch() returns a two-element list
  2. $lunch forces scalar context on the returned list, getting the last element
  3. $lunch forces scalar context on a literal list, getting the last element
    and raising a warning ('Pork' is thrown away, hence useless)
  4. @lunch is an array and say() prints its contents
  5. $lunch forces scalar context on an array, @lunch, getting the number of elements
  6. %lunch is flattened (by parameter passing) into a list; contents printed
  7. ?
  8. %lunch is flattened (by assignment to @lunch) into a list
  9. $lunch forces scalar context on @lunch, getting the number of elements
The output 7: 1/8 baffles me. I expected anything else.

In reply to Typecasting Hashes by Xiong

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.