Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Sharing variables between modules

Reply
Thread Tools

Sharing variables between modules

 
 
Tomek
Guest
Posts: n/a
 
      11-10-2006
Hello!
I am using Perl 5.8.8 on FreeBSD. I have to admit that I already lost 2
hours on something what should be obvious, but for me it isn't
I have 2 files, 1st is a parenting script, and 2nd is an included module.
I want to read from 2nd module a variable set in parenting script.

I have tried:

File 'a':

#!/usr/local/bin/perl
use b;
our $a='a';
$b::a=$a;

File 'b.pm':

package b;
print $a;

Executing 'a' prints just nothing
-----------------------------------------------
I also tried the second variant:
File 'a':

#!/usr/local/bin/perl
use b;
our $a='a';

File 'b.pm':

package b;
use base 'Exporter';
@EXPORT=qw{$a};
our $a;
print $a;

Effect is the same No data printed I am now clueless, what am I
doing wrong ?

Regards, Tomasz Kraus
 
Reply With Quote
 
 
 
 
usenet@DavidFilmer.com
Guest
Posts: n/a
 
      11-10-2006
Tomek wrote:
> Executing 'a' prints just nothing


That's because (among other reasons) 'a' has no print command.

I don't think you're clear on what modules are or what they do.
use()ing a module isn't the same thing as inserting and executing the
code. You don't export variables from modules; you export subroutines.
You call the subroutine and pass data to it. For example:

#!/usr/bin/perl
use strict; use warnings;
use b qw{ print_stuff }; #this is a subroutine we'll call

my ($one, $two, $three) = (1,2,3);
print "In the parent program: $one $two $three \n";
print_stuff($one, $two, $three); #call the module's sub, pass data
__END__

The 'b' module looks like this:

sub print_stuff {
my ($foo, $bar, $baz) = @_;
print "Module got this info: $foo $bar $baz \n"
}
1;


