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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

calling a sub in a .pm file

Status
Not open for further replies.

Oostwijk

Technical User
Oct 19, 2003
82
NL
I've got an external file named bieb.pm

in this file there is an sub named ShowBug
I've got also a normal script in which I call on the procedure:

#!/usr/local/bin/perl
use CGI;
$q=new CGI;
print "Content-type: text/html\n\n";
require bieb;


When I call ShowBug with this line:
&Showbug;
it works perfectly, however when I want to call ShowBug like this:
tie %h, "DB_File", "fruit", O_RDWR|O_CREAT, 0666, $DB_HASH || &ShowBug;

it isn't working... What am I doing wrong here ??
 
I don't receive an error message but the sub isn't executed.
 
Can you show us the subroutine? Have you put 'must execute' debug in the subroutine to make sure its really not execututing? (like make hte first line "print 'Hi ma!';"

Finally, &ShowBug will only execute if the tie fails, is your tie failing?

 
You should use
Code:
or
instead of
Code:
||
.
You could add parenthesis too.

What you are doing is:
Code:
tie(%h, "DB_File", "fruit", O_RDWR|O_CREAT, 0666, $DB_HASH || &ShowBug);
I mean: you call tie with as last argument a logical or between $DB_HASH and the result of the ShowBug function. As $DB_HASH is 'true' (unless it is undef or 0 or '' but I think it is unlikely) the function is never called.

What you want is:
Code:
tie(%h, "DB_File", "fruit", O_RDWR|O_CREAT, 0666, $DB_HASH) || &ShowBug;
Another way:
Code:
tie %h, "DB_File", "fruit", O_RDWR|O_CREAT, 0666, $DB_HASH or &ShowBug;
because
Code:
or
as a very low precedence (lower than the coma precedence).

When in doubt, use parenthesis.

--------------------
"When all else has failed, read the manuals."

Denis
 
I've tried both your solutions dchoulette, but it still doesn't function.

Here is my bieb.pm code:
sub ShowBug{
print qq{..hello everybody..};
exit;
}
1;

I've place a false filename in the tie sentence, so the ShowBug sub should be called. Very strange...
 
Try callling it like this :

&bieb::ShowBug

It doesn't sound like you are exporting it to the namespace you are working in.

I feel like we are not getting some info to solve the problem but I do not know what info you are not giving. I will continue to parse this issue.
 
hmmm, that doens't work either.
The script that calls the sub looks like this:

#!/usr/local/bin/perl
use CGI;
use Benchmark;
use DB_File;
$q=new CGI;
print "Content-type: text/html\n\n";
require bieb;
&Header;
$t0 = new Benchmark;
tie(%h, "DB_File", "ruit", O_RDWR|O_CREAT, 0666, $DB_HASH) || &bieb::ShowBug;
while( my ($key, $string) = each %h) {

## rest of my script


I've posted my .pm file in my last comment.
The existing db file on the computer is named fruit, ruit doesn't exist.
 
hmmm, I hope someone can solve my problem ?
 
What does this output :

tie(%h, "DB_File", "ruit", O_RDWR|O_CREAT, 0666, $DB_HASH) || print "I broke! $! : $&" ;

 
That doesn't print anything..

But i've seen that when I test the normal script, the db filename which didn't exist before is being made.
 
if it does not print anything then there is no error.

If there is no error the || condition never gets executed.

I think that whatever context you are running this in is not erroring out and the ShowBug is not getting executed..
 
Can I rewrite the tie function so that it only reads the database, instead of making a database if the db file doesn't exist ? That would solve the problem...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top