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 >> perl5-porters
perl5-porters
Re: Every.pm on CPAN
by Abigail other posts by this author
Jul 21 2008 7:46AM messages near this date
Re: Every.pm on CPAN | Re: Every.pm on CPAN
On Mon, Jul 21, 2008 at 09:39:26AM -0500, Ted Zlatanov wrote:
>  On Fri, 18 Jul 2008 16:40:34 -0700 Eric Wilhelm <scratchcomputing@[...].com> wrote: 
>  
>  EW> # from Ted Zlatanov
>  EW> # on Friday 18 July 2008 06:26:
>  
>  >> 2) there's a way to know not just the line, but also the character
>  >> position of a function call, so we can do
>  >> 
>  >> if (every(5) || every(6)) {}
>  >> 
>  >> without resorting to shenanigans like using \5 and \6 or passing a
>  >> special identifier in addition to the number.
>  
>  EW> It looks like the every(6) would miss a count whenever every(5) was 
>  EW> true -- would that be intentional?  Or did you mean '|' instead 
>  EW> of '||' ?
>  
>  The logic is screwy, but I was trying to draw attention to how every(6)
>  will have the same counter as every(5) based on the called line.  You're
>  right this is a bug waiting to bite though :)
>  
>  EW> In any case, how does the calling character position matter if you're 
>  EW> keying by divisor and caller()?  if(every(5) | every(5)) ?
>  
>  Probably a better example would be
>  
>  sub go { foreach (@_) { print if every(5); print if every(6); } }
>  
>  This should print every fifth and sixth member: 5, 6, 10, 12, etc.

And it will print every 30th member twice.

Considering that both

    print if every(5) || every(6);

and

    print if every(5);
    print if every(6);  # Ugly code duplication.

don't print every fifth and sixth member just once, I think that 
using:

    print if ++ $count % 5 == 0 ||
                $count % 6 == 0;

is preferably, as it doesn't surprise people.

I guess 

    print if every(5) + every(6);

would work, but that's not something I'd really like to use.


>  EW> Provide a closure for more advanced usages?
>  
>  EW>   my $fives = Every->new(5);
>  EW>   ... if($fives->());
>  
>  Yes, I thought about that too, but it can be called in tight loops so
>  I'm not sure using objects would be efficient.  I'd need to time it.
>  It's also more verbose and introduces yet another variable, both of
>  which I love to avoid :)


If it's done in a tight loop, tight enough that what you do in the loop
is significant, I'd go for '++ $count % 5 == 0', instead of calling an
extra subroutine.

Of course, if I just wanted to print every fifth member of an array,
I'd write:

    for (my $i = 4; $i < @_; $i += 5) {
        print $_ [$i];
    }


Abigail
Thread:
Ted Zlatanov
Abigail
Ted Zlatanov
Ben Morrow
Roland Giersig
David Nicol
Ted Zlatanov
Ted Zlatanov
David Nicol
Ben Morrow
Ted Zlatanov
Chromatic
Aristotle Pagaltzis
Nicholas Clark
Yitzchak Scott-Thoennes
David Nicol
Ted Zlatanov
Abigail
Ted Zlatanov
Abigail
David Nicol
Ted Zlatanov
Aristotle Pagaltzis
Ted Zlatanov
David Cantrell
David Nicol
Eric Wilhelm
David Nicol
Ben Morrow
Aristotle Pagaltzis
Paul Johnson
David Nicol
Eric Brine
Gisle Aas
David Nicol
Nicholas Clark

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