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 8608067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:31:16+00:00 2026-06-12T03:31:16+00:00

Possible Duplicate: Perl + POO and “Can't call method ”prepare I learned poo and

  • 0

Possible Duplicate:
Perl + POO and “Can't call method ”prepare”

I learned poo and i got to play with perl, create this module but when I call the segudno method I skip the following error ‘Use of uninitialized value $database in concatenation ( .) … ‘ Followed by ‘Can’t call method ‘prepare’ mm i don’t really understand, any suggestions?

#!/usr/bin/perl

use warnings;
use strict;
use DBI;
use DBD::mysql;

package MysqlTest;

sub new{
    my $class = shift;
    my $query={};
    bless($query, $class);
}
sub conexion{
    my $self=shift;
    my($database, $hostname, $user, $pwd)=@_;
    $self->{"host"}=$hostname;
    $self->{"database"}=$database;
    $self->{"user"}=$user;
    $self->{"pass"}=$pwd;
    our $connect = DBI->connect("DBI:mysql:database=$database;host=$hostname;", $user, $pwd) or die $DBI::errstr;
    my $mysqlopen = 1;
return;
}
sub consulta{
    my $self=shift;
    if (!my $mysqlopen) { &conexion(); }
    my $id = "SELECT * FROM save_bookmarks WHERE id='123'";
    our $result = my $connect->prepare($id);
    $result->execute();
    my @resultado = $result->fetchrow_array();
    print "@resultado\n";
    return;
}
sub datos{
    my $self=shift;
    print "::DATOS DE ACCESO::\n";
    while ((my $key, my $value)=each(%$self)){
        print "$key => $value\n";
    }
}
1;

in other file for call msg and created objected.

#!/usr/bin/perl
use MysqlTest;
use warnings;
use strict;

my $mysqltest = MysqlTest->new();
$mysqltest->conexion("bookmarks", "localhost", "root", "pass");
$mysqltest->consulta();

this output in console.

Use of uninitialized value $database in concatenation (.) or string at MysqlTest.pm line 23.
Use of uninitialized value $hostname in concatenation (.) or string at MysqlTest.pm line 23.
Can't call method "prepare" on an undefined value at MysqlTest.pm line 31.

any idea?

thanks.

  • 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-06-12T03:31:17+00:00Added an answer on June 12, 2026 at 3:31 am

    Your code includes this line:

    if (!my $mysqlopen) { &conexion(); }
    

    You call your conexion sub with no arguments. However, this sub expects several arguments, including a blessed object, that you don’t provide. You might want to fix that. $database and $hostname also are expected in the arguments.

    Your call to conexion will always be executed, because my $var creates a new variable and initializes it with undef—and the negation of undef is a true value.

    Then you have this statement:

    our $result = my $connect->prepare($id);
    

    my creates a new variable $connect on which you try to call the method prepare. This doesn’t work, as the created variable is no blessed reference but simply undef.

    Scope in Perl

    Here is a verbose example on how lexical scope with my works in Perl

    # $var doesn't exist
    
    sub foo {
       # $var doesn't exist
       my $var;
       # $var exists
       bar();
       # $var exists
    }
    
    # $var doesn't exist
    
    sub bar {
       # $var doesn't exist
       return;
    }
    
    # $var doesn't exist
    

    You define a my variable mysqlopen in conexion, then redefine it in consulta. It is, however, not the same variable, as these variables reside in different scope.

    Instead, you are likely to add fields mysqlopen and connect in the object you are passing around, as this is object data. You can then use this information just like the host or database fields.

    Also, do not call the method conexion without an object. Objects are usually created with new.


    Edit

    It is quite difficult for me to debug your code and parse your English at the same time, so here is your code with the worst bugs removed. However, I have no experience with DBI, so it may not work directly:

    sub conexion{
       my $self=shift;
       die "I need args!" unless @_;
       my($database, $hostname, $user, $pwd)=@_;
       $self->{host}     = $hostname;
       $self->{database} = $database;
       $self->{user}     = $user;
       $self->{pass}     = $pwd;
       $self->{connect}  = DBI->connect(
          "DBI:mysql:database=$database;host=$hostname;",
           $user,
           $pwd,
       ) or die $DBI::errstr;
       $self->{mysqlopen}= 1;
       return;
    }
    
    sub consulta{
       my $self = shift;
       if (not $self->{mysqlopen}) {
          die "This object wants to be conexioned before you consulta anything";
          # you could also do
          #    $self->conexion(DEFAULT_VALUES);
          # but then you would really *have* to provide defaults!
       }
       my $id = "SELECT * FROM save_bookmarks WHERE id='123'";
       my $result = $self->{connect}->prepare($id);
       $result->execute();
       my @resultado = $result->fetchrow_array();
       print "@resultado\n";
       return;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Can main function call itself in C++? I found this problem very
Possible Duplicate: Why does Perl's glob return undef for every other call? This is
Possible Duplicate: perl “dollar sign and question mark” question I am trying to understand
Possible Duplicate: Why am I getting an “Out of memory” error with Perl's XML::Simple?
Possible Duplicate: How can I understand nested ?: operators in PHP? Why does this:
Possible Duplicate: Comparing Two Arrays Using Perl How can I print values which exist
Possible Duplicate: How can I take screenshots with Perl? How can I take a
Possible Duplicate: How can I read lines from the end of file in Perl?
Possible Duplicate: In Perl, how can find the date of the previous Monday for
Possible Duplicate: How can I check if I have a Perl module before using

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.