Link to home
Start Free TrialLog in
Avatar of Peewee
Peewee

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of Dave Cross
Dave Cross
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Peewee
Peewee

ASKER

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
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.
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...
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!
Avatar of Peewee

ASKER

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

thanks Peewee
Any progress on this?

Dave...
Avatar of Peewee

ASKER

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
Just checking you still haven't forgotten :)