SPUG: Confusing behaivior with exported variables
Peter Darley
pdarley at kinesis-cem.com
Mon Mar 23 12:10:14 PDT 2009
Mark and Tye,
I would have sworn that I took out the my declaration in the second module. I’ll try removing that and see how it works.
I usually do use strict as a default, but the first module didn’t have it so I removed it in my efforts to figure out what was going on.
Thanks,
Peter
From: Mark Mertel [mailto:mark.mertel at yahoo.com]
Sent: Friday, March 20, 2009 2:16 PM
To: Peter Darley; spug-list at pm.org
Subject: Re: SPUG: Confusing behaivior with exported variables
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
206.441.4663
mark.mertel at yahoo.com
http://www.linkedin.com/in/markmertel
http://www.seattlejobs.com/13147468
_____
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
Folks,
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;
BEGIN
{
use Exporter ();
@ISA = qw(Exporter);
@EXPORT = qw(%Session $SessionID
&TieSession &UntieSession ... );
ConnectDB(DBName=>'Sessions');
}
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;
BEGIN
{
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,
Peter
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/spug-list/attachments/20090323/07f13bfe/attachment.html>
More information about the spug-list
mailing list