2

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."
3
  • 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. Commented Sep 28, 2013 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. Commented Sep 28, 2013 at 11:07
  • It is my mistake. Really sorry. Next time it will not happen Commented Sep 28, 2013 at 11:09

1 Answer 1

2

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}.

Sign up to request clarification or add additional context in comments.

11 Comments

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"
When you called new you never stored the arguments anywhere. You only stored an empty hash ref, {}.
That is because you set $self->{last_name}, but you return $self->{$last_name}.
@Nitesh If you did $self = @_ then you have not understood my answer, and I encourage you to read that part again.
@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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.