I wrote a perl script that returns values from 'foreach', 'if-elsif-else' and 'while' -constructs.
You can put perl code with value-returning loops to the beginning of the script. The script translates and evaluates it when it's ran. There's an odd example code :-) in the file but the syntax can be easily learned from it.
The script is attached to this mail. (Please let me know if that's not relevant way to do it in this mailing list.) There's additional information in the file how to use it.
I'd be glad if You told feelings and thoughts about this technique after trying it - but, please, experiment a little bit before hauling it over the doalms (then You're free to do so :) ). And the point is in return values - not how the script is written.
--- 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)
From: Dan Anderson <[EMAIL PROTECTED]> To: Ville Jungman <[EMAIL PROTECTED]> CC: [EMAIL PROTECTED] Subject: RE: Should loops return a value? Date: Sun, 28 Sep 2003 15:34:01 -0400
> I'm not creating a new language - this wouldn't remove anything from Perl
> nor from other languages and thus would be extremaly hard to ruin the whole
> perl with this.
I wasn't implying that you were trying to create a new langauge. I am simply pointing out that there are a huge number of languages out there which were experimental in nature and very innovative. Some of them -- like java -- are still around. Some of them are either no longer used, not used in production environments, or just toys.
Take Haskell for instance. You can return functions from functions and other things. Is this useful for some projects? Probably. Am I going to switch to Haskell because of this? No.
> This only would add possibility to use those values but You could just write
> code and use old scripts like before - and maybe use these few revolutionaly
> :) commands too.
Yes, but I am saying it depends on how useful it is to the average programmer and how easily it cleans up the code to see whether or not it is worth adding. The paradigm is intriguing, but will it really cause enough of a difference in the way we code that it will cause sweeping changes.
Good luck with it though. I would love it if you managed to come up with something to make coding more efficient.
-Dan
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
_________________________________________________________________
The new MSN 8: smart spam protection and 2 months FREE* http://join.msn.com/?page=features/junkmail #!/usr/bin/perl -w
#######################################################################################
#
# This program simulates how some constructs could return values
#
# Supported constructs are while, for, foreach and if-elsif-else
# - If-elsif-else return $_ automatically but that might not be very handy always.
# - Other command return values only with added commands (see below) and that also
# might not be the best solution - but better than nothing still :)
#
# Added commands are 'retnext' and 'retlast'.
# - They returns value to their 'parent'.
# In addition 'retlast' creates a 'last'-command
#
# If You have some ideas how to make this work, please mail to me
# - email:
# [EMAIL PROTECTED]
# (secondary: ville [dot] jungman [at] frakkipalvelunam [dot] fi)
#
# Known bugs... :-)
use strict;
#######################################################################################
#
# Your program goes to $_ below
# - Please note that You have to escape single quotes with a slash: \'
# - Example program isn't commented and that's why it might be hard to follow - so write Your own.
# Also it isn't either useful or readable and it could be written more wisely etc... in fact i just
# writed something when i made the translator.
$_ = '
my $len=undef;
print "Feed me with some strings, please. You can use spaces to separate words. If one of the words is \"len\", I\'ll tell You the word lengths, too. Give an empty line when You\'re ready.\n\n";
"I ate:\n",
(join ",\n",
foreach(
while(my $input=<>){
chomp $input;
last unless $input;
retnext
foreach(split /\s/,$input){
retnext
if(/^len$/){
print "<Ok, I\'ll tell You the lengths>\n";
$len=1;
undef;
}else{
$_
}
;
}
;
}
){
if($_){
retnext
if($len){
"$_ (length == ".length($_).")";
}else{
$_;
}
}
;
}
)
."\n"
;
';
# switch this to 1, if You want to see the code being built my $debug=0;
while(
################# # if, else, elsif
s/ (\W) ( if\s* \([^)(}{]*?\)\s* \{[^}{]*?\}\s* (?: ( else| elsif\s*\([^)(}{]*?\) )\s* {[^}{]*?}\s* )* ) / my $temp=$1."do___SB___$2"."___FB___"; $temp =~ s#([{])#___SB___#g; $temp =~ s#([}])#___FB___#g; $temp =~ s#([(])#___SP___#g; $temp =~ s#([)])#___FP___#g; $temp /sgxe
||
######## #retnext
s/ (\W) retnext\s (.*?); /$1 push [EMAIL PROTECTED],$2;/sgx
||
######## #retlast
s/ (\W) retlast\s (.*?); /$1 push [EMAIL PROTECTED],$2; last; /sgx
||
##################### # foreach, for, while
s/ (\W) ( (?: (foreach|for|while) )\s* \( [^)(}{]*? \)\s* \{ [^}{]*? \} ) / my $temp=$1."do___SB___my [EMAIL PROTECTED]();$2;[EMAIL PROTECTED]"."___FB___"; $temp =~ s#([{])#___SB___#g; $temp =~ s#([}])#___FB___#g; $temp =~ s#([(])#___SP___#g; $temp =~ s#([)])#___FP___#g; $temp; /sxeg
){ if($debug){ print "$_\n#press enter"; <>; } }
s/___SB___/\{/g; s/___FB___/\}/g; s/___SP___/\(/g; s/___FP___/\)/g;
if($debug){ print "#to see the final code"; <>; print; print "#press enter to eval the code\n"; <>; } eval $_;
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]