[EMAIL PROTECTED] wrote:
I was thinking over your idea this morning, in the context of Perl's automatic context handling. I was wondering if it might be a good idea to return a different value depending on the context the loop is assigned in. For example:
$loopmax = for (@etc) {....}
@looparray = for (@etc) {....}
%loophash = for (@etc) {....}
I was thinking this would set $loopmax to the number of times the loop ran, @looparray to an array of the last values worked with in the loop (Similar to the operation of a sub, except for each iteration. 'return' should of course be available to set a particular value.), and $loophash would be a hash with the key being the index value ($_ for the iteration) and the value being the same value put in the array.
This might be extremely good. I've also been thinking what the default returned values should be but that's hard imagine.
I think there might be some problems with this, too.
For example if You wrote: $value= foreach(1 .. 3){ retlast $scalar if $scalar == 3; }
, You would get answer 3 but it's a bit ambiguous because You can not now what returned it.
I think the default returns are not as controlled as returns via retnext and it'd get messy if it'd be possible to use both ways. Default returns make the constructs forcibly to act more or less like filter ie to return (defaultly at least) as many values as the loop spins. Without default returns it'd be more natural to make the constructs to just roll over returning values only when the programmer wants to - and by that way the performance wouldn't downgrade at all if returns aren't used.
The default returns still are very interesting and You had very good ideas about them. I'm really not sure should they somehow be included in this or not. The tagging was also suggested and it sounded good - i don't still understand everything about it.
We don't have this problem with If's because they already return default values automatically - and i think it's just the best possible way with them. In addition it's possible to return more values by retnexts.
I suppose it's possible to return hashes from these constructs already (have You tried it with the script attached to my last mail before this?). Just return a pairs of scalars and put them to hash instead of array.
If you wanted the 'last' return value in the loop you could always use $looparray[-1] (the last value in the list), or it might be useful to provide a special syntax for that value. (Since it would be extremely common, and the $looparray[-1] solution has performance implications, especially with long running loops.)
Of course, the standard interpolation would work as well:
@looparray = for (@etc) { return ( $val1, $val2 ); } would return an array of array references. (And a bigger argument for special syntax to return only one value...)
There should be no problems to return array references now.
The hash return value is the most interesting to me: it allows the creation of certain types of hash very easily. For instance, I have one subroutine in a module I've written with 6 arguments, which I put into a hash, like so:
%note{value} = $_[0];
%note{lang} = $_[1];
%note{location} = $_[2];
%note{server} = $_[3];
%note{bank} = $_[4];
%note{bankurl} = $_[5];
With the above loop syntax, I could write:
my @argvals = ( 'value', 'lang', 'location', 'server', 'bank', 'bankurl' ); %note = foreach (@argvals) {$_[$i++];};
You are right. This would be great. But if one value-pair should be excluded there'd be problems. That's why i think it might be better the way it's now:
%note = foreach (@argvals) {retnext $_,$_[$i++];};
It's more to write, but also much more flexible. If You want to return only 4 value-pairs, You could alter the code just a little bit (i haven't tested these examples):
%note = foreach (@argvals) {retnext $_,$_[$i++] if($i < 4);};
This (i believe) works now. It's hard to say which one is better because You have to write more now but have more control. Or could there be better ways than these? What do You think?
($i++ might be replaceable by $_ in the last, I'd have to check.)
This is much better readability from my standpoint (especially considering in the actual subroutine I'd separate the two statements by half a page of class initization necessary before I can get to the arguments...), and it has the advantage that I can add and remove arguments quickly by just changing the array.
Just thought I'd run this by you, as a way to extend your idea. Daniel T. Staal
I've been so "hurry" that I have made just one script with the translator - the script that was included in the translator's attachment - so it's easily possible that You have more practical experience than i. I'll also test it more and look what Your ideas feels when i get more experience with this. It'll also be easier to install and test if this was a CPAN-module so i try to get so far someday.
--- ville jungman, 2 laureston crescent, tower, blarney, cork, ireland tel. + 353 - 21 - 451 6847, http://www.kolumbus.fi/vilmak usko Herraan Jeesukseen, niin sinä pelastut. (apt. 16:31)
_________________________________________________________________
STOP MORE SPAM with the new MSN 8 and get 2 months FREE* http://join.msn.com/?page=features/junkmail
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]