[sf-perl] oddity with an exported and localized variable

Joe Brenner doom at kzsu.stanford.edu
Thu Nov 26 00:23:04 PST 2009


David Sharnoff mentioned he'd seen some odd behavior with an
exported localized variable.  If I understand the problem,
the following reproduces it for me (using perl 5.10.0 on
ubuntu jaunty).

The point here is that the exported sub appears to be
seeing the value the variable was assigned in the module,
rather than seeing the current value assigned in the script.
This isn't the way dynamic scoping is supposed to work,
correct?  Any ideas what might be going on here?


#=== begin script
use warnings;
use strict;

use FindBin qw($Bin);
use lib ("$Bin/../lib/");

use Module::MonoPackage::Export qw( $exported_variable
                                    check_value_exported_sub );

print "initial value: $exported_variable\n";  # 'initial"
local $exported_variable = "new value";
my $expected = 'new value';
print "the new value: $exported_variable\n";  # 'new value'
print "check_value_exported_sub sees: \n";
check_value_exported_sub(  );                 # 'initial'
print "check_value_local_def sees: \n";
check_value_local_def( );                     # 'new value'

sub check_value_local_def {
  print "exported_variable: $exported_variable\n";
  return $exported_variable;
}
# === end script

# ===
# The module: /home/doom/lib/Module/MonoPackage/Export.pm

package Module::MonoPackage::Export;
use 5.008;
use strict;
use warnings;

require Exporter;

our @ISA = qw(Exporter);
our %EXPORT_TAGS = ( 'all' => [
  qw(
     $exported_variable
     check_value_exported_sub
    ) ] );
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
our @EXPORT = qw(  );
our $VERSION = '0.01';

our $exported_variable = "initial";

sub check_value_exported_sub {
  print "exported_variable: $exported_variable\n";
  return $exported_variable;
}

1;
# === end module


More information about the SanFrancisco-pm mailing list