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: Intermediate:
Passing arguments into a perl-module...

 



mikerobb
Deleted

Jul 7, 2000, 2:07 PM

Post #1 of 7 (1157 views)
Passing arguments into a perl-module... Can't Post

I have a file "b.pm" which contains
a sub inc() that i want to have it accept a parameter from another perl file.

the other perl file "a.pl" unshifts to get the path right - "use b"..

in the code i want to do something like:
b:inc($variable);

and in b.pl:
my $parm = shift;

Any idea what I am doing wrong?

Thanks.... Smile



------------------


Kanji
User

Jul 7, 2000, 4:54 PM

Post #2 of 7 (1157 views)
Re: Passing arguments into a perl-module... [In reply to] Can't Post

mikerobb--

You don't provide anywhere near enough (like what errors you're getting) or consistent (where the heck did b.pl come from?) information for you anyone to tell you what you're doing wrong, but you need to ...

Make sure b.pm had a defined package namespace (package b;) and returns a true value (1;).

In a.pl, when you call functions in b.pm, make sure you use two colons, not one (b::inc($var);) as in your example.

Here's all that put together ...
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

#!/usr/bin/perl -wT
##
## This is a.pl
##
use strict;
# BEGIN { unshift @INC, "." } if you prefer
use lib qw( . );
use b;


my $variable = "Hello";
b::inc($variable);</pre><HR></BLOCKQUOTE>
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

package b;


sub inc {
my $parm = shift;
print "$parm World!\n;
}


1;</pre><HR></BLOCKQUOTE>


mikerobb
Deleted

Jul 7, 2000, 5:00 PM

Post #3 of 7 (1157 views)
Re: Passing arguments into a perl-module... [In reply to] Can't Post

thanks for the information.
i'll go put it to the test.

i didn't have a strict or a
use lib qw( . );

Thanks again.

--mike


Kanji
User

Jul 7, 2000, 5:18 PM

Post #4 of 7 (1157 views)
Re: Passing arguments into a perl-module... [In reply to] Can't Post

use strict; is -- unfortunately -- not necessary to run that script, and use lib qw( . ); is functionally the same as BEGIN { unshift @INC, "."; }, which you implied you already did in a.pl.


mikerobb
Deleted

Jul 7, 2000, 7:44 PM

Post #5 of 7 (1157 views)
Re: Passing arguments into a perl-module... [In reply to] Can't Post

hmm.. Smile

here is the code i am using:

----package ads.pm -----
package ads;
$LOGDIR='/home/path/';
$name="";
sub inc() {
my $name=shift;
if ("$name" eq "") {return;}
for($i = 1; $i <= 3; $i++ ) {
if (-e "$LOGDIR/$name.lock") {sleep 1;}
else {
open(LOCK, ">$LOGDIR/$name.lock");
print LOCK "0";
close(LOCK);
last;
}
}

if (-e "$LOGDIR/$name") {
open (FILEB,"$LOGDIR/$name");
$COUNT = <FILEB>;
close (FILEB);
chop($COUNT) if $COUNT =~ /\n$/;
$value = 1 + $COUNT;

open(FILEC,">$LOGDIR/$name");
print FILEC "$value\n";
close (FILEC);
} else {
open (FILEB,">$LOGDIR/$name");
print FILEB "1";
close (FILEB);
}

unlink("$LOGDIR/$name.lock");

}


return 1;

-----file track.pl----------
#!/usr/bin/perl
BEGIN { unshift(@INC, "/home/path/"); }

$adsdir = "/home/path";
$adsUSERS = "$neatdir/users.txt";
$HOMEPAGE = "http://www.myhomepage.com/";


use ads;

use CGI qw(:standard);
$search = new CGI;

$adname = $search->param('adname');
$action = $search->param('action');
$password = $search->param('pass');
$username = $search->param('username');

if (("$username" eq "me") &&
("$password" eq "password") &&
("$action" eq "test")) {
ads::inc($adname);
}
&sendHome;
################
# sub routines #
################


sub sendHome {
print "Location: $HOMEPAGE\n\n";

------ERROR------

$ perl -wc track.pl
Bad name after ads:: at track.pl line 22.
$ perl -wc ads.pm
ads.pm syntax OK
$ perl track.pl
Bad name after ads:: at track.pl line 22.

-----

Thanks. Smile


--mike



japhy
Enthusiast

Jul 8, 2000, 4:41 AM

Post #6 of 7 (1157 views)
Re: Passing arguments into a perl-module... [In reply to] Can't Post

The problem is you defined the ads::inc function as

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


sub inc () { ... }
</pre><HR></BLOCKQUOTE>

The () is a prototype for the function -- THAT specific prototype means "this function gets no arguments". You probably shouldn't be using function prototypes, generally. Remove the () here, and all will be well.


mikerobb
Deleted

Jul 8, 2000, 4:52 AM

Post #7 of 7 (1157 views)
Re: Passing arguments into a perl-module... [In reply to] Can't Post

DOH!

Thanks so much for your extra pair of eyes!

Smile

--mike

 
 


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

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