And here is an "inside-out" version of the same. The attributes have their own hashes that are not exposed to the outside world, so the nasty users can only access them by calling a method. The "object" that they see is the address of a 'my' variable, which is useless to them.
#class Address using inside-out objects package Address; use Scalar::Util qw(refaddr); use strict; use warnings; my %street; my %city; my %state; my %zip; #constructor sub new { my ($class, $street, $city, $state, $zip) = @_; # Get any old (unique) reference my $self = bless \do{my $gash}, $class; my $key = refaddr $self; # Extract the address $street{$key} = $street; $city {$key} = $city; $state {$key} = $state; $zip {$key} = $zip; return $self; } sub street { my ( $self, $street ) = @_; my $key = refaddr $self; $street{$key} = $street if defined($street); return $street{$key}; } sub city { my ( $self, $city ) = @_; my $key = refaddr $self; $city{$key} = $city if defined($city); return $city{$key}; } sub state { my ( $self, $state ) = @_; my $key = refaddr $self; $state{$key} = $state if defined($state); return $state{$key}; } sub zip { my ( $self, $zip ) = @_; my $key = refaddr $self; $zip{$key} = $zip if defined($zip); return $zip{$key}; } sub print { my ($self) = @_; my $key = refaddr $self; print "Address: $street{$key}\n". "$city{$key}\n" . "$state{$key}\n" . "$zip{$key}\n\n" ; } 1;

In reply to Re^2: object oriented tutorial question by cdarke
in thread object oriented tutorial question by convenientstore

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.