On Tue, 15 Jan 2002 00:02:55 -0500, Michael G Schwern wrote:

>> As for style... I prefer if(..) { ... } elsif (..) { ... } like this:
>
>Going for short, not pretty.  The && approach is about half a line
>shorter.

It's really the repetition of the "next" that is the thorn in my eye.
And "do BLOCK" is not one of my favourites either.

>With your printf trick in place its down to:
>
>#!/usr/bin/perl -w # run this file with perl -x
>seek(DATA,0,0);for(<DATA>){/^#!/&&last;/(\w+):/&&print("--== $1 ==--\n")&&
>next;/(\w+)[ ]+ (\w+)(?:[ ]+(\d+))?/&&printf("%-8s: %s std_logic%s;\n",$1,
>$2,length$3&&"_vector($3 downto 0)")&&next}
>__DATA__

Oops, length $3 with no $3 returns "0".

You can go for ?: instead of if/else. Shorter still. And I had forgotten
about that "open 0" trick. So here is version A:

open 0;while(<0>){/^#!/&&last;print/(\w+):/?"--== $1 ==--\n":
/(\w+)[ ]+ (\w+)(?:[ ]+(\d+))?/?sprintf("%-8s: %s std_logic%s;\n",$1,
$2,defined$3&&"_vector($3 downto 0)"):$_};

If you don't need the default (look at the operator just in front of the
"sprintf"):

open 0;while(<0>){/^#!/&&last;print/(\w+):/?"--== $1 ==--\n":
/(\w+)[ ]+ (\w+)(?:[ ]+(\d+))?/&&sprintf("%-8s: %s std_logic%s;\n",$1,
$2,defined$3&&"_vector($3 downto 0)")};


As for the do: did you know that the RHS of a s///e may contain
semicolons? In other words: you don't need a "do BLOCK" there. It acts
as if everything is wrapped in one.

I don't think you can use it here, but I think that it is cute.

        $_ = "I've got 2 bottles of beer on the wall.\n";
        s/(\d+) bottles?/my $i = $1-1; $i!=1 ? "$i bottles" :
           "$i bottle"/ge;
        print;

-- 
        Bart.

Reply via email to