I've just been caught out when using someone else's API in conjunction with the default variable $_
foreach (@rps_server_details) {
@server_data = ();
@server_data = split(/,/);
@$esp_hosts = ();
$filters{server_name} = $server_data[0];
print "--->$_<--\n";
$esp_hosts = $esp->get_hosts(fields => $fields, %filters) || die "$@";
print "--->$_<--\n";
The output for this is:
--->igrid8873.someone.com,app_10<--
Use of uninitialized value in concatenation (.) or string at ./rps_inv_lookup.pl line 120.
---><--
Specifying my own loop variable instead of relying on $_ fixes the problem.
Am I just being naive by using $_ in conjunction with an API someone else has written? Or is this a bug in that API module?
It is a bug in the API. If you use $_
in a function it is important to add a
local($_);
inside the function to avoid clobbering the caller's $_
, or otherwise avoid using $_
in a library function to be called by others.
If you can limit yoursel to Perl versions > 5.9.1 then you can also make $_ lexical which makes it easier to understand than local
with
my $_;
But this will break on earlier versions of Perl.
From man perlvar:
As
$_
is a global variable, this may lead in some cases to unwanted side-effects. As of perl 5.9.1, you can now use a lexical version of$_
by declaring it in a file or in a block with "my". Moreover, declaring "our $_
" restores the global$_
in the current scope.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With