use a hash from a module within a main program without passing by reference or value

  • Thread starter Sebastian Marants
  • Start date
S

Sebastian Marants

Hello,

i have a module and my program that uses it.
i want to use the hash which is only declared
in my module within the main program.
I want the output to be
one
two
two
one

but it prints nothing
anyone can help me out and teach me the
scope of variables when using modules?
in other words: how can i make %MYCOOLHASH
available in test.pl without passing it by reference
or value.

thanks in advance
Sebastian

--- test.pl ------------------
use mycoolmodule;

print "$MYCOOLHASH{'test1'}\n";
print "$MYCOOLHASH{'test2'}\n";
&changeValues();
print "$MYCOOLHASH{'test1'}\n";
print "$MYCOOLHASH{'test2'}\n";
exit;
----------------------------------------

--- mycoolmodule.pm ------------------

my %MYCOOLHASH;
$MYCOOLHASH{'test1'} = "one";
$MYCOOLHASH{'test2'} = "two";

sub changeValues
{
$MYCOOLHASH{'test1'} = "two";
$MYCOOLHASH('test2'} = "one";
}
1;
exit;
----------------------------------------
 
J

jl_post

Sebastian said:
i have a module and my program that uses it.
i want to use the hash which is only declared
in my module within the main program.
I want the output to be
one
two
two
one

but it prints nothing

--- test.pl ------------------
use mycoolmodule;

print "$MYCOOLHASH{'test1'}\n";
print "$MYCOOLHASH{'test2'}\n";
&changeValues();
print "$MYCOOLHASH{'test1'}\n";
print "$MYCOOLHASH{'test2'}\n";
exit;
----------------------------------------

--- mycoolmodule.pm ------------------

my %MYCOOLHASH;
$MYCOOLHASH{'test1'} = "one";
$MYCOOLHASH{'test2'} = "two";

sub changeValues
{
$MYCOOLHASH{'test1'} = "two";
$MYCOOLHASH('test2'} = "one";
}
1;
exit;
----------------------------------------


Dear Sebastian,

Try declaring %MYCOOLHASH with "our" instead of "my". Using "my"
makes %MYCOOLHASH accessible only to the "mycoolmodule.pm" file,
whereas using "our" makes it accessible to the entire "main" package.

I also recommend using "use strict;" and "use warnings;". If you
had used them, they would have let you know that %MYCOOLHASH did not
exist in the "test.pl" file.

I hope this helps, Sebastian.

-- Jean-Luc Romano
 
E

Eric Schwartz

Sebastian Marants said:
i have a module and my program that uses it.
i want to use the hash which is only declared
in my module within the main program.
I want the output to be
one
two
two
one

but it prints nothing
anyone can help me out and teach me the
scope of variables when using modules?

In this case, it's no different than the scope of anything else. 'my'
variables are lexically scoped, and therefore not accessible from
outside your module. 'perldoc -f my' to read more about this.
in other words: how can i make %MYCOOLHASH
available in test.pl without passing it by reference
or value.

As a general principle, You Don't Want To Do This(tm). There are a
few places where global variables are required, but almost every place
they're used, they're not needed. In this case, I would probably use
an accessor and a setter function, which I will explain below.

--- test.pl ------------------
use mycoolmodule;

use warnings;
use strict;
print "$MYCOOLHASH{'test1'}\n";
print "$MYCOOLHASH{'test2'}\n";

print GetHashVal('test1');
print GetHashVal('test2');
&changeValues();

You don't want to call functions with & unless you know what it does
and why. I will bet your next paycheck that you don't. This isn't a
crime, so just educate yourself by reading 'perldoc perlsub'.
print "$MYCOOLHASH{'test1'}\n";
print "$MYCOOLHASH{'test2'}\n";

See above. Also, all-upper-case names read as if you're shouting, and
are by convention reserved for constants in Perl. This clearly isn't
a constant, since you're changing it, so perhaps

%my_cool_hash

would be better here.
exit;
----------------------------------------

--- mycoolmodule.pm ------------------

my %MYCOOLHASH;
$MYCOOLHASH{'test1'} = "one";
$MYCOOLHASH{'test2'} = "two";

sub changeValues
{
$MYCOOLHASH{'test1'} = "two";
$MYCOOLHASH('test2'} = "one";
}

In general, I'd avoid making global variables here, too.

Maybe something like:

our %MYCOOLHASH;

sub changeValues
{
my (%hash) = @_;

$hash{test1} = 'two';
$hash{test2} = 'one';

return %hash;
}

Then you could do in your main program:

%MYCOOLHASH = changeValues(%MYCOOLHASH);

or even

%my_switched_hash = changeValues(%my_cool_hash);

in case you want to save the original to compare the switched one with
later.

This can't be your real code; this exit here will cause your main
program to exit as soon as it is encountered. Please cut and paste
real code.

-=Eric
 
X

xhoster

Sebastian Marants said:
Hello,

i have a module and my program that uses it.
i want to use the hash which is only declared
in my module within the main program.

Why? If you want to do this, it often means that you
haven't designed your module boundaries very well.

I want the output to be
one
two
two
one

but it prints nothing

I bet it prints *something*.
anyone can help me out and teach me the
scope of variables when using modules?

The docs can probably teach you that. If you read
the apparently relevant docs and still don't know, it would be best
if you tell us that in your post.

See:

perldoc -f my
perldoc perlsub
perldoc perlmod

And, because the answer to your question (other than "don't do that")
is to use package rather than lexical variables, you should probably
also see:

perldoc -f our
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top