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;

In reply to Keeping data in an object by AzaBat

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.