Search code examples
perlapicalling-convention

Is it a convention to avoid using $_ when using other people's Perl API's?


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?


Solution

  • 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 localwith

    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.