When you needed an array ref, you used a function. When you needed a hash ref, you used a scalar. If that's all you want, there's no problem. Just have the function always return an array, and have the scalar always contain a hash reference.

Perhaps what you really what is

@{ $gimme_a_ref } = ( 1, 2, 3 ); $gimme_a_ref->{haha} = 'hihi';

or

@{ gimme_a_ref() } = ( 1, 2, 3 ); gimme_a_ref()->{haha} = 'hihi';

In other words, perhaps what you really want is to return different references based on the type of reference needed. I don't know if there's a term for that, but it's not auto-vivification.

In the case of first syntax I presented, operator overloading should do the trick. Check @{} and %{} in overload.

In the case of second syntax I presented, you could simply return an object with @{} and %{} overloaded (like for the first syntax), or you could mimic WANT. (I hope I have the module name right, I don't have access to CPAN to check.)


In reply to Re: Autovivifying XS routine by ikegami
in thread Autovivifying XS routine by Anno

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.