Hi, Jeff, :)

On 15 Oct 2002, jeff wrote:

> Quick question. I want to pass a variable to a non-oo module that I
> created. But I how do I get the module to contain the value of the
> variable that was created in the 'main' namespace.

In general, this is not good style.  A module should be a
self-contained unit of code that should not (generally) use the
client's resources and only use other module's resources through
standard interfaces.  If you're going to pass data back and forth, do
it through function argument lists.

Also, there's nothing "special" about an object-oriented module and a
non-OO module.  An OO module blesses some reference using its package
name... that's about it.  All of the module functionality is available
regardless of the style you use.

> I think there is something easy that I am not understanding. Example
> code below.

> PROGRAM BEGIN
> #!/usr/bin/perl
> use HelloWorld;
>
> $var = 'hello world';
>
> printStatement;
> exit;
> PROGRAM END

> MODULE BEGIN
> #!/usr/bin/perl
>
> package HelloWorld;
> require Exporter;
>
> our @ISA       = qw(Exporter);
> our @EXPORT    = qw(printStatement);
>
> our $VERSION   = 1.00;
>
> sub printStatement { print $var; }

> 1;
> MODULE END

> THINGS I'VE TRIED BEGIN

> I've tried passing in $var to printStatement,

> printStatement($var)

> in the program

If you wrote printStatement like so:

sub printStatement {
    my $arg = shift;
    print $arg;
}

(Or just 'sub printStatement { print @_; }' would do.)

then calling

printStatement( $var );

would print out the contents of $var.  Is this what you want?

> I've tried using the 'main' namespace in the module

> print $main::var;

Regardless of why you want to do it this way, I was able to replace
'print $var' with 'print $main::var' and see the output I expected.
I'm not exactly sure what you were doing that caused this to fail.

Again though, this is poor style.

> I've tried passing a reference the $var in the module

> printStatement (\$var);

You still need to modify the definition of printStatement to expect an
argument via the '@_' array.  This is exactly the same as
printStatement($var), really.

> Executive summary: how do I define a variable in my program and have
> a module use that variable definition (in a non-oo module).

What are you expecting to be different between OO and non-OO modules?
Perl is pretty casual about these things...

---Jason
Sonos Handbell Ensemble
http://www.sonos.org/


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to