AzaBat has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to figure out how to keep data in an object so it can be accessed in other parts of the program.

I have a sample script that is using a module, saved as Test.pm. I pass some data to one method, then to another. I want to use the data I passed earlier in yet another method, but it's not there.

What do I need to do to make the data accessible or how do I access it? Something is lacking from my understanding and I just can't figure it out. print $test->full_name; doesn't print the first or the last name.

The script:

use strict; use Test; my $test = Test->new; print first_name('Joe'); print last_name('Blow'); print $test->full_name; sub first_name { my $first = shift; # Do things with first name $test->first_name(first => $first); } sub last_name { my $last = shift; # Do things with last name $test->last_name(last => $last); }

The module

package Test; use strict; sub first_name { my $self = shift; $self = {@_}; return "First Name: $self->{first}\n"; } sub last_name { my $self = shift; $self = {@_}; return "Last Name: $self->{last}\n"; } sub full_name { my $self = shift; return "Full_Name: $self->{first} $self->{last}\n"; } sub new { my $class = shift; my $self = {}; bless ($self, $class); return $self; } 1;

Replies are listed 'Best First'.
Re: Keeping data in an object
by broquaint (Abbot) on Jun 29, 2003 at 00:40 UTC
    You need to change the name of your module as it conflicts with the standard module of the same name Test (or use lib, but a name change is probably the more sensible option). Once you've sorted that out you might also want to change your methods to something a bit more sensible like
    sub first_name { my( $self, $args ) = ( shift, {@_} ); $self->{ first } = $args->{first}; return "First Name: $args->{first}\n"; }
    As this way you aren't re-assigning the object's reference (not that it affects the object itself) and you're also assigning the specified arguments passed in to $self so that the full_name method will behave as expected.
    HTH

    _________
    broquaint

      Thanks broquaint & antirice, that answers a lot of the questions in my head. I thought I might be clobbering my blessed $self. I just couldn't figure a way stop it.

Re: Keeping data in an object
by antirice (Priest) on Jun 29, 2003 at 00:17 UTC

    Well, let's take a look. $self within first_name is scoped locally. It is set to a blessed ref of class Test which is passed to first_name as a parameter. On the next line, you change $self to an anonymous hash with the values contained within @_. In the process, you lose the blessed ref and thus are no longer working with your object. Therefore, you lose your data once you leave the sub. Try something like this instead:

    sub first_name { my $self = shift; $self->{'first'} = shift; return "First Name: $self->{first}\n"; }

    And call it like this:

    $test->first_name("bill");

    Hope this helps. Note that you need to do this to last_name as well.

    antirice    
    The first rule of Perl club is - use Perl
    The
    ith rule of Perl club is - follow rule i - 1 for i > 1

Re: Keeping data in an object
by artist (Parson) on Jun 29, 2003 at 03:45 UTC