I'm seeking some help about object Orientated Programming in Perl. I am not new to Perl and have written a number of standalone packages with methods but never really using OOP techniques. If I use some incorrect terminolgy please put me right.

I have a class called User and a constructor which creates a User object given the username. The User class has a login method which logs you into a system given the password and saves an instance variable which is the unique session id required to perform any further actions when logged in. ok so far.

Each user can have multiple bank accounts so the User class also has a method to return a list of accounts once logged in.

Lastly, the User class has an open_account method which opens a bank account given the account number and returns a User::Account object.

The User::Account object returned can be used to say query the balance so it has a balance method. However, all communication with the server needs to be done using a single call to the server which is passed an API name and all calls to the server once logged in need to pass the session id (lets call this method "get" which User::login would also use but this would be the one case where the session id is not required).

Logically, to me, the "get" method belongs in the User class as it is common to all User::*. This means the User::Account::balance method needs to call User::get but User::Account does not know the session id or server info that User::login obtained.

How would one share the session id and server info obtained in User::new and User::login and store in instance variables of User with User::Account?

Package User sub new { my ($class, $username) = @_; my $self->{username} = $username; # connect to server and store connection details in $self->{server} bless $self, $class; } sub get { my $self = shift; # call server using $self->{server} adding session_id if we have one } sub login { my $self = shift; $self->get('login'); # will store session_id in User object } sub get_accounts { my $self = shift; return $self->get('accounts'); } sub open_account { my ($self, $account_id) = @_; my $self->{account} = User::Account->new(account_id); return $self->{account}; } Package Account our @ISA = qw(User); sub balance { # needs to call User::get but does not have session id or server inf +o }

In reply to Perl OOP help for OOP newbie by Anonymous Monk

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.