CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
PErl Module - not working

 



theju
New User

Mar 4, 2013, 8:09 PM

Post #1 of 4 (320 views)
PErl Module - not working Can't Post

package Mymodule;

require Exporter;
@ISA=qw(Exporter);
@EXPORT=qw(myfunc1 myfunc2);
@EXPORT_OK=qw($myvar1 $myvar2);

$myvar1 = 0;
$myvar2 = 0;

sub myfunc1
{
$myvar1 += 1;
}

sub myfunc2
{
$myvar2 += 2;
}
1;


========================================

#!/opt/rational/common/bin/ratlperl
use Mymodule;

&myfunc1;
&myfunc2;


print "X=$myvar1\n";
print "Y=$myvar2\n";


BillKSmith
Veteran

Mar 4, 2013, 9:07 PM

Post #2 of 4 (318 views)
Re: [theju] PErl Module - not working [In reply to] Can't Post

You are printing $main::myvar1 not the $Mymodule::myvar1 that you intend.

Your module works fine with the following change to your main file.

Code
use Mymodule qw($myvar1 $myvar2 myfunc1 myfunc2);

Good Luck,
Bill


theju
New User

Mar 5, 2013, 1:06 AM

Post #3 of 4 (313 views)
Re: [BillKSmith] PErl Module - not working [In reply to] Can't Post

Thanks, it worked!

for every module I write, I need to call "use" like below?

use Mymodule qw($myvar1 $myvar2 myfunc1 myfunc2);


Laurent_R
Enthusiast / Moderator

Mar 5, 2013, 4:58 AM

Post #4 of 4 (310 views)
Re: [theju] PErl Module - not working [In reply to] Can't Post

You generally don't need to import explicitely idenfiers that are declared in the @EXPORT array, except if you import explictely something else (read the documentation).

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives