On Wed, 22 May 2002, Chris Angell wrote:
> Everyone,
>
> Please correct me if I am emailing the wrong address/list. Thanks.
Well, in general, you are mailing the wrong list; however, we do know a
lot of perl 5, so you'll get your question answered :) (This is the Perl
6 list, used for discussing design and features of the new version of
Perl)
> I have an idea for the int() function. I think it would be cool if it
> returned false/undefined when the argument passed to it is a whole number.
> For example:
>
> int(1) or print "argument passed to int() is something other than a
> decimal number";
>
> A friend came up with this:
>
> sub myint { return if $_[0] =~ /\A\d+\z/; $_[0] =~ /^(\d+)/ ? $1 : 0 }
>
> What do you guys think?
That or:
sub myint($) { int $_[0] == $_[0] ? $_[0] : int $_[0] }
That way you don't have the string conversion or the regex match using up
your processor time. It could be made more efficient:
sub myint($) { my $i = int $_[0]; $i == $_[0] ? $_[0] : $i }
Since this is a Perl 6 list, here's how you would do it in Perl 6 (unless
there's a better way):
sub myint($x) { my $i = int $x; $i == $x ? $x : $i }
Luke