Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am new to OO perl. I am trying to write one simple program but getting the error.

Created a package Employee.pm as

package Employee;

sub new {
    my $class = shift;
    my $self = {};
    bless $self, $class;
    return $self;
}

sub get_names {
    my $self = @_;
    print " getting the names \n";
    return $self;
}

sub set_names {
    my ($self, $last_name) = @_;
    $self->{last_name} = $last_name;
    return $self->{$last_name};
}
1;

And created a .pl file as

use strict;
use warnings;

use Employee;

my $obj = new Employee("name" => "nitesh", "last_name" => "Goyal");

my $val = $obj->get_names();

print %$val;

my $setName = $obj->set_names("kumar");

print "$setName \n";

I am getting error as

"Can't use string ("1") as a HASH ref while "strict refs" in use at class1.txt line   10."
share|improve this question
 
One another query : Like in "sub get_names" we are declaring $self = @_ , and in the calling script we are passing as $obj->get_names(). Why no arguments need to give while calling when we have done $self = @_ in the sub. –  Nitesh Sep 28 at 10:40
 
Please do not correct your code in the question. If you remove your errors, our answers will look pretty strange. If you have new questions about your code, ask a new question. This one is asked and answered now. –  TLP Sep 28 at 11:07
 
It is my mistake. Really sorry. Next time it will not happen –  Nitesh Sep 28 at 11:09

1 Answer

up vote 2 down vote accepted

The error

"Can't use string ("1") as a HASH ref ..

Comes from this part:

sub get_names {
    my $self = @_;

When an array is put in scalar context, it returns its size. Since you call the sub with

$obj->get_names();

Only one argument is passed, which is the object, so @_ contains 1 argument, and its size is 1, therefore in the sub get_names, the variable $self is set to 1. Hence the error. What you probably should do is

my $self = shift;

But then, that will not do anything, because you never stored the names in your constructor. As mpapec said, you should do

my $self = { @_ }; 

in the constructor sub new.

Also, in get_names, you simply return the object, which is not very useful. You should perhaps return $self->{name} and $self->{last_name}.

share|improve this answer
 
thanks. But what does this mean "But then, that will not do anything, because you never stored the names in your constructor. As mpapec said, you should do" –  Nitesh Sep 28 at 10:54
 
When you called new you never stored the arguments anywhere. You only stored an empty hash ref, {}. –  TLP Sep 28 at 10:55
1  
That is because you set $self->{last_name}, but you return $self->{$last_name}. –  TLP Sep 28 at 11:02
1  
@Nitesh If you did $self = @_ then you have not understood my answer, and I encourage you to read that part again. –  TLP Sep 28 at 11:03
1  
@Nitesh You do not need to supply arguments to get_names because you are getting the names from the object, and the object is stored in $obj. –  TLP Sep 28 at 11:05
show 7 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.