Gossamer Forum
Home : General : Perl Programming :

annoying... "can't use string ... as HASH ref"

Quote Reply
annoying... "can't use string ... as HASH ref"
I keep getting this error all the time:

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

Basicly, I'm trying to modify a key in a hashref object to report back errors in my module, and when I remove the code that does this, the error goes away.

Anyone able to decipher that message? I did a search in the forum but it pretty much only turned up Links SQL related topics, and no helpful information was found.

--Philip
Links 2.0 moderator
Quote Reply
Re: [junko] annoying... "can't use string ... as HASH ref" In reply to
Could you show a code snippet?
Quote Reply
Re: [PaulWilson] annoying... "can't use string ... as HASH ref" In reply to
I got it working but I still don't understand why the original code didn't work.

Here's a "dummy" module that works properly:
Code:
use strict;
my $obj = new Test;
my $test = $obj->function() or ( print $obj->error() and exit);
print $test;
package Test;
use strict;
sub new {
my $class = shift;
my $self = {};
$self->{'error'} = '';
return bless $self, $class;
}
sub function {
my $self = shift;
if ( 1 < 5) {
$self->{'error'} = "1 cannot be greater than 5!";
return 0;
}
return 1;
}
sub error {
my $self = shift;
return $self->{'error'};
}

The error occured when trying to pass the error to another sub to handle setting the error variable...

Code:
use strict;
my $obj = new Test;
my $test = $obj->function() or ( print $obj->error() and exit);
print $test;
package Test;
use strict;
sub new {
my $class = shift;
my $self = {};
$self->{'error'} = '';
return bless $self, $class;
}
sub function {
my $self = shift;
if ( 1 < 5) {
set_error("1 cannot be greater than 5!");
return 0;
}
return 1;
}
sub set_error {
my $self = shift;
my $error = shift;
$self->{'error'} = $error;
return $self;
}

sub error {
my $self = shift;
return $self->{'error'};
}

--Philip
Links 2.0 moderator
Quote Reply
Re: [junko] annoying... "can't use string ... as HASH ref" In reply to
Hi,

You call:

set_error("1 cannot be greater than 5!");

so the first argument is a string. Yet inside set_error:

sub set_error {
my $self = shift;
my $error = shift;
$self->{'error'} = $error;
return $self;
}

It thinks the first argument is $self, a Test object I assume? So when you call $self->{error} = $error, $self is actually a string, and hence the warning.

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] annoying... "can't use string ... as HASH ref" In reply to
awe shux.. now I see what the problem was... I just needed to do:
Code:
set_error($self, "error")
I spent half a day banging my head on my desk trying to figure that out. I feel stupid now :-) Thanks Alex. I guess what got me is that direct calls from the script automaticly place the object as the first argrument, and didn't realize that in internal calls you must implicitly include it.

--Philip
Links 2.0 moderator
Quote Reply
Re: [junko] annoying... "can't use string ... as HASH ref" In reply to
Actually, you probably want to do:

$self->set_error ("error");

instead. This means it's a method call, and $self will be the first argument.

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] annoying... "can't use string ... as HASH ref" In reply to
yeah, I just tested that right after I posted, and was going to edit my post and you beat to it.

--Philip
Links 2.0 moderator