For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > August 2007 > How to create a package level variable









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How to create a package level variable
Sundeep

2007-08-24, 7:59 am

I need to have a package level variable. For that I am using the
following method:

package XYZ;

$XYZ::db_initialised = 0;

sub init_db() {
DB::init() unless $XYZ::db_initialized;
$XYZ::db_initialized = 1;
}

or

package XYZ;

my $db_initialised = 0;

sub init_db() {
DB::init() unless $db_initialized;
$db_initialized = 1;
}

Method 1 works fine and the database is initialized only once
(DB::init() is other module used by me for all db ops)

Method 2 fails and tries to initialize database again...
I can't use method 1 as per company standards... and I can't
understand the reason why method 2 is failing...

Jeff Pang

2007-08-24, 7:59 am

2007/8/24, Sundeep <sundeep.techie@gmail.com>:
> I need to have a package level variable. For that I am using the
> following method:
>
> package XYZ;
>
> $XYZ::db_initialised = 0;
>
> sub init_db() {
> DB::init() unless $XYZ::db_initialized;
> $XYZ::db_initialized = 1;
> }
>
> or
>
> package XYZ;
>
> my $db_initialised = 0;
>
> sub init_db() {
> DB::init() unless $db_initialized;
> $db_initialized = 1;
> }
>
> Method 1 works fine and the database is initialized only once
> (DB::init() is other module used by me for all db ops)
>
> Method 2 fails and tries to initialize database again...
> I can't use method 1 as per company standards... and I can't
> understand the reason why method 2 is failing...
>


I'm not sure why you said it try to initialize database again (each time?).
But in method2 you made a closure,when calling method2 from out of
package XYZ,the value of $db_initialized would be kept,and looks like
it's a real package variable.

see this test,

$ cat XYZ.pm
package XYZ;

my $db_initialised = 0;

sub init_db() {
$db_initialized ++;
print $db_initialized,"\n";
}

1;

$ cat xyz.pl
use strict;
use XYZ;

XYZ::init_db;
XYZ::init_db;
XYZ::init_db;

$ perl xyz.pl
1
2
3
Sundeep Gupta

2007-08-24, 7:59 am

Thanks Jeff,

It works fine now.

The problem was that this module (XYZ) module uses another module, which
again has the use statement to load XYZ. I don't know if the perl loads the
module again and might be this caused to reset the value back to 0.
When I commented the use statement of that module, it just works fine.

Thanks,
Sundeep

On 8/24/07, Jeff Pang <rwwebs@gmail.com> wrote:
>
> 2007/8/24, Sundeep <sundeep.techie@gmail.com>:
>
> I'm not sure why you said it try to initialize database again (each
> time?).
> But in method2 you made a closure,when calling method2 from out of
> package XYZ,the value of $db_initialized would be kept,and looks like
> it's a real package variable.
>
> see this test,
>
> $ cat XYZ.pm
> package XYZ;
>
> my $db_initialised = 0;
>
> sub init_db() {
> $db_initialized ++;
> print $db_initialized,"\n";
> }
>
> 1;
>
> $ cat xyz.pl
> use strict;
> use XYZ;
>
> XYZ::init_db;
> XYZ::init_db;
> XYZ::init_db;
>
> $ perl xyz.pl
> 1
> 2
> 3
>




--
~!~ Sun ~!~ { Its simple to be happy. But its difficult to be simple}

Xavier Noria

2007-08-24, 7:59 am

On Aug 24, 2007, at 11:58 AM, Sundeep Gupta wrote:

> The problem was that this module (XYZ) module uses another module,
> which
> again has the use statement to load XYZ. I don't know if the perl
> loads the
> module again and might be this caused to reset the value back to 0.
> When I commented the use statement of that module, it just works fine.


If you load a module with use(), it will only get loaded once, no
matter how many use()s for that module are executed throughout the
source tree. In particular the package variable is not reset:

fxn@feynman:~/tmp$ cat Foo.pm
use strict;
package Foo;
our $x = 0;
1;
fxn@feynman:~/tmp$ perl -MFoo -wle '$Foo::x = 1; use Foo; print
$Foo::x'
1

-- fxn

Tom Phoenix

2007-08-24, 7:01 pm

On 8/23/07, Sundeep <sundeep.techie@gmail.com> wrote:

> $XYZ::db_initialised = 0;
>
> sub init_db() {
> DB::init() unless $XYZ::db_initialized;
> $XYZ::db_initialized = 1;
> }


Do you spell it initialised or initialized?

Cheers!

--Tom Phoenix
Stonehenge Perl Training
Mr. Shawn H. Corey

2007-08-24, 7:01 pm

Tom Phoenix wrote:
> Do you spell it initialised or initialized?


Yes.

british-english: initialise
american-english: initialize


