The gmtime function normally returns a list, and you have to remember what's in what position. However, you can use the Time::gmtime module which makes it return a hash reference instead.

In Perl 6, the function can detect more contexts, and do this built-in. But what about today?

My idea is to return a pseudo-hash. For example, here is a function that returns a bunch of values:

sub FileTimeToSystemTime ($) { my $input= shift; # binary data formatted as an int64 my $output= '.' x 16; # 16 byte structure output. my $result= $f->Call ($input, $output); # ... check $result for errors. my @retval= unpack ("v*", $output); # Year, Month, DayOfWeek, Day +, Hour, Minute, Second, Milliseconds unless (wantarray) { # ... format this into a ctime-like string return $string; } ### here is the interesting part: unshift @retval, \@names; return @retval; }
Now, there is an extra value at the beginning of the array. But that doesn't really change the fact that you need to remember what's in what position—only the actual positions are affected, not the logic or the issue.
my ($names, $Year, $Month, $DayOfWeek, $Day, $Hour, $Minute, $Seco +nd, $Milliseconds)= FileTimeToSystemTime ($int64);
I can ignore the $names variable, or if I'm only interested in the Day, could subscript the array with [4] (not 3).

However, without using a separate form of the function, I could do this:

my $value= \(FileTimeToSystemTime ($int64)); my $Day= $value->{Day}; # ... etc.
That is, I can invoke the power of the pseudo-hash, or ignore it and use the list.

What do y'all think of this idea?

Meanwhile... what's the story of pseudo-hashes in general? I think I saw in the Apocolipse that they are going away in Perl 6 and Larry calls them a failed experiment. What's he got against them? Is some other feature going to take its place (In Perl 6 this function would look for hash context, so it won't need the pseudo-hash).

—John


In reply to Returning A Pseudo-Hash in Array Context by John M. Dlugosz

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.