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 | |
by AzaBat (Acolyte) on Jun 29, 2003 at 01:57 UTC | |
|
Re: Keeping data in an object
by antirice (Priest) on Jun 29, 2003 at 00:17 UTC | |
|
Re: Keeping data in an object
by artist (Parson) on Jun 29, 2003 at 03:45 UTC |