in reply to Re: lexicaly scoped export of variables?
in thread lexicaly scoped export of variables?

Thanks, I wanted to be sure that I'm not missing an obscure pragma.

My approach is to declare the lexicals within the destination scope and to inspect the names in the importing package with PadWalker.

Of course this doesn't help importing groups of lexicals so they have to be listed individually, but maybe just syntactic sugar is not the worst approach.

use strict; use warnings; package bla; my ($x,$y)=(42,43); require export_var; do { export_var->import my ($x,$y) ; print $x,$y; } for 1..3; print $x,$y;

use strict; use warnings; use feature 'say'; package export_var; use PadWalker qw[var_name]; use Data::Dump; my %value = ( '$x'=>1, '$y'=>2, ); sub import { my $pkg=shift; for my $var (@_) { $var = $value{ var_name(1,\$var) }; } }

Cheers Rolf

( addicted to the Perl Programming Language)

Replies are listed 'Best First'.
Re^3: lexicaly scoped export of variables?
by tobyink (Canon) on Mar 31, 2014 at 21:34 UTC

    PadWalker should certainly do the trick if you're happy to declare the variables with my in the target scope.

    use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name