--
Just my 0.00000002 million dollars worth,
Shawn

"For the things we have to learn before we can do them, we learn by doing them."
Aristotle

"If you think Terrans are comprehensible, you don't understand them."
Great Fang Talphon
Chas Owens

2007-08-24, 7:01 pm

On 8/24/07, Mr. Shawn H. Corey <shawnhcorey@magma.ca> wrote:
> Tom Phoenix wrote:
>
> Yes.
>
> british-english: initialise
> american-english: initialize

snip

Yes, but in Perl you have to choose one and stick to it. The original
code example was:

package XYZ;

$XYZ::db_initialised = 0;

sub init_db() {
DB::init() unless $XYZ::db_initialized;
$XYZ::db_initialized = 1;
}

or

package XYZ;

my $db_initialised = 0;

sub init_db() {
DB::init() unless $db_initialized;
$db_initialized = 1;
}

Unless you have some funky source filter installed that normalizes
spelling variants Perl is going to have a problem. This is why the
strict pragma is so important.
Dr.Ruud

2007-08-24, 7:01 pm

"Chas Owens" schreef:

> [$db_initiali.ed]
> Unless you have some funky source filter installed that normalizes
> spelling variants Perl is going to have a problem.


Yes, I like the idea:

use autocorect;

(which of course corrects that line last, to signal that it's done)

--
Affijn, Ruud

"Gewoon is een tijger."
Xavier Noria

2007-08-24, 7:01 pm

On Aug 24, 2007, at 8:13 PM, Dr.Ruud wrote:

> "Chas Owens" schreef:
>
>
> Yes, I like the idea:
>
> use autocorect;


I think there's an Acme:: module that did something like that, it
used some heuristic (edit distance or whatever) and chose an already
existing indentifier. Something like that. But I can't find it.

-- fxn

Mr. Shawn H. Corey

2007-08-24, 7:01 pm

Xavier Noria wrote:
> On Aug 24, 2007, at 8:13 PM, Dr.Ruud wrote:
>
>
> I think there's an Acme:: module that did something like that, it used
> some heuristic (edit distance or whatever) and chose an already existing
> indentifier. Something like that. But I can't find it.
>
> -- fxn


Actually, it was I problem I encountered on an international project. The British team spelled if initialise and the American, initialize. We could fix it by search and replace but since we didn't have control of either source, we had to redo the s&r ev
ery time we got an update. And neither team would do it since they had other, more important items to finish first. Sadly, the project was in C :( In Perl, we could have used:

*initialise = \$initialize;


--
Just my 0.00000002 million dollars worth,
Shawn

"For the things we have to learn before we can do them, we learn by doing them."
Aristotle

"If you think Terrans are comprehensible, you don't understand them."
Great Fang Talphon
Chas Owens

2007-08-24, 7:01 pm

On 8/24/07, Xavier Noria <fxn@hashref.com> wrote:
> On Aug 24, 2007, at 8:13 PM, Dr.Ruud wrote:
>
>
> I think there's an Acme:: module that did something like that, it
> used some heuristic (edit distance or whatever) and chose an already
> existing indentifier. Something like that. But I can't find it.


I remember something about it as well (using a map of UK->US and
Levenshtein distance to catch typos), but can't find anything in CPAN
either. Thankfully Damian is busy with Perl 6 or he might write it.
Chas Owens

2007-08-24, 7:01 pm

On 8/24/07, Mr. Shawn H. Corey <shawnhcorey@magma.ca> wrote:
> Xavier Noria wrote:
>
> Actually, it was I problem I encountered on an international project. The British team spelled if initialise and the American, initialize. We could fix it by search and replace but since we didn't have control of either source, we had to redo the s&r

every time we got an update. And neither team would do it since they had other, more important items to finish first. Sadly, the project was in C :( In Perl, we could have used:
>
> *initialise = \$initialize;

snip

That would only fix global variables (and even then only if it came
after the globals were declared). Lexical variables would still be
broken, not to mention hash keys. If a simple search and replace was
all you needed then a source filter would probably work best (i.e. %80
of the time). For instance, should this be translated?

my $key = "colour";

It depends on how it is used later:

my $colour = $property{$key}; #yes, I think.
my $word = $uk_to_us{$key}; #no

and there is no way (baring strong AI*) a program can make that
decision for you.

* and if we get strong AI, well UK vs US word differences will be the
least of our problems.
Jeff Pang

2007-08-24, 9:59 pm

2007/8/24, Mr. Shawn H. Corey <shawnhcorey@magma.ca>:
> Tom Phoenix wrote:
>
> Yes.
>
> british-english: initialise
> american-english: initialize
>


But why not just spell it as init?for us both initialise and
initialize are complicated,I can't remember and differ them at all. :)
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com