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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |