ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> perl6-language
perl6-language
Re: Custom iterators
by Michael G Schwern other posts by this author
Oct 3 2001 7:07PM messages near this date
Re: Custom iterators | Re: Custom iterators
On Wed, Oct 03, 2001 at 01:05:26PM +0200, Bart Lateur wrote:
>  But I really really doubt if there's any computer language in the world

>  that uses underscores in their keywords.

                                 ^^^^^^^^

OH!  Then I *did* explain it badly!

These > >>are not keywords!<<< They're not even magic built-in methods.

They're perfectly ordinary, user-defined methods and functions.
That's the real beauty of it.  It lets any function act as an
iterator.

Perhaps it's because I've been using file examples so much.  Ok, I'll
pick something else...

Here is *literally* how you might write a function which reads a web
page, finds the links and then you get only the ones that are mp3s.

    use LWP::Simple;
    sub eachlink {      # Happy?  No underscores. :P
        my(@urls) = @_;

        # Since it isn't really the point to rewrite the guts of
        # HTML::LinkExtor, I'll do it the really cheesy way.
        foreach my $url (@urls) {
            my $html = get $url;

            while( $html =~ /(http://\S+)/g ) {
                local $_ = $1;
                yield;
            }
        }
    }

The payoff is in how you use that function:

    eachlink(@urls) {
        print "Oooh, musack!" if /\.mp3$/;
    }

This is not only for function calls (as the & prototype is).  It would
work on method calls just the same:

    HTML::Link.eachlink(@urls) {
        print "Oooh, musack!" if /\.mp3$/;
    }

Compare that interface with the current interface to HTML::LinkExtor
and you might get an idea of it's usefulness.


-- 

Michael G. Schwern   <schwern@[...].com>     http://www.pobox.com/~schwern/

Perl6 Quality Assurance     <perl-qa@[...].org> 	     Kwalitee Is Job One

Death follows me like a wee followey thing.
	-- Quakeman


Thread:
Michael G Schwern
Piers Cawley
Michael G Schwern
Bart Lateur
Michael G Schwern
Bart Lateur
Michael G Schwern
Uri Guttman
Michael G Schwern
Brent Dax
Bryan C. Warnock
Michael G Schwern

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved