I'd like to have a class sub 'Arr', that if passed a param, it would return $p->{Arr}$param,
ELSE, I'd like it to return @$p-{Arr}, in such a way that if I'm using it in a scalar context, it gives me the # of elements in that array, but if I used it in a context needing a reference, like push @{$p->{Arr}}, "val", it would 'do the right thing'...

I can create an ugly hack where I can force it to return a ref, by using 'wantarray', to send it the message and use "push @{($p->{Arr},)[0]},val", -- and to get the scalar context, always be sure to put in 0+$p->{Arr}, but that's is a seriously ugly and special-cased hack.  What I really want is a 'wantref' function that I can use in the 'Arr' func to determine whether or not to pass back the ref or not.

Originally I had:

    sub Ts { $_[1] ? $_[0]->{Ts}[$_[1]] : $_[0]->{Ts} }

(Note: 'subname' 'varies', I used 'Arr' for the general case in my description).  That didn't do anything good...in a printf "%d", $p-{Arr}, it always returned '1' (even though the array had 3 elements).  To get that i had to use:

    sub Ts { $_[1] ? $_[0]->{Ts}[$_[1]] : 0 + $_[0]->{Ts} }

Of course that doesn't work for the ref... Then I tried the ugly hack:

    sub Fans { $_[1] ? $_[0]->{Fans}[$_[1]] : wantarray ? $_[0]->{Fans}:0+@$_[0]->{Fans} }

That does what I described above under 'ugly hack'...i.e. I can get the ref back and use it in push by embedding it a list, and get the #elements back by adding '0' to it where I call it...

So how do I get it to work without the "smarmy" syntax...i.e. get a func that returns a ref or a scalar depending on how it's called?

Is there a 'wantref' func that I don't know about that has some odd name?

Thanks!

 

p.s. Hey, what's with P.M. not recognizing &;GT; in code and xlating it to ">"? Doesn't HTML convert text entities everywhere?

In reply to How to determine if 'ref' is wanted? (ala 'wantarray') by perl-diddler

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.