in reply to 'local' for imported variables doesn't work as expected

Hmm not sure what's wrong in your setting...

I ran a quick test on my mobile, and this works for me.

Probably an issue with Exporter ?

use v5.12; use warnings; BEGIN{ package log; our $var= "default"; sub tst { say $var } # export *::var = *var; *::tst = *tst; } package main; our $var; { local $var = "foo"; tst(); } tst();

perl local_import_var.pl foo default

Anyway I'd rather recommend another design, like using caller to get the function's name or setting a regex to parse the message for keywords.

HTH :)

EDIT

AHH, got it. What exporter does is to only export the scalar slot, which is causing your problem.

*::var = \$var; # replace!

Effect:

perl local_import_var.pl default default

So exporting the whole typeglob will fix it.:).

In case exporter doesn't support * , use your own import

Cheers Rolf
(addicted to the Perl Programming Language :)
see Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re^2: 'local' for imported variables doesn't work as expected
by muthm (Sexton) on Oct 06, 2024 at 22:28 UTC
    LanX wrote:
    So exporting the whole typeglob will fix it.:).

    In case exporter doesn't support * , use your own import

    This perfectly does the job!

    I replaced

    our @EXPORT = qw( %debug $d_area dsay );
    by
    our @EXPORT = qw( %debug *d_area dsay );
    in Dsay.pm.

    I now can use

    sub one { local $d_area = "one"; dsay "debugging output from one"; } sub two { local $d_area = "two"; dsay "debugging output from two"; } $debug{TWO} = 1; one; two;
    and get debugging output from the 'two' area only. Debugging output stops as soon as the localized $d_area value drops out of scope.
    And this without the need of qualifying $Dsay::d_area anymore!

    Thank you so much! This really helps, and I have learned a lot!
      > Thank you so much!

      You're very welcome! :)

      > This really helps, and I have learned a lot!

      Great. It's nice to hear that once in a while ... :)

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      see Wikisyntax for the Monastery