SPUG: Confusing behavior with exported variables

Peter Darley pdarley at kinesis-cem.com
Mon Mar 23 18:23:16 PDT 2009


Mark,

 

                Since there is only going to be one instance I don’t see what value creating an object has.  It just makes the code harder to read.  Especially since they would have properties, but never any methods.  Is there something I’m missing?

 

Thanks,
Peter

 

From: spug-list-bounces+pdarley=kinesis-cem.com at pm.org [mailto:spug-list-bounces+pdarley=kinesis-cem.com at pm.org] On Behalf Of Mark Mertel
Sent: Monday, March 23, 2009 3:25 PM
To: Peter Darley; spug-list at pm.org
Subject: Re: SPUG: Confusing behaivior with exported variables

 

Globals are not recommended. You could make them package variables and declare them with 'my' or 'our', and then access them using the package name $Neo::Web::Session, or $Darley::Supers::CurrentUser when from outside the package. Personally, I'd make an object and fetch them using an accessor, i.e.

 

my $web = new Neo::Web;

 

$web->session;

 

package Neo::Web;

 

sub new {

...

}

 

sub session {

...

} 

---
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: Mark Mertel <mark.mertel at yahoo.com>; spug-list at pm.org
Sent: Monday, March 23, 2009 12:10:14 PM
Subject: RE: SPUG: Confusing behaivior with exported variables

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/f5f311e6/attachment-0001.html>


More information about the spug-list mailing list