I was playing around with classes within Perl and trying to implement some of the patterns I've used in the past when I hit a road block. I'm still relatively new to Perl, and I can't seem to figure out what is going on here with this State pattern.
#### a driver for the car { package Driver; sub new { my $class = shift; my $vars = {}; $vars->{ALERTNESS} = 0; $vars->{STATE} = dstatenorm->new(); bless ($vars, $class); return $vars; } sub alertness { my $vars = shift; my $state = $vars->{STATE}; if(@_) { $vars->{ALERTNESS} = shift; } return $state->alertness( $vars->{ALERTNESS} ); } 1; } #### driver state normal { package dstatenorm; sub new { my $class = shift; my $vars = {}; $vars->{"NAME"} = "Normal"; bless ($vars, $class); return $vars; } sub alertness ($) { my $curAlertness = shift; return $curAlertness; } 1; }
When I call the "alertness" routine on the driver, I get dstatenorm=HASH(0x1832734). It looks like I'm getting close to the return I want, but I'm not sure where or how or if I have to dereference to make this work. Thanks in advance!

Update: Found it. I was shifting and the first value is the class itself. Shifting twice did the trick, so I guess I have to access @_ directly?


In reply to Implementing State pattern by Justin_BSI

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.