Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Checking whether two variables have the same structure

by derby (Abbot)
on Apr 09, 2002 at 18:23 UTC ( [id://157814]=note: print w/replies, xml ) Need Help??


in reply to Checking whether two variables have the same structure

++ to Corion and broquaint but just TMTOWTDI mode Inline::C:

#!/usr/bin/perl use Inline C; use strict; my $foo = "bar"; my $bar = [qw(foo bar baz)]; my $baz = {foo => "bar", baz => "quux"}; my $x = [ {foo => "bar", baz => "quux"}, {foo => "bar", baz => "quux"}, {foo => "bar", baz => "quux"}, ]; my $y = { foo => ["bar","quux"], baz => ["quux","bar"], }; print q("foo" -> ), describe("foo"), "\n"; print q(\\$foo -> ), describe(\$foo), "\n"; print q($bar -> ), describe($bar), "\n"; print q(\\$bar -> ), describe(\$bar), "\n"; print q($baz -> ), describe($baz), "\n"; print q(\\$baz -> ), describe(\$baz), "\n"; print q($x -> ), describe($x), "\n"; print q(\\$x -> ), describe(\$x), "\n"; print q($y -> ), describe($y), "\n"; print q(\\$y -> ), describe(\$y), "\n"; __END__ __C__ SV* describe( SV *var ) { SV *tmp; if( ! SvROK( var ) ) { return( newSVpvf( "%s", "S" ) ); } else { switch( SvTYPE( SvRV(var) ) ) { case SVt_PVAV: tmp = describe( av_pop( (AV *)SvRV(var) ) ); return( newSVpvf( "Ao%s", SvPV( tmp, PL_na ) ) ); case SVt_PVHV: tmp = describe( hv_iterval( (HV *)SvRV(var), hv_iternext( (HV *)SvRV(var) ) ) ); return( newSVpvf( "Ho%s", SvPV( tmp, PL_na ) ) ); case SVt_PVCV: return( newSVpvf( "%s", "C" ) ); case SVt_PVGV: return( newSVpvf( "%s", "G" ) ); case SVt_PVMG: return( newSVpvf( "%s", "B" ) ); case SVt_RV: tmp = describe( SvRV(var) ); return( newSVpvf( "r%s", SvPV( tmp, PL_na ) ) ); case SVt_IV: case SVt_NV: case SVt_PV: return( newSVpvf( "%s", "oS" ) ); default: return( newSVpvf( "?" ) ); } } }

-derby

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (8)
As of 2024-03-28 09:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found