0

I have implemented a class called cell and trying to store a char to it. I find that there is a problem to retrieve the char with my setter and getter.

#This is the detail of the class:      
package Cell;
sub new{
  my $class = shift;
  my $self = {  
    content => -1,
  };
  return bless $self, $class;
}

#This is my setter and getter  
sub setContent{
  my ($self,$ch) = @_;
  $self->{content} = $ch;
  print("stored char".$self->{content}."\n");
} 

sub getContent{
  my $self = @_;
  print("passing back char".$self->{content}."\n");
  return $self->{content};  
} 

# this is the way i used the class
my $ch = "a";
my $cell = Cell->new();
$cell->setContent($ch);     
my $testvar = $cell->getContent();
print("test start\n".$testvar);
print("test end\n");
#

I find that I am able to pass the char to the setter and should be stored in $self->{content}, but when trying to retrieve char using getter, $self->{content} become empty

Premlatha
  • 1,676
  • 2
  • 20
  • 40
Kent Wong
  • 143
  • 8

1 Answers1

3

The one direct error is the line in your "getter" getContent method:

my $self = @_;  # assigns number of elements in the array to $self!

This attempts to assign an array to a scalar, what doesn't make sense as it stands. What is done instead is that the array is evaluated to the number of its elements and that is assigned to $self. So in the rest of the method there is no object in $self but only a number, here 1.

If you enabled warnings via use warnings; on the top of the program, you'd receive

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

for the line where you attempt to call a method on $self, telling you that the expected object (hashref) is in fact a string. This gives you the exact line and an informative, if terse, diagnostic message. Without the warnings enabled the code quietly proceeds, but can't do as expected.

The moral:   Please always have use warnings (and use strict) at the beginning

The fix is to make it a "list assignment" -- assign in the list "context"

my ($self) = @_;

where the list context is imposed on the assignment operator = by (...) on the left hand side. Now elements in the array @_ are assigned to variables listed in (), one for one. There is just one variable on the LHS here ($self) so the first element of @_ is assigned to it and the rest, if there were any, discarded.

Or, assign a single element of @_ to $self in a scalar assignment. In object oriented code this is often done using shift, so to also remove the object from the argument list for later convenience

my $self = shift;

where shift in a subroutine by default works on @_.

See context and Scalar vs List Assignment Operator.

I'd recommend to always write a class in its own .pm file, which is then used in a program. For error handling in packages see Carp.

zdim
  • 64,580
  • 5
  • 52
  • 81
  • See also: https://stackoverflow.com/questions/2126365/whats-the-difference-between-my-variablename-and-my-variablename-in-perl – mob Apr 02 '19 at 14:51