Howdy!

There's a whole bunch of misdirection in them thar comments...

use strict;  #try to catch as many type errors as possibleWrong... The strict pragma is meant to "restrict unsafe constructs", according to its POD. This is not the same thing as "catching type errors". See below for applications...

print $a+@b; #coerce array into number
Misleading... This isn't "type coercion". This is "evaluation in a scalar context".
print $a+$ref_b; #coerce reference into number
A reference is a species of scalar that numifies to the memory address of its referent. No coercion here. It's still a scalar.
my $d=eval %c; #coerce hash into string print "d=$d"; #amusing result
Misleading again. This is "evaluation of a hash in a scalar context". I'm not sure what is actually amusing about the result. It's all there in the documentation.
my @e=%c;print "@e"; #hashes and arrays are different types. Oh wait +... print "c=$_" for %c;
They are both collections, so one could group them as the same meta-type, but they have different characteristics that warrant treating them as different types. The assignment is simply "evaluation of a hash in a list context". 'for %c' produces the same situation. As usage goes, it's value is limited.
my @t=12; #coerce number into array print @t; print 0+@t;
The assignment sets up a list context, in which there is only one item, the "12". I don't see "type coercion" here either. The two 'print' statements are unremarkable demonstrations of evaluating an array in list and scalar contexts.
print "\\4 = ".(\4->{"what???"}); #???
The output of this line lies. \4 produces a different value than does \4->{"what???"}. In the debugger, 'p \4' prints a ref to a scalar; 'x \4' shows that '4' is the referent. The numeric part of the ref is different from that printed by this line, which is, in fact, a scalar reference to an undefined value. What was this supposed to demonstrate?

Further fun with perl -MO=Deparse finds that this print is parsed as

print '\\4 = ' . \$4{'what???'};
How interesting...
sub test{ return ("a",123) }; #sub returns a list my $scalar_list=test(); #coerce into scalar my @array_list=test(); #coerce into array my %hash_list=test(); #coerce into hash print "\$scalar_list=$scalar_list\n\@array_list=@array_list";
$scalar_list demonstrates the comma operator in action. What are you trying to demonstrate here? What did you intend to demonstrate with %hash_list? Where does "coercion" come into play here?
no warnings; my %i=$ref_a; print %i; #apparently hashes can be scalar refs...
Oh? How do you demonstrate that? I note that you "cleverly" turn warnings off so the casual observer won't see "Odd number of elements in hash assignment" pop up from the assignment, nor "Use of uninitialized value in print" from the print. What do you think is actually stored in %i? Iterate over the keys and dereference them, if you dare. Hint: it won't work. Spectacularly.

Further investigation with Deparse show that this is parsed as

my (%i) = $ref_a;
creating the list context from which it expects to get an even number of items. The same condition applies above at 'my @t=12;'
no strict; $$ref_a->[88]=7; print $$ref_a->[88]
Why turn strict off here? Which facet of strictures were you hoping to sneak by here? Pray explain.

Deparse reports a triple dollar sign, not a double.

So, to get to the bottom: bad troll; no biscuit.

yours,
Michael

In reply to Re: strong typing by herveus
in thread (Completely OT) - Hero(i)n programming language on Slashdot by dragonchild

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.