SPUG: Confusing behaivior with exported variables

Mark Mertel mark.mertel at yahoo.com
Fri Mar 20 14:16:14 PDT 2009

probably should 'use strict' as well. the first package doesn't declare the %Session hash, so it becomes global, and the second package scopes the %CurrentUser inside the BEGIN block. 'use strict' should point these things out.
Mark Mertel
mark.mertel at yahoo.com

From: Peter Darley <pdarley at kinesis-cem.com>
To: spug-list at pm.org
Sent: Friday, March 20, 2009 12:35:40 PM
Subject: SPUG: Confusing behaivior with exported variables

                It’s been a long time since I’ve bugged y’all with a stupid question, but I’ve got a new one so I’m hoping someone can help me out.
                The problem is with exporting variables, and having them available to both a script that uses the module exporting the variable, and the module it’s self.  I have two modules, one on a machine at work and one on a machine at home.  Both are doing something that looks super similar to me.
                At work I have: 
package Neo::Web;
use ...
use CGI qw/:standard/;
use CGI::Cookie;
use Apache::DBI;
use Apache::Session::Postgres;
use Data::Dumper;
                use Exporter   ();
                @ISA         = qw(Exporter);
                @EXPORT      = qw(%Session $SessionID 
                                                  &TieSession &UntieSession ... );
sub TieSession
                tie %Session, 'Apache::Session::Postgres', $SessionID, {Handle=>$Neo::DB::Sessions, Commit=>0};
                $SessionID = $Session{_session_id};
                The scripts using this package can get at %Session, and this module can get at %Session.  They are the same variable and have the same contents.
                At home I have:
package Darley::Supers;
use ... ;
use Data::Dumper;
                my (%CurrentUser);
        use Exporter   ();
        @ISA         = qw(Exporter);
        @EXPORT      = qw(%CurrentUser
                                  &Power &Challenge &GetUser &GetOrg &GetCity);
sub SetCurrentOrgSetting
                my (%Args) = @_;
                # Args: Setting = Setting to store
                #             Value = Value to store
                StoreOrgSetting(OrgID=>$CurrentUser{Org}{org_id}, Setting=>$Args{Setting}, Value=>$Args{Value});
                $CurrentUser{Org}{Settings}{$Args{Setting}} = $Args{Value};
                return 1;
                And the script using this module can get at %CurrentUser, but when this package uses it, it’s empty.  I can’t figure out why the exported hash is the same in the first example, but has two different values in the second.
Thanks for any help,

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/spug-list/attachments/20090320/891a4715/attachment.html>

More information about the spug-list mailing list