0

Iam writing a perl script ,in which iam using a module utils.pm and in utils.pm iam using another module DB.pm in which i have a sub routine connetToDB().

in utils.pm iam writing

use DB qw (connectToDB());

and below iam calling that subroutine as

my $connection=DB::connectToDB();         (This is line 30)

it is giving an error like follows. Can someone pls help?

Undefined subroutine &DB::connectToDB called at utils.pm line 30.

you can see the DB.pm code here

6
  • You probably need to show us more of the code, particularly the contents of DB.pm. Does the DB package actually contain a connectToDB subroutine? Commented Mar 9, 2017 at 6:13
  • yes.it contains.. i will add the screenshot of DB.pm Commented Mar 9, 2017 at 6:21
  • 4
    Don't add a screenshot of your code; add it as code! And don't ever post database usernames and passwords in public places. BTW, you need to change your password immediately. Commented Mar 9, 2017 at 6:23
  • You probably should use strict; use warnings; at least Commented Mar 9, 2017 at 6:27
  • 1
    As an aside: Don't call your module "DB". That name is used by the Perl debugger: metacpan.org/pod/distribution/perl/lib/DB.pm Commented Mar 9, 2017 at 7:08

2 Answers 2

4

The direct error in the shown code is that inside qw() you need names. The use pragma

Imports some semantics into the current package from the named module

(my emphasis). The "connectToDB()", with parentheses, is not the correct name for the subroutine. The error message simply says that it didn't find such a sub.

So just drop the parens, use DB qw(connectToDB);.


The code for the package was added to the question and here are some comments.

A similar fix is needed with your @EXPORT: you need the subroutine names (lose &).

Perhaps more importantly, you defined the sub using prototypes. Your sub is consistent with the prototype you use so I'll assume that it's done on purpose.

This is a very advanced (mis?)feature, which is very different from similar looking devices in other languages and is normally not needed. Chances are that you expect wrong things from prototypes. Go search for it. I'd advise against.

A side note: the prototype-related () and & are not a part of the subroutine name.

The last executed statement that returns in a module must return true, or code won't compile. The convention to ensure this is to put 1; at the end of the package.

Finally, you shouldn't name the module DB as that namespace is used internally by Perl. Also, such a generic name is just not good for a module -- it makes it easy to run into conflicts.

Sign up to request clarification or add additional context in comments.

2 Comments

i have changed module name to some other . Now getting output. Thanks everyone.
Thanks a lot to ikegami for a number of comments and edits.
0

use DB qw(connectToDB);

my $connection=DB->connectToDB();

or

if you have defined a constructor "new" in DB.pm module then

my $connection=DB->new();

my $result = $connection->connectToDB();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.