in reply to Parse float from string

I recently banged my head on exactly this problem, and tye pointed out that Perl has it own internal routine for deciding whether a scalar is a valid number.

It seems a shame that this internal routine is not exposed to the programmer, and I made an attempt to gain access to it (under AS) using Win32::API to access the internal routine looks_like_number() function in perl56.dll directly. Unfortunately, it proved impossible (for me anyway) to gain access to the address of a scalar in a form that it would accept from within perl. Tye kindly tracked this down to being something to do with it needing a thread-safe verion of the pointer.

I believe that it would be possible to write a small XS module to expose the function, which would make a lot more sense than everyone having to rewrite it's logic using regexs or whatever, but I am not yet ready to go delving into the excesses of XS.

Any takers?

Maybe they will expose it in a later version of Perl. Who knows.


Nah! Your thinking of Simon Templar, originally played by Roger Moore and later by Ian Ogilvy

Replies are listed 'Best First'.
Re: Re: Parse float from string
by mpeppler (Vicar) on Nov 05, 2002 at 01:04 UTC
    The XS code is trivial (aside from the name of the module - I used a dummy for illustration purposes :-)):
    #include "EXTERN.h" #include "perl.h" #include "XSUB.h" MODULE = Foo::IsNum PACKAGE = Foo::IsNum int looks_like_number(sv) SV *sv
    However I don't think this would quite solve the problem.
    In particular it returns false when testing the string "123abc" (which makes sense, this isn't a number). So to extract the number I think you still need to consider using a regexp.

    email me or /msg me if you'd like to have the code to play around with...

    Michael

      Your right. It wouldn't help a great deal in this particular case.

      The problem I had trying to access it without resorting to XS came (I was reliably informed) because it (or at least the version exported by perl56.dll under AS/build633multi-threaded) requires that it is passed a pTHX_ SV* rather than a simple SV* as shown below and it isn't possible to get at one of those from within Perl itself. I could well be wrong on that, but I failed spectacularly for 3 days to achieve it.

      PERL_CALLCONV I32 Perl_looks_like_number(pTHX_ SV* sv);

      As for the XS. I am not, nor do I wish to be at this point, set up to go through the pain of building my own XS modules. I am managing to keep myself busy trying to learn the in's and out's of perl itself. A process which I am thoroughly enjoying. I don't wish (at this time) to pollute that joy by getting into the world of XS which I have seen several monks who's abilities I much respect, complain publicly (though possibly in part in jest) regarding the vaguries of using XS.

      Thanks a lot for the offer though. I wonder, but don't have enough insentive at this stage to research it, how hard it would be to create a module that would reliably work cross platform?

      Please excuse my ignorance if what you offered would do that, but I mean one that I could install with the need to compile it.


      Nah! Your thinking of Simon Templar, originally played by Roger Moore and later by Ian Ogilvy
        Thanks a lot for the offer though. I wonder, but don't have enough insentive at this stage to research it, how hard it would be to create a module that would reliably work cross platform?
        Well - I don't actually have a Win32 box that I could develop/test this on. And I personally don't have a use for such a module - I did this to illustrate that XS isn't very difficult for simple access to C routines (it can get hairy if you need to really access the perl guts, but that's another story.)

        Michael