user6788226 user6788226 - 11 months ago 53
Perl Question

What is the appropriate way to return a variable from another namespace::method using a dispatch table



use strict;
use warnings;
use lib '/home/user/perl/lib/';

use SQLConnect;

my $db_something = SQLConnect->newConn({dns => "SOMETHING"});

my $sql_data = $db_something->select_fetchallArray();

for my $row ( @{$sql_data} ) {

print ",";

for my $column (@{$row}) {
print $column . ",";

print "\n"; (personal)

sub newConn {
my ($class, $args) = @_;

my $self = bless {
dns => $args,
user => myINIconfig::ini_dbUser($args->{dns}),
password => myINIconfig::ini_dbPass($args->{dns}),
}, $class;

Is this the appropriate way to return a variable using a dispatch table? The variables I am trying to return live in my
package, which contains a method to parse an
file for the username and password to our SQL databases. I am receiving the following error:

Can't use string ("SOMETHING") as a HASH ref while "strict refs" in use.

sub ini_dbUser {
my $self = shift;

my $dbh = ( $self->{dns} );

open( INI, "$path$ini" ) || die "Can't open $ini: $!\n";

while (<INI>) {

next if /^\s*#/; # ignore comments
next if /^\s*;/; # ignore ;

if ( /^\s*[\s*(.+?)\s*]\s*$/ ) { }

if ( /^\s*([^=]+?)\s*=\s*(.*?)\s*$/ ) {

if ( ( $database eq $dbh ) && ( $keyword eq "username" ) ) {
$user = $value;
print "$user\n";

return ($user);

Answer Source

You're getting confused about levels of hash indexing

First of all, your constructor newConn is being passed a reference to a hash {dns => "SOMETHING"} for the value of $args, which you then use directly when you create the object in the element dns => $args. That means you are setting it to dns => {dns => "SOMETHING"} which doesn't look at all right

Then you're extracting the value of the dns element of $args when you call myINIconfig::ini_dbUser($args->{dns}), so you're calling myINIconfig::ini_dbUser("SOMETHING"). But ini_dbUser first copies that parameter to $self, and then tries to use it as a hash reference in my $dbh = ( $self->{dns} ). Hence your error message Can't use string ("SOMETHING") as a HASH ref

I can't tell what is correct, but changing the call to just myINIconfig::ini_dbUser($args) will at least get it working

Your use of $self implies that ini_dbUser is an object-oriented method, but you're calling it as a simple function. Is that perhaps the problem?