There is only one situation when I consciously assign the null string to a scalar:
{ my $vec; vec( $vec, 0, 1 ) = 1; };; Use of uninitialized value $vec in vec at (eval 9) line 1, <STDIN> lin +e 1. Use of uninitialized value $vec in scalar assignment at (eval 9) line +1, <STDIN> line 1. { my $vec = ''; vec( $vec, 0, 1 ) = 1; };;
A somewhat analogous situation arises with substr:
{ my $s; substr( $s, 0, 1, 'fred' ); };; Use of uninitialized value $s in substr at (eval 13) line 1, <STDIN> l +ine 5. { my $s = ''; substr( $s, 0, 1, 'fred' ); };;
But that is less useful because it only silences the warning if there is an overlap between the range being overwritten and that that already exists:
{ my $s = ''; substr( $s, 1, 1, 'fred' ); print $s; };; substr outside of string at (eval 16) line 1, <STDIN> line 8. { my $s = ' '; substr( $s, 1, 1, 'fred' ); print $s; };; fred
But beyond those few special cases, it's pretty much pointless typing.
(I also think that using q{} instead of '' is a waste of effort; and a mild form of obfuscation. PBP strikes again.)
In reply to Re: declare and init a scalar
by BrowserUk
in thread declare and init a scalar
by fionbarr
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |