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

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.

Replies are listed 'Best First'.
Re: Typecasting Hashes
by almut (Canon) on Feb 20, 2010 at 11:03 UTC
    The output 7: 1/8 baffles me.

    See perldata:

    "If you evaluate a hash in scalar context, it returns false if the hash is empty. If there are any key/value pairs, it returns true; more precisely, the value returned is a string consisting of the number of used buckets and the number of allocated buckets, separated by a slash. (...)"

      Thank you almut++. I had no idea where to look. I was especially stumped because 8 buckets were allocated "just in case". I couldn't figure out where any 8 came from.

OT: Code tags (re: Typecasting Hashes)
by roboticus (Chancellor) on Feb 20, 2010 at 14:04 UTC

    Xiong:

    Just a minor note: When you add your code tags, you don't want to have any extra lines in there. Why? Perlmonks has a feature that automatically adds line numbers for reading code chunks. So when you stated that line 20 has the error, the line marked as line 20 is blank. (Of course, your hashbang line shows up as line 2, so it was easy to see that the line marked 21 is the culprit.)

    ...roboticus

      Perlmonks has a feature that automatically adds line numbers for reading code chunks.
      A nifty feature, but how to switch it on? Is it something in CSS?

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        CountZero:

        IIRC, it's probably this bit in my Display Settings: <b><font color="red">&01;</font></b>&nbsp;

        I don't recall where I found it (I did a Super Search, but didn't turn up the link I got it from.)

        ...roboticus

        Update: Mere seconds after I post, I find the link. It's listed in Help for Display Settings in the Code Prefix section, and yes, it's the bit I mentioned above.