Re: Quoting non-numbers
From: Ben Morrow (usenet_at_morrow.me.uk)
Date: 03/11/04
- Next message: Ben Morrow: "Re: Deleting a folder recursively"
- Previous message: Ben Morrow: "Re: perl on windows"
- In reply to: Anno Siegel: "Re: Quoting non-numbers"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 11 Mar 2004 01:07:03 +0000 (UTC)
Quoth anno4000@lublin.zrz.tu-berlin.de (Anno Siegel):
> Ben Morrow <usenet@morrow.me.uk> wrote in comp.lang.perl.misc:
> >
> > Quoth "John W. Krahn" <krahnj@acm.org>:
> > >
> >
> > {
> > no warnings 'numeric';
> >
> > > s/(\S+)/ $1 eq $1 + 0 ? $1 : "'$1'" /eg;
> >
> > }
>
> That quotes 1.0. Not to mention leading zeros.
>
> I have been using the "+ 0" test in this form:
>
> sub is_num {
> use warnings FATAL => 'numeric';
> no warnings 'void';
> eval { shift + 0 };
> not $@;
> }
>
> That reflects Perl's opinion about a string being a number, but it doesn't
> inline easily.
If you're going to do that, you might as well use
Scalar::Util::looks_like_number, which I'd imagine would be somewhat
faster.
Ben
-- It will be seen that the Erwhonians are a meek and long-suffering people, easily led by the nose, and quick to offer up common sense at the shrine of logic, when a philosopher convinces them that their institutions are not based on the strictest morality. [Samuel Butler, paraphrased] ben@morrow.me.uk
- Next message: Ben Morrow: "Re: Deleting a folder recursively"
- Previous message: Ben Morrow: "Re: perl on windows"
- In reply to: Anno Siegel: "Re: Quoting non-numbers"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|