perl-diddler has asked for the wisdom of the Perl Monks concerning the following question:

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?

Replies are listed 'Best First'.
Re: How to determine if 'ref' is wanted? (ala 'wantarray')
by ikegami (Patriarch) on Nov 07, 2011 at 02:00 UTC

    Even if it's easy to check if the next op in the caller is a dereference, a function that returns any of 1) a reference to the array, 2) the contents of the array, or 3) the number of elements in the array is way too magical.

    Instead of

    @{ $o->Ts } my $ref = \@{ $o->Ts }; my @array = $o->Ts; my $count = $o->Ts;

    You'll get fewer headaches with

    @{ $o->Ts } my $ref = $o->Ts; my @array = @{ $o->Ts }; my $count = @{ $o->Ts };

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

    Cause it saves you a lot of work.

      So you are giving me reasons why you think it shouldn't generally be done. That's fine. I agree.

      But ... I'm still looking for an answer to the question.

      Besides, 'way to magical'...having a function that acts like an array... yeah...right...*NOT*....

      People have functions that grab onto operators and such all the time...those are no less 'magical'. It's call 'syntactic sugar'.... might NOT be something I'd use everywhere, since in an OO setup, you don't usually want to give the 'client' direct access to your array, but in some cases, the efficiency is worth the trade off -- especially if you are your own client and know you'll be "well behaved"...:-)...

      As for :

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

      Cause it saves you a lot of work.

      It also violates the HTML standard.  I.e. it breaks tools that use the standard.  If PM used a keyword that wasn't part of the HTML standard, like 'literal', then there wouldn't be a prob, but redefining standard HTML...UG...  better would be a PM specific tag, like pm_code... then its likely to never conflict with any future reserved words...

       

      Try https://addons.mozilla.org/en-US/firefox/addon/write-area/.

      It brings features to pm, that pm 'should' have builtin, (most sites that have html composition have builtin html editors....but not slashdot, and not perlmonks...)...  perlmonks is more like a social site -- a community site, than a new site -- so IMO, I think it should move toward the 'friendly side' rather than expecting people to write code to post a message...But that' just my feeling...

      At least slashdot is smart enough to recognize plaintext and leave it as such .. whereas here...all formatting is stripped,  and a proprietary formatting language is used...ug!

      About the above addon...to use it...when you get a write box, just right-click and say 'edit in a write area'  brings up a little thml editor right in the window.  (I sorta prefer it when it comes up outside the window as it does on most sites...not sure why it is different here...(same on sysinternals site)...edits inline)...

       

       

       

        I'm still looking for an answer to the question.

        Weird, because I answered it. You need to look at the opcode tree to see if the next op in the caller is a dereference.

        People have functions that grab onto operators and such all the time.

        No idea what that means.

        It's call 'syntactic sugar'

        I didn't say being magical was bad, I said what you wanted was too magical. There is syntactic sugar, and then there is syntactic sludge.


        It also violates the HTML standard.

        So? I'm sure you'll find it's violated in many other ways since this site doesn't accept HTML, but something similar to it.

        Just put a <p> at the front of every paragraph, and put computer text (input, output, code) in <c>...</c> tags.

        If PM used a keyword that wasn't part of the HTML standard, like 'literal', then there wouldn't be a prob,

        Actually, it wouldn't help at all. Those tools you mention would misparse the contents of the tag no matter what it's called.

        At least slashdot is smart enough to recognize plaintext and leave it as such .. whereas here...all formatting is stripped, and a proprietary formatting language is used...ug!

        Your "ug" is misplaced. It should be attached to "all formatting is stripped". Throwing out the baby with the bathwater is not a good solution.

Re: How to determine if 'ref' is wanted? (ala 'wantarray')
by anneli (Pilgrim) on Nov 07, 2011 at 01:08 UTC

    Off-topic: I'm not sure I understand your comments re: &gt;. The <code> and <c> tags aren't "HTML"; they quote all their contents so they'll appear as-is, until the next </code> or </c> respectively, so you can just paste code without worrying about markup.

    Note that <pre> does not get the same special treatment.

      Oh..was afraid of that...since  'code'   is an HTML keyword like'p'...

      , I used an extension where it says 'edit in a write area', that composes in HTML and writes the result.  It works with most sites that take HTML, but I guess it doesn't work here because perlmonks has redefined the 'code' keyword to behave differently than the standard specifies?  Urg...how...'unstandard'  ;-)

      I wonder how much of a bother it would be to use a keyword that ISN'T defined in HTML so we wouldn't be redefining the standard?  Just a thought.

       

        I wonder how much of a bother it would be to use a keyword that ISN'T defined in HTML...

        I use the <c> tag.


        Improve your skills with Modern Perl: the free book.