multiple modifiers: a possible solution

I

ioneabu

Sorry if the formatting gets messed up. I am not on my own computer
now and Google is the only way I can post this from here. Try this
out:

#!/usr/bin/perl

use warnings;
use strict;

my @a = ('a','b','c');
my @b = ('1','2','3','4','5');
my $c = 1;
my $d = multimod('print for @a for @b if $c');
eval $d;
print "\n$d\n";

sub multimod
{
my $mods = "if|for|while|unless";
my $a = shift;
my ($b,$c,$d);
while ($a =~ /^(.*?)($mods)(.*?)($mods)(.*)/)
{
$c = $d = '';
if ($4 and $5)
{
$a = "$4$5";
$c = $4;
$d = $5;
}
if ($1) {$b = "$2($3) {$1}"}
else {$b = "$2($3) {$b}"}
}
return "$c($d) {$b}" if $c and $d;
}
 
A

Anno Siegel

Sorry if the formatting gets messed up.

Predictably it did.
I am not on my own computer
now and Google is the only way I can post this from here. Try this
out:

#!/usr/bin/perl

use warnings;
use strict;

my @a = ('a','b','c');
my @b = ('1','2','3','4','5');
my $c = 1;
my $d = multimod('print for @a for @b if $c');
eval $d;
print "\n$d\n";

Yes, that may work, but it's beside the point. The point of combining
statement modifiers is elegance, (or at least tightness) of code. Calling
a sub and eval'ing the result doesn't give us that.

Your example shows that it can be done in Perl, but there never was much
doubt about that.

[code snipped]

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top