What concept is it that I am not understanding?

Several, and there are a number of things that you ought to do to make your code clearer, more robust and easier to maintain.

First off, if you are going to accept a number of parameters in a sub you should:

sub mySub { my ($param1, $param2, %bunchOfOptions) = @_;

so that it is clear at a glance what the parameters are that are expected. Although Perl allows great freedom to pass parameters using a vast range of techniques (potentially in the same sub), it also allows you a wonderful range of techniques for shooting yourself in the foot. Make an effort to use a consistent parameter passing technique and your code will work much more reliably.

The first major concept you have missed is that methods intended to be called on blessed references generally must be called on blessed references. In your constructor you call add_numbers before you even have a blessed reference let alone call it using the blessed reference. In fact there are very few occasions when you should call a sub using & in any case.

The second important (and related) concept is that the object is the reference once it has been blessed. $ref contains the object. With your current design you the bless line should precede the add_numbers call.

You ought to use explicit return values rather than relying on the last lvalue. It makes it much clearer that you are returning the value by intent rather than as a side effect. Putting it all together your code should look something like:

use strict; use warnings; package Music; sub new { my ($class, $number1) = @_; my $self = bless {"Number_one" => $number1}, $class; $self->{"Number_two"} = $self->add_numbers (); return $self; } sub add_numbers { my ($self) = @_; return $self->{"Number_one"}; } sub display_values { my ($self) = @_; print "$self->{'Number_one'}\n$self->{'Number_two'}\n"; } 1;

Perl reduces RSI - it saves typing

In reply to Re: dereference hash key object in access method by GrandFather
in thread dereference hash key object in access method by gctaylor1

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.