Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Pattern matching and "for" loops

2 views
Skip to first unread message

Dave Whipp

unread,
Jan 13, 2006, 2:36:22 PM1/13/06
to perl6-l...@perl.org
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)?


Dave.


(ps. if anyone on this list is looking for a $perl_job in Santa Clara,
CA, please contact me by email: dwhipp at nvidia -- no promises, but we
might even pay you to work on p6)

Austin Hastings

unread,
Jan 13, 2006, 2:45:59 PM1/13/06
to Dave Whipp, perl6-l...@perl.org
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

Brad Bowman

unread,
Jan 13, 2006, 8:42:33 PM1/13/06
to perl6-l...@perl.org

On 13/01/06 20:36, Dave Whipp wrote:
> Is there some way to associate alternate codeblocks for different
> patterns (i.e. local anonymous MMD)?

Is it possible to have an anonymous multi sub?
This would seem to require new syntax for combining two anonymous
definitions.

Of course we want everything to be first class, at least for feature
bragging rights.

Lexically scoped multi subs wouldn't require more syntax but I'm
not sure how two multis of different scope types are combined.
What counts as a fully redefinition, as partial redefinition and
as an extension?

It's 2am so ignore all gibberish,

Brad

--
In the words of the ancients, one should make his decisions within the
space of seven breaths. Lord Takanobu said, "If discrimination is long,
it will spoil." -- Hagakure

0 new messages