Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re: Subroutine Even/Odd

by tobyink (Abbot)
on Oct 04, 2012 at 07:36 UTC ( #997197=note: print w/ replies, xml ) Need Help??

in reply to Subroutine Even/Odd

This construct is weird:

if($number%2==0) { $number = $_; } print "$number";

It works because $_ is initially undef and you never assign to it. It's incredibly fragile though.

Here's how I'd do it...

use 5.010; use List::MoreUtils qw(part); my $numbers = [ 1 .. 10, 211, 1000 ]; my ($evens, $odds) = part { $_ % 2 } @$numbers; say "Even numbers: "; say $_ for @$evens; say "Odd numbers: "; say $_ for @$odds;
perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Comment on Re: Subroutine Even/Odd
Select or Download Code

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://997197]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2015-10-05 04:17 GMT
Find Nodes?
    Voting Booth?

    Does Humor Belong in Programming?

    Results (107 votes), past polls