Fellow Monks!

I am trying to construct the most general access method possible for objects. The way I see it, the maximum uses of one method accessing array data is three:

1) Pushing the input into the data.
2) If an array return walue is whanted, and no argument is given, return a copy (or ref to) the array.
3) Under the same conditions as 2) but a scalar is asked for, return the number of elements in the array (or the last index of it).
However, this presents a problem for me, since the following code does not give me what I want under 3):
package Labels::Level; use Carp::Datum (":all", $ENV{DATUM}); sub new{ DFEATURE my $f_,"Creating new Level"; my $invocer =shift; my $class = ref($invocer) || $invocer; my $self = {xmin => 0, xmax => 0, name => "", labels => [], @_}; bless($self,$class); return DVAL $self; } sub labels{ DFEATURE $f_; my $self = shift; if(@_){ my $inRef = shift; push @{$self->{'labels'}},$inRef; } else{ return DARY @{$self->{'labels'}} if wantarray; } my $out = @@{$self->{'labels'}}?$#{$self->{'labels'}}:0; return DVAL $out; } package main; use Labels::Level; $main::lvl=Labels::Level->new(xmax=>10,xmin=>0,name=>'myname'); $main::lvl->labels("labelref"); $main::lvl->labels("labelref2"); @main::out = $main::lvl->labels; print "@main::out"."::::".$main::lvl->labels."\n";
This always gives me:
labelref labelref2::::0
Can anyone tell me why, and what to do about it..
I am of course happy for all the help I can get!
/Dargosch

In reply to Array troubles.. (Making a three-way method..) by dargosch

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.