Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6577533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:38:55+00:00 2026-05-25T15:38:55+00:00

I get this error Can’t call method prepare without a package or object reference

  • 0

I get this error

Can’t call method “prepare” without a package or object reference at … [in createSqlTable line starting with my $execute].

with code similar to the following (snippets):

use othermodule1;
use othermodule2;

my $dbh = othermodule1::connectToDatabase();

if ( defined $databaseHandler )
{
  print "\ndatabaseHandler is defined here after calling connectToDatabase\n";
}

othermodule2::setDatabaseHandler( $databaseHandler );

othermodule2::createSqlTable();

othermodule1:

my $databaseHandler = unset;

sub connectToDatabase
{
  $databaseHandler = DBI->connect('DBI:mysql:db','db','pwd') or die "Could not    connect to database: ";
}

othermodule2:

my $dbhandler = unset;

sub setDatabaseHandler
{
  $dbhandler = @_;
}


sub createSqlTable()
{
  my $query = "CREATE TABLE atable ( a CHAR(30) NULL, b CHAR(30) NULL )"; # etc...

  my $execute = $dbhandler ->prepare($myquery) or die "Couldn't prepare statement: " . $dbhandler->errstr;
  $execute->execute or die "Couldn't execute statement: " . $dbhandler->errstr;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T15:38:55+00:00Added an answer on May 25, 2026 at 3:38 pm
      $dbhandler = @_;
    

    Is the problem. You are assigning in a scalar context – so the value of scalar(@_) will be assigned to $dbhandler – in this case, 1, since you passed 1-element parameter list.

    It should be: ($dbhandler) = @_; to use the list context or as an alternate idiom, $dbhandler = shift;

    Contrast:

    $ perl -e 'sub x { $x = @_ ; print "$x\n"} ; x(33);'
    1
    $ perl -e 'sub x { ($x) = @_ ; print "$x\n"} ; x(33);'
    33
    

    A second, unrelated problem, is that you misnamed your variable. You have $dbh in main script yet after assigning it you keep using $databaseHandler instead.

    if ( defined $databaseHandler )  # BAD
    if ( defined $dbh )              # GOOD
    

    The above mistake (using $databaseHandler instead of $dbh) does not show up/matter if your modules are defined in the same file as your main script above it, because first module’s my $databaseHandler declaration places that variable in scope of the rest of the file (main script included). But it will STOP working, if your module is in its own file (second example below)

    $ cat /tmp/p1.pm
    package p1;
    my $v = undef;
    sub x {
        $v = 3;
    }
    1;
    $ cat /tmp/x1.pl
    use p1;
    my $c = p1::x();
    my $v_print =  defined $v ? $v : "_UNDEF_";
    print "v=$v_print\nc=$c\n";
    
    $ perl -I /tmp /tmp/x1.pl
    v=_UNDEF_
    c=3
    
    ###############################################
    
    $ cat /tmp/x2.pl
    package p1;
    my $v = undef;
    sub x {
        $v = 3;
    }
    1; # END package p1
    package main;
    my $c = p1::x();
    my $v_print =  defined $v ? $v : "_UNDEF_";
    print "v=$v_print\nc=$c\n";
    
    $ perl /tmp/x2.pl
    v=3
    c=3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I get this error: Can't locate Foo.pm in @INC Is there an easier way
We get this error in Visual Studio 2005 and TFS very often. Can anyone
i get this error {Method 'System.DateTime ConvertTimeFromUtc(System.DateTime, System.TimeZoneInfo)' has no supported translation to SQL.}
I get this error: System.Reflection.TargetException: Object does not match target type. when trying to
I get this error on an update panel within a popupControlExtender which is within
I get this error when I do an svn update : Working copy XXXXXXXX
I get this error:- You have an error in your SQL syntax; check the
I get this error message when installing SQL 2005 Analysis services. The cabinet File
I get this error when debugging my ASP.NET web application after I trigger an
When I commit I get this error from Subversion: bash-2.05b$ svn commit -m testing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.