multiple modifiers: a possible solution

Discussion in 'Perl Misc' started by ioneabu@yahoo.com, Jan 23, 2005.

  1. Guest

    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;
    }
    , Jan 23, 2005
    #1
    1. Advertising

  2. Anno Siegel Guest

    <> wrote in comp.lang.perl.misc:
    > 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
    Anno Siegel, Jan 23, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. shila
    Replies:
    3
    Views:
    671
    Tor Iver Wilhelmsen
    Feb 24, 2004
  2. Razvan
    Replies:
    11
    Views:
    893
    Andrew Thompson
    Jul 17, 2004
  3. Indudhar
    Replies:
    4
    Views:
    363
    Patrick May
    Oct 21, 2005
  4. Miquel

    Class & modifiers modifiers

    Miquel, Jan 25, 2007, in forum: Ruby
    Replies:
    4
    Views:
    160
    Miquel
    Jan 26, 2007
  5. Replies:
    3
    Views:
    75
    Brian McCauley
    Jan 21, 2005
Loading...

Share This Page