Upon first reading this, I thought, "what's the difference?" More to the point, "how does one behave differently from the other?" After all, an empty string in a numeric context is zero anyway. How would one ever see the difference?

Some time later, the answer popped into my head. A string turned number will trigger warnings, but s/// won't. I verified this in the Test::More fashion.

use strict; use warnings; use Test::More 'tests' => 7; sub empty_string { '' } sub no_replacement { my $x = 'x'; return ($x =~ s/y//); } my $warning; $SIG{__WARN__} = sub { $warning = shift }; ok( ! defined $warning, 'no warning recorded' ); is( 0+empty_string(), 0, 'empty string is zero' ); ok( $warning, 'warning is generated' ); my $expected_warning = q{Argument "" isn't numeric in addition (+)}; is( substr( $warning, 0, length $expected_warning ), $expected_warning, q{warning is the one that's expected} ); undef $warning; ok( ! defined $warning, 'recorded warning is cleared' ); is( 0+no_replacement(), 0, 'no replacement is zero' ); ok( ! defined $warning, 'no warning is generated' );

Thanks for the food for thought.


In reply to Re^4: Newbie: parentheses, map, etc. by kyle
in thread Newbie: parentheses, map, etc. by nefigah

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.