I'm trying to dereference a hash key inside an access method and cannot figure out why it's failing. I keep getting this error "Can't use string ("5") as a HASH ref while "strict refs" in use at Music.pm line 19." If I manually assign the value in the method it works and in another method I can print the value using the same syntax. The big picture is that I want to pass one value to the constructor and then use that value along with an access method to populate the second value in the constructor. This is possible when I use the hard coded value in the method.
What concept is it that I am not understanding?

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

This is the line in question from the sub add_numbers method:

my $value = $self->{"Number_one"} ;

Here's my user/driver program:

use strict; use warnings; use Music; my $value = Music->new(5); $value->display_values;

Any clues on what I need to study?


In reply to 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.