CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Why am i getting the error Can't use string ("") as a HASH ref

 



Tejas
User

Feb 21, 2017, 7:16 AM

Post #1 of 5 (1713 views)
Why am i getting the error Can't use string ("") as a HASH ref Can't Post

 
Hi

Below is the code i have implemented and it initially works fine and during the run time it throws
the error due to else statement


Quote
Can't use string ("") as a HASH ref while "strict refs"


Code
	package MyPack::Alerts; 
sub new
{
my ($class, $args) = @_;

my $self = {
# empty array of error strings
# empty hash of the endpoints that have had BEM alerts sent
endpoints => {},
alert_id => 0,
};


sub myfunc
{
if (! exists $self->{endpoints}->{$anEndpointName}) {
++$self->{alert_id};
$self->{endpoints}->{$anEndpointName}->{alertString} = $anAlertString;
$self->{endpoints}->{$anEndpointName}->{alertID} = $self->{alert_id};
}
else {
$self->{endpoints}->{$anEndpointName}->{alertString} = $anAlertString;
}
}


Why am i getting the below error, whats wroing with the code, Am i missing something ?

Thanks
Tejas


FishMonger
Veteran / Moderator

Feb 21, 2017, 7:46 AM

Post #2 of 5 (1710 views)
Re: [Tejas] Why am i getting the error Can't use string ("") as a HASH ref [In reply to] Can't Post

The code you posted won't compile, so I can say why you're getting that error.

Please post a short but COMPLETE script that demonstrates the problem.


FishMonger
Veteran / Moderator

Feb 21, 2017, 7:49 AM

Post #3 of 5 (1709 views)
Re: [Tejas] Why am i getting the error Can't use string ("") as a HASH ref [In reply to] Can't Post

BTW, your "new" sub is not creating an object. You're not "blessing" the hash ref and you need to add a return statement.


FishMonger
Veteran / Moderator

Feb 21, 2017, 7:56 AM

Post #4 of 5 (1706 views)
Re: [Tejas] Why am i getting the error Can't use string ("") as a HASH ref [In reply to] Can't Post

Cross posted on SO.
http://stackoverflow.com/questions/42371328/cant-use-string-as-a-hash-ref-while-strict-refs


BillKSmith
Veteran

Feb 21, 2017, 8:32 AM

Post #5 of 5 (1703 views)
Re: [Tejas] Why am i getting the error Can't use string ("") as a HASH ref [In reply to] Can't Post

You do not seem to understand perls object oriented features. Please study the documents perlobj and perlootut. Then post a complete OO program which demonstrates your problem.
Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives