Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Modules are SOO FUN!!!! HURRAAAA HURRAA 1

Status
Not open for further replies.

VspecGTR3

Technical User
Feb 3, 2003
62
US
i know how to use the return in a modules to send info to the script but how do i send data to a module.
not sure if this works right...
--EXAMPLE--
(script.pl)
use crunch;
$user_input = 2;
print &crunch;

(crunch.pm)
sub crunch {
$user_input = 4;
$result = ();
return $result = $user_input + 4;
}

i assume by NOT using my or local ill be sending the modules information.
 
To understand why that wouldn't work read this:

Not using my() doens't make all variables 'global', they are still scoped to the package (or file) in which they are contained. Instead, treat module subroutines as you would any other subs and pass the value explictily
Code:
&crunch($user_input);
jaa
 
alrite now i think about it....
soo let me get this straigh in the script all i have to do is type :

use crunch;
&crunch($user_input);
$user_input = 2;
print &crunch;

do i have the right idea?
 
Try something like this Vspec

(crunch.pm)
package crunch;
sub crunch {
$user_input = shift;
return $user_input + 4;
}
1;

(main script)
use crunch;

$user_input = 2;
print crunch($user_input);
Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
thanks mike but i figured that out just needed a hint
 
a package is often referred to as a "namespace". This is a more precise means of calling:

(main script)
use crunch;

$user_input = 2;
print crunch::crunch($user_input); #call subroutine crunch which exists in the "namespace" crunch

The crunch::crunch is somewhat confusing because the subroutine and package name happen to be the same. It
might have been crunch::munch if sub was munch.

If you also had a package:
package lunch;
sub crunch {
$user_input = shift;
return $user_input + 8;
}
You could also call lunch::crunch() from main.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top