my @arr3 = do {
no warnings 'uninitialized';
map "$arr1[$_] $arr2[$_]",
0 .. ($#arr1>$#arr2 ? $#arr1 : $#arr2);
};
(implicitly assuming C<use warnings;> as it "Should"!)
Why the no-warnings stmt?
Not to get (one of those rare authentically) *unwanted* warnings.
In case one of the arrays is uninitialized?
Or in case one of the values *within* one of the
arrays is?
The latter, with the former as a special case of it.
Anything to say about whether to do this
in general?
In general there are situations in which it is desirable or even
advisable to *locally* disable some warnings or strictures.
Also -- *why* turn off the warnings, ie why
don't you *want* to know about them (these "errors"?)?
Because I *do* know in advance that I may get a warning that is not
really something going wrong.
In (my) practice C<no warnings 'uninitialized'> is the most frequently
used and the most reasonable one.
Hope this example clarifies things up (I moved the code to a sub):
#!/usr/bin/perl -l
use strict;
use warnings;
sub zip (\@\@) {
my @a=@{ $_[0] };
my @b=@{ $_[1] };
# no warnings 'uninitialized';
map "$a[$_] $b[$_]",
0 .. ($#a>$#b ? $#a : $#b);
}
print '@arr1 and @arr2 have the same length';
my @arr1 = qw /foo bar baz/;
my @arr2 = 0 .. $#arr1;
print for (zip @arr1, @arr2), '';
print '@arr1 and @arr2 have different lengths';
@arr2 = 0 .. @arr1;
print for zip @arr1, @arr2;
__END__
If you run it, then you get:
# ./foo.pl
@arr1 and @arr2 have the same length
foo 0
bar 1
baz 2
@arr1 and @arr2 have different lengths
Use of uninitialized value in concatenation (.) or string at
../foo.pl line 10.
foo 0
bar 1
baz 2
3
If you uncomment the <no warnings> line the warning goes away and you
get the (supposedly) desired output. Of course it is implicit that I
made an educated guess at what the desired output is for undefined
entries...
Michele