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:
- You can fully qualify them with the package name, as in
$packageName::variableName;
- 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.
| [reply] |
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?
| [reply] |
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 | [reply] [d/l] [select] |
|
|
Or $main::shared = "Hello World!\n";
| [reply] [d/l] |
|
|
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.
| [reply] |
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 | [reply] |
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 | [reply] |
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 | [reply] |
|
|
| [reply] |
|
|
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? | [reply] [d/l] |
|
|
|
|
|
|