Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: 'local' for imported variables doesn't work as expected

by LanX (Saint)
on Oct 04, 2024 at 10:41 UTC ( [id://11162051]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11162051]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (2)
As of 2026-03-07 05:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.