develooper Front page | perl.perl6.language | Postings from January 2006

Re: Pattern matching and "for" loops

Thread Previous
From:
Austin Hastings
Date:
January 13, 2006 11:46
Subject:
Re: Pattern matching and "for" loops
Message ID:
43C80377.4020809@Yahoo.com
Dave Whipp wrote:

> Today I wrote some perl5 code for the umpteenth time. Basically:
>
>   for( my $i=0; $i< $#ARGV; $i++ )
>   {
>      next unless $ARGV[$i] eq "-f";
>      $i++;
>      $ARGV[$i] = absolute_filename $ARGV[$i];
>   }
>   chdir "foo";
>   exec "bar", @ARGV;
>
> I'm trying to work out if there's a clever perl6 way to write this
> using pattern matching:
>
>   for @*ARGV -> "-f", $filename {
>     $filename .= absolute_filename;
>   }
>
> Would this actually work, or would it stop at the first elem that
> doesn't match ("-f", ::Item)?
>
> Is there some way to associate alternate codeblocks for different
> patterns (i.e. local anonymous MMD)?
>
>
That's given/when. I seem to recall that C<given> and C<for> do not
topicalize the same way, by design, but my recollection may be dated.

If I'm wrong, then:

for ... { when "-f" { ... }}

If I'm right, then there is probably an argument for a standalone "when"
that uses the default topic, or for some sort of shorthand to coerce a
unified topicalization.

=Austin


Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About