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:
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.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; }
I can ignore the $names variable, or if I'm only interested in the Day, could subscript the array with [4] (not 3).my ($names, $Year, $Month, $DayOfWeek, $Day, $Hour, $Minute, $Seco +nd, $Milliseconds)= FileTimeToSystemTime ($int64);
However, without using a separate form of the function, I could do this:
That is, I can invoke the power of the pseudo-hash, or ignore it and use the list.my $value= \(FileTimeToSystemTime ($int64)); my $Day= $value->{Day}; # ... etc.
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Returning A Pseudo-Hash in Array Context
by LD2 (Curate) on Jul 10, 2001 at 05:58 UTC | |
|
Re: Returning A Pseudo-Hash in Array Context
by frag (Hermit) on Jul 10, 2001 at 19:23 UTC | |
|
(tye)Re: Returning A Pseudo-Hash in Array Context
by tye (Sage) on Jul 10, 2001 at 23:34 UTC | |
by John M. Dlugosz (Monsignor) on Jul 11, 2001 at 01:15 UTC |