Question : bless perl

ok, i'm in dummy mode at the moment. i'm in need of somwe reminding of whats wrong..

here's my module code:

sub new {

        my $class = shift;
        my $self =
                {
                        test => 'test',
                };
        bless $self, $class;
        return $self;

}

sub getValue {

        my ( $self, $key ) = @_;
        return $self->{'$key'};

}

sub setValue {

        my ( $self, $key, $value ) = @_;
        $self->{'$key'} = $value;

}


here's my script code:

use Web::WebBatch;


my $WebBatch = new Web::WebBatch();
$WebBatch->ExecuteTest();


my $test = $WebBatch->getValue('test');
print "accessor test:$test\n";


$WebBatch->setValue('test', 'insert');
$test = $WebBatch->getValue('test');
print "accessor test:$test\n";




here's my results:

accessor test:
accessor test:insert


why isn't it:
accessor test:test
accessor test:insert

Answer : bless perl

sub getValue {

       my ( $self, $key ) = @_;
       return $self->{'$key'};

}

sub setValue {

       my ( $self, $key, $value ) = @_;
       $self->{'$key'} = $value;

}

Variables don't intepolate in single-quoted strings. Best to just leave the quotes off completely.

sub getValue {

       my ( $self, $key ) = @_;
       return $self->{$key};

}

sub setValue {

       my ( $self, $key, $value ) = @_;
       $self->{$key} = $value;

}
Random Solutions  
 
programming4us programming4us