Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

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. 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

THINGS I'VE TRIED END

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

Jeff

edited: Wed Oct 16 15:59:10 2002 by jeffa - formatting fixes

Replies are listed 'Best First'.
Re: variable passed to module
by thpfft (Chaplain) on Oct 16, 2002 at 00:03 UTC
    I've tried passing in $var to printStatement

    that should work, as long as your module subroutine retrieves the variable properly. When you call sub($var), it's the value of $var, not the variable itself, that gets handed over, so you can't retrieve it by asking for $var in the sub, as use strict will point out.

    Instead, the parameters passed to a subroutine are available within it as a flattened list in @_, which by great good fortune is the default subject of the shift function. Hence the very common idiom:

    printStatement('foo'); sub printStatement { # @_ is ('foo') my $var = shift; # $var is now what was $_[0], the first item in @_, ie 'foo' print $var; } # or just sub printStatement { print shift }
    I've tried using the 'main' namespace in the module

    not such a good plan. the idea is not to mingle namespaces, so that you don't have to wander through your modules trying to work out who changed what. There are circumstances where it makes sense to do it the other way around, but they're few and special (eg mod_perl startup scripts):

    $HelloWorld::var = 'foo'; printStatement();
    I've tried passing a reference the $var in the module

    nothing wrong with that either, and often the best approach (eg if you think the interface to your module may change later to make this function accept parameters that might include lists or hashes). You just have to make the printStatement sub retrieve the variable from @_ and dereference it before printing:

    my $important_message = 'foo'; printStatement(\$important_message); sub printStatement { my $var = shift; print $$var; }

    perlsub has the details, and how to rtfm has how to get there, thanks to masem.

    updated for clarity

Re: variable passed to module
by nothingmuch (Priest) on Oct 15, 2002 at 23:44 UTC
    If you use strict you'll see your first problem.
    The basic concept is mistaken - the namespace is supposed to help unrelated modules keep from clashing. Hence a named variable will not work.
    You can, however share globals such as $_, and also localized globals. If you replace $var with $_ in both statements, it'll work. Morever
    $_ = 'gorch'; { local $_ = 'foo'; printStatement(); } printStatement();

    Will have nice results.
    See perlsub for more details on lexical and dynamic scoping.
    In general you should always pass an argument to the subroutine for something like this.

    -nuffin
    zz zZ Z Z #!perl