bless perl

Peewee
Peewee used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Perl programmer, author and trainer
Commented:
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;

}

Author

Commented:
davorg,

you argument doesn't support the fact that it works using the set & get methods, but doesn't set the initial value of test.

have a close look at it again.

regards Peewee

Commented:
what did you do in your $WebBatch->ExecuteTest()?  Maybe that wiped out your initial test value?  I agree with davorg though about single quotes.  It's surprising to see that actually worked for you.
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

Dave CrossPerl programmer, author and trainer

Commented:
Oh, but I think it does explain everything that is going on. Did you make my suggested changes and try it again?

Let's look at it in more detail.

my $WebBatch = new Web::WebBatch();
# At this point $WebBatch is a ref to a hash containing the key 'test'

$WebBatch->ExecuteTest();
# Don't know what this method does

my $test = $WebBatch->getValue('test');
print "accessor test:$test\n";
# At this point you thing you are asking for the value associated with the key 'test',
# but because of the bug in getValue you are asking for the value associated with
# the key '$key'. As there isn't one, you get back "undef"

$WebBatch->setValue('test', 'insert');
# At this point you think you are setting the value associated with the key 'test',
# but because of the bug in setValue you are setting the value associated with
# the key '$key'.
# Your hash now looks like this:
#  {
#    'test'  => 'test';
#    '$key' => 'insert';
#  }

$test = $WebBatch->getValue('test');
print "accessor test:$test\n";
# Once again, you think you are getting the value associated with the key 'test',
# but because of the bug in getValue you are actually getting the value associated
# with the key '$key'. Because of the bug in setValue, there is now a value associated
# with that key, so you get the value 'insert' back.

Hope this is clearer. To demonstrate the problem even mor clearly, I suggest that you use Data::Dumper to print out the contents of $WebBatch after your call to setValue.

Also try, getting and setting other values. You'll see that you're only ever getting and setting _one_ value (the one with the key '$key').

Dave...

Commented:
I just tried the test script Peewee wrote and realized that you're only getting '$key'.  Your $WebBatch->{test} is still there and working, it's just that your getvalue always gives $WebBatch->{'$key'}.  Davorg was right.
Of course Davorg is right!

Author

Commented:
hmmm,
 seems to be a consensus here, i'll check it out over the weekend..

thanks Peewee
Dave CrossPerl programmer, author and trainer

Commented:
Any progress on this?

Dave...

Author

Commented:
davorg,
my apologies i haven't had time as yet... it may not be to-day pr tommorrow but i won't forget about it...

Peewee
Dave CrossPerl programmer, author and trainer

Commented:
Just checking you still haven't forgotten :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial