Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Pure Perl Internals (with Pure Perl Segfaults)

by diotalevi (Canon)
on Mar 11, 2004 at 23:54 UTC ( [id://336029]=note: print w/replies, xml ) Need Help??


in reply to Pure Perl Internals (with Pure Perl Segfaults)

Why not use B and Devel::Peek and maybe Devel::Pointer::PP? In fact, does this work anyplace that Emily Dickenson in perl doesn't? It looks like Brian was assuming a standard layout in memory but I don't think this is portable to big endian cpus or to compilers with different packing or reordering.
  • Comment on Re: Pure Perl Internals (with Pure Perl Segfaults)

Replies are listed 'Best First'.
Re: Re: Pure Perl Internals (with Pure Perl Segfaults)
by Ovid (Cardinal) on Mar 12, 2004 at 00:04 UTC

    It's pure Perl (only your last example is) and it's the start of a clean, OO method of walking through Perl's structs. Frankly, I can't think of any reason I would use this (Ingy needed a pure Perl method of determining if a scalar contained an integer or a string), but it's fun (to me).

    As for portability, Ingy admits that this is alpha code, but he's using Config to try to make it as portable as possible.

    Cheers,
    Ovid

    New address of my CGI Course.

      Eh. Sure B is compiled but by being so, it doesn't have to guess at how the compiler that constructed the interpreter arranged things in memory nor does it have to care about big vs little endian. If you do it in pure perl then you have to solve both of those problems as well.

      ... Oh I see. That's nicely done.

        Well, considering that pack and unpack were written with exactly this sort of thing in mind in the first place... :-)
      Ingy needed a pure Perl method of determining if a scalar contained an integer or a string
      While his results are cool, he could have saved a lot of work if he'd asked around to see if anyone had already found a solution to this problem (note the unary "~", it's important)
      sub is_integer { ~$_[0] !~ /\D/ }
      Here are some tests to demonstrate that it works:
      #!/usr/bin/perl -wT use strict; use Test; BEGIN { plan tests => 345 } for my $num (-56789, -300, -1, 0..100, 5345, 6574572, 23457356) { ok( is_integer($num) ); my $string = "$num"; ok( not is_integer($string) ); } for my $string ((map { chr($_) } 0..127), 'dog', 'cat', 'mouse') { ok( not is_integer($string) ); } sub is_integer { ~$_[0] !~ /\D/ }
      I found the is_integer() implementation in my cool-snippets-from-perlmonks directory, but can't seem to find the original thread.....

      -Blake

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2024-04-19 15:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found