develooper Front page | perl.perl6.language | Postings from May 2004

Re: C style conditional statements

Thread Previous | Thread Next
From:
Luke Palmer
Date:
May 12, 2004 11:46
Subject:
Re: C style conditional statements
Message ID:
20040512184600.GA8212%luke@luqui.org
Aaron Sherman writes:
> Right off the bat, let me say that I've read A1-6, E7, A12, S3, S6, E1,
> E6 and much of this mailing list, but I'm still not sure that all of
> what I'm going to say is right. Please correct me if it's not.

Did you really need to ask me to? ;-)

> Perl 5:
> 
>         #!/usr/bin/perl
>         
>         while(<>) {
>         	s/\w+/WORD/g;
>         	print;
>         }
> 
> Perl 6:
> 
>         #!/usr/bin/perl
>         
>         while $stdin.getline -> $_ {
>         	s:g/\w+/WORD/;
>         	$stdout.print;
>         }

It actually turns into:

    for <> {
        s:g/\w+/WORD/;
        print;
    }

Which looks and feels even more like Perl (and not like Java...).  I'll
explain below.

> is it really that new and scary? The wonky old STDIO is probably going
> to go and get replaced with an IO::Handle like interface (I don't think
> that's final, but I recall it being said), but that's NOT new, it's just
> changing over to the long-established and removing the Perl3ish IO
> syntax.

Well, the IO-objects are iterators, and you use <$iter> to iterate.  It
makes sense that <> would iterate over $*ARGV by default.

> The funky -> syntax replaces implicit $_ization of while parameters...
> good change IMHO.

It doesn't *replace* it.  Actually, $_ becomes even more pervasive.  In
a closure block like:

    for @something -> $foo { ... }

BOTH $foo and $_ get bound to each element of @something.  That just
means that $_ is always the current inner topic, as opposed to only when
you have another name for it.

> Other than that it's Perl as you've always known it. Any Perl 5
> programmer should be able to look it over and figure it out with perhaps
> just one or two hints.
> 
> Ok, another:
> 
>         #!/usr/bin/perl
>         
>         use IO::Socket::INET;
>         $n=IO::Socket::INET->new(LocalPort=>20010,Listen=>5);
>         $n->listen();
>         while(($s=$n->accept())){
>         	print <$s>;
>         	close $s;
>         }
> 
> 
> Perl 6:
> 
>         #!/usr/bin/perl
>         
>         use IO::Socket::INET;
>         my IO::Socket::INET $n = (LocalPort=>20010,Listen=>5);
>         $n.listen();
>         while ($s=$n.accept()) {
>         	$stdout.print($s.getlines);
>         	$s.close;
>         }

Here you go:

    use IO::Socket::INET;
    my $n = new IO::Socket::INET: LocalPort => 20010, Listen => 5;
    $n.listen();
    while (my $s = $n.accept) {
        print <$s>;
        $s.close;       # or even close $s;
    }

The second line has a lot of different variants; I opted on the side of
conservatism.

Luke

Thread Previous | Thread Next


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