[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[no subject]
- <!--x-content-type: text/plain -->
- <!--x-date: Sun Aug 1 19:52:05 2004 -->
- <!--x-from-r13: syrgpu ng culqrnhk.bet (Tyrgpu) -->
- <!--x-message-id: [email protected] -->
- <!--x-reference: 1091393720.458.20.camel@devel -->
- <!--x-reference: [email protected] -->
- <!--x-reference: 1091402640.458.24.camel@devel --> "http://www.w3.org/TR/html4/loose.dtd">
- <!--x-subject: [ale] Private members in perl -->
- <li><em>date</em>: Sun Aug 1 19:52:05 2004</li>
- <li><em>from</em>: fletch at phydeaux.org (Fletch)</li>
- <li><em>in-reply-to</em>: <1091402640.458.24.camel@devel> (Chris Fowler's message of "01 Aug 2004 19:24:00 -0400")</li>
- <li><em>references</em>: <1091393720.458.20.camel@devel> <<a href="msg00043.html">[email protected]</a>> <1091402640.458.24.camel@devel></li>
- <li><em>subject</em>: [ale] Private members in perl</li>
[...]
Chris> my @ARA = {};
Chris> my $ref = \@ARA;
Chris>
Chris> bless $ref, $class;
Chris> return $ref;
Chris> }
OK, you've made an array @ARA which contains a single element (that
single element being a reference to an anonymous hash) and then you
return a blessed reference to that array. You probably mean to just
have a new empty array and return a reference to that (my @self;
return bless \@self, $class;) or you want to use the anonymous
arrayref constructor (my $self = []; return bless $self, $class).
Chris> sub get_num { my $self = shift; my @W = $self; return $#W;
Chris> }
Your instance is an array ref, so presuming you want to return the
number of elements contained therein you probably want:
sub get_num { return scalar @{ shift() } }
Or more verbosely:
sub get_num {
my $self = shift;
my $num_elems = @{ $self };
return $num_elems;
}
If you really want the index of the last element (which is what you
were getting; the number of elements in an array is obtained by using
the array in a scalar context (either explicitly as in the first case
or implicitly as in the second)) you would use $#{ shift() } (or $#{
$self } in the verbose version).
Chris> sub append { my $self = shift; push @{$self}, shift;
Chris> }
This should work fine; I'd bet it's the problem with the get_num being
broken that's making it look like its empty. When in doubt, it can
help to use Data::Dumper or YAML to dump out what your instances look
like under the hood (or even just use the x command in the debugger).
--
Fletch | "If you find my answers frightening, __`'/|
fletch at phydeaux.org | Vincent, you should cease askin' \ o.O'
| scary questions." -- Jules =(___)=
| U
</pre>
<!--X-Body-of-Message-End-->
<!--X-MsgBody-End-->
<!--X-Follow-Ups-->
<hr>
<!--X-Follow-Ups-End-->
<!--X-References-->
<ul><li><strong>References</strong>:
<ul>
<li><strong><a name="00037" href="msg00037.html">[ale] Private members in perl</a></strong>
<ul><li><em>From:</em> cfowler at outpostsentinel.com (Chris Fowler)</li></ul></li>
<li><strong><a name="00043" href="msg00043.html">[ale] Private members in perl</a></strong>
<ul><li><em>From:</em> fletch at phydeaux.org (Fletch)</li></ul></li>
<li><strong><a name="00045" href="msg00045.html">[ale] Private members in perl</a></strong>
<ul><li><em>From:</em> cfowler at outpostsentinel.com (Chris Fowler)</li></ul></li>
</ul></li></ul>
<!--X-References-End-->
<!--X-BotPNI-->
<ul>
<li>Prev by Date:
<strong><a href="msg00046.html">[ale] OT: Well it is going to hit the list sooner or later.</a></strong>
</li>
<li>Next by Date:
<strong><a href="msg00048.html">[ale] Re: Google in Suwanee?</a></strong>
</li>
<li>Previous by thread:
<strong><a href="msg00045.html">[ale] Private members in perl</a></strong>
</li>
<li>Next by thread:
<strong><a href="msg00039.html">[ale] Colocation and Atlanta</a></strong>
</li>
<li>Index(es):
<ul>
<li><a href="maillist.html#00047"><strong>Date</strong></a></li>
<li><a href="threads.html#00047"><strong>Thread</strong></a></li>
</ul>
</li>
</ul>
<!--X-BotPNI-End-->
<!--X-User-Footer-->
<!--X-User-Footer-End-->
</body>
</html>