--
The best way to get a good answer is to ask a good question.
David Filmer (http://DavidFilmer.com)

 
Reply With Quote
 
 
 
 
Mumia W. (reading news)
Guest
Posts: n/a
 
      11-10-2006
On 11/10/2006 01:43 PM, Tomek wrote:
> Hello!
> I am using Perl 5.8.8 on FreeBSD. I have to admit that I already lost 2
> hours on something what should be obvious, but for me it isn't
> I have 2 files, 1st is a parenting script, and 2nd is an included
> module. I want to read from 2nd module a variable set in parenting script.
>
> I have tried:
>
> File 'a':
>
> #!/usr/local/bin/perl
> use b;
> our $a='a';
> $b::a=$a;
>
> File 'b.pm':
>
> package b;
> print $a;
>
> Executing 'a' prints just nothing
> -----------------------------------------------
> I also tried the second variant:
> File 'a':
>
> #!/usr/local/bin/perl
> use b;
> our $a='a';
>
> File 'b.pm':
>
> package b;
> use base 'Exporter';
> @EXPORT=qw{$a};
> our $a;
> print $a;
>
> Effect is the same No data printed I am now clueless, what am I
> doing wrong ?
>
> Regards, Tomasz Kraus


Shouldn't that be the other way around? The module that you're use-ing
defines the variables, and the other module prints those values.



--
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
usenet@DavidFilmer.com
Guest
Posts: n/a
 
      11-10-2006
Mumia W. (reading news) wrote:
> Shouldn't that be the other way around? The module that you're use-ing
> defines the variables, and the other module prints those values.


I think the OP is confusing use() and do().

--
The best way to get a good answer is to ask a good question.
David Filmer (http://DavidFilmer.com)

 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      11-10-2006

Quoth Tomek <(E-Mail Removed)>:
> Hello!
> I am using Perl 5.8.8 on FreeBSD. I have to admit that I already lost 2
> hours on something what should be obvious, but for me it isn't
> I have 2 files, 1st is a parenting script, and 2nd is an included module.
> I want to read from 2nd module a variable set in parenting script.
>
> I have tried:
>
> File 'a':
>
> #!/usr/local/bin/perl


You *always* want

use strict;
use warnings;

here.

> use b;


Don't use lowercase module names. They are reserved for pragmata
(modules that affect how perl parses your program).

> our $a='a';


Don't use the variables $a and $b. They are slightly magic (they are
used by the sort function) and don't need to be declared. This makes
things harder to debug.

> $b::a=$a;


This is occuring too late. The 'print $a;' below happens as soon as the
'use b;' line is compiled, which is before any of the code in 'a' is
run.

> File 'b.pm':
>
> package b;


You need

use strict;
use warnings;

here, as well, as their effects are limited to the lexical scope they
are used in: in this case, the file.

> print $a;


If you'd used warn instead of print here, it would have been clear that
the statement was in fact being executed but the variable was not set
yet.

If you really want to do something like this, you want

#!/usr/bin/perl

use warnings;
use strict;

# The BEGIN makes the assignment happen at compile time.
# Note that this must also come *before* the use Foo line.

BEGIN { $Foo::foo = 'foo' }

use Foo;

__END__

package Foo;

use warnings;
use strict;

print $foo;

1;

> I also tried the second variant:
> File 'a':
>
> #!/usr/local/bin/perl
> use b;
> our $a='a';


What is the 'our' there for? Do you know what it does?

> File 'b.pm':
>
> package b;
> use base 'Exporter';
> @EXPORT=qw{$a};
> our $a;
> print $a;


Here, again, $b::a is being set too late, after the print command has
already happened.

What are you actually trying to acheive here? Doing real work (as
opposed to initialisation) in the body of a module is generally
considered bad practice: it is much better to export subs from the
module. If your intention was that $a be some sort of setting for your
module, then it is usual *not* to export it but to set it by its full
name in the calling program. If the module's actual work were to be put
in a sub, then you can set the variable before you call the sub,
something like

package Foo;

use strict;
use warnings;

# 'use base' does some odd things to make 'fields' module work.
# It's generally considered better not to use it for Exporter.

require Exporter;
our @ISA = 'Exporter';

# Putting stuff in @EXPORT is usually considered bad style, as
# @EXPORT_OK makes it clearer where things are imported from.

our @EXPORT_OK = qw/do_stuff/;

# It's usual to use Titlecase for package globals, so they stand out
# in the code. This is similar to using ALL_CAPS for constants.

our $Setting;

sub do_stuff {
warn $Setting;
}

1;
__END__

#!/usr/bin/perl

use strict;
use warnings;

use Foo qw/do_stuff/;

$Foo::Setting = 'foo';
do_stuff;

__END__

Ben

--
Like all men in Babylon I have been a proconsul; like all, a slave ... During
one lunar year, I have been declared invisible; I shrieked and was not heard,
I stole my bread and was not decapitated.
~ (E-Mail Removed) ~ Jorge Luis Borges, 'The Babylon Lottery'
 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      11-10-2006

Quoth (E-Mail Removed):
> Tomek wrote:
> > Executing 'a' prints just nothing

>
> That's because (among other reasons) 'a' has no print command.


This is not the problem. The print statement is executed, just earlier
than the OP thought.

> I don't think you're clear on what modules are or what they do.
> use()ing a module isn't the same thing as inserting and executing the
> code. You don't export variables from modules; you export subroutines.


While it is not common, you most certainly *can* export variables from
modules. The 'vars' module, for instance, exists solely to export
variables. Other examples are the Socket module, which exports $LF, $CR,
and $CRLF, and the FindBin module, which exports $Bin.

You are correct, of course, that it is generally better practice to
export subs and pass arguments to them, but TMTOWTDI .

Ben

--
We do not stop playing because we grow old;
we grow old because we stop playing.
(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sharing persisten cache between modules Bart Ogryczak Python 7 10-24-2006 10:53 AM
Global variables in modules---preserving values between module calls Joseph Wakeling C Programming 9 03-05-2006 12:25 PM
Don't understand global variables between modules Bart Python 4 02-23-2005 06:57 PM
Sharing variables between pages... =?Utf-8?B?QXNoYSBHaWxs?= ASP .Net 2 06-09-2004 06:41 AM



Advertisments