Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

pointy counters

by tobyink (Canon)
on Dec 20, 2011 at 23:13 UTC ( [id://944495]=CUFP: print w/replies, xml ) Need Help??

use Pointy::Counter; my $i = counter; while ($i --> 10) { print "Counter is $i\n"; }

This achieved via some fairly basic (albeit unintuitive) use of overloading. See https://metacpan.org/release/TOBYINK/Pointy-Counter-0.001.

I really need to investigate here to see how feasible it would be to achieve syntax like this:

use Pointy::Counter; while (ctr $i --> 10) { print "Counter is $i\n"; }

Replies are listed 'Best First'.
Re: pointy counters
by choroba (Cardinal) on Dec 22, 2011 at 09:32 UTC
    What about the inverted arrow? :-)
    perl -E '$from = 0; $to = 10; say $to while $to --> $from' perl -E '$from = 0; $to = 10; say $to while $from <-- $to'
Re: pointy counters
by keszler (Priest) on Dec 20, 2011 at 23:35 UTC
Re: pointy counters
by jffry (Hermit) on Dec 20, 2011 at 23:38 UTC

    This is kind of neato, I suppose, but I'm having trouble figuring out why. What's wrong with a for loop and a range operator?

    for my $i (1..10) { print "i = $i\n"; }
      Well, duh. The range operator isn't even nearly pointy enough!
      That is merely point-ish, not pointy :)
Re: pointy counters
by choroba (Cardinal) on Dec 21, 2011 at 08:39 UTC
    This works without any module:
    my $c = 10; print "$c\n" while $c --> 0;

      Yeah, counting backwards works without needing Pointy::Counter. That's why Pointy::Counter only offers support for counting forwards.


        I do not even understand the topic nor the syntax
        perl -e "my $c = 10; print qq!$c\n! while $c --> 0;"
        but I see that also
        perl -e "my $c = 10; print qq!$c\n! while $c ++> 0 && $c < 100;"
        runs fine.. BUT what a notation is this --> or ++> ?
        there are no rules, there are no thumbs..

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://944495]
Approved by ww
Front-paged by cavac
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-25 20:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found