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

Wise Ones,
My Dilemma,
My Code First:
#!/usr/bin/perl use strict; use ModuleName; our $variableName = "HelloWorld"; $returnedValue = packageName->routineName(); print $returnedValue;


Filename: ModuleName.pm
package packageName; sub routine { print $strng; return "Done"; } 1;

I would assume that this should print HelloWorldDone but it doesn't. Any ideas on how to pass variables to modules without technically passing them?
if I renamed the module to a *.pl file and called the subroutine directly, the subroutine can access the variable fine.
Thanks Ironicsky!

Replies are listed 'Best First'.
Re: Passing Variables
by steves (Curate) on Oct 23, 2004 at 22:24 UTC

    It might help you to understand a few very basic things I always try to teach new Perl programmers:

    • With the exception of special variables defined by Perl (well, okay you can do that too ...) there really are no "global" variables in Perl as there are in many languages. The variables you're definining in your example are package variables. Those are different than global variables.
    • To access another package's package variables, you can do one of two things:
      1. You can fully qualify them with the package name, as in $packageName::variableName;
      2. The package defining the variables can export them in a way that lets them become part of the namespace of another package. That is typically done using Exporter.

    The way I think of Perl's package variables is that both the defining and using packages get to decide how "global" to make them: the defining package by how it defines and exports them; the using package by how it imports them. On the importing side, the using package is really making a decision about how it wants to pollute its own package namespace.

    In addition to Export, the documentation on import and use should help you to understand this better. Some people don't realize that use lets the using package decide how much it wants to take advantage of what the package being used has decided to Export by default.

Re: Passing Variables
by steves (Curate) on Oct 23, 2004 at 21:54 UTC

    Are you sure you copied the code correctly? You have a sub in Module.pm named routine but your main code is calling something named routineName. routine is printing a variable named $strng that isn't defined anywhere. Did you mean to be trying to print $variableName instead of $strng?

Re: Passing Variables
by rlb3 (Deacon) on Oct 23, 2004 at 22:12 UTC
    Hello,

    You may want to try something like this:

    file: test.pl
    #!/usr/bin/perl use strict; use warnings; use PackageName; our $shared = "Hello World!\n"; my $returnedValue = PackageName->routine(); print $returnedValue; __END__

    file: PackageName.pm
    package PackageName; sub routine { print $main::shared; return "Done\n"; } 1;
    This maybe what your looking for.

    rlb3
      Or  $main::shared = "Hello World!\n";

      However it is much more important to point out that, this is not the right thing to do.

      You are using mudules, but yet try to break the higher principles that "modules" meant to server.

Re: Passing Variables
by Anonymous Monk on Oct 23, 2004 at 22:05 UTC
    Sorry, yes thats correct. I did copy/paste wrong. the packageName and routineName are just meant to be placeholders for the actual names, I've yet to think of decent ones. But yes, $strng is supposed to be $variableName
Re: Passing Variables
by Anonymous Monk on Oct 23, 2004 at 22:45 UTC
    Resolved!
    I'd like to thank you all for assisting me with this issue. I have resolved my coding and everything works fine now! have a good day everyone
Re: Passing Variables
by Anonymous Monk on Oct 23, 2004 at 22:20 UTC
    That has worked!
    Now, on the same lines of thinking... If I call use DBI; in my script.cgi file, Do I need to recall use DBI; in the Module.pm or is there a way to export the already called modules to modules called from the Main Thanks again Ironicsky

      It makes sense for you to create connection from a single place of your application, and then pass the connection to the rest (for example your modules).

        But how to I pass a connection? would I do it like
        use DBI; use Module; $dbh- dbi->connect(...); $variable = $packageName->routine($dbh);
        or is there a better way to make the Module Assume that everything called in the main script is useable?