Stories
Slash Boxes
Comments

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

chromatic (983)

chromatic
  (email not shown publicly)
http://wgz.org/chromatic/

Blog Information [technorati.com] Profile for chr0matic [technorati.com]

Journal of chromatic (983)

Monday April 25, 2005
02:46 AM

B::XPath

[ #24356 ]

Why call walkoptree() yourself (see B.pm) when you have the power of XPath (at least as much as Class::XPath supports?

#!perl

use strict;
use warnings;

use B::XPath;

use vars qw( $foo $bar );

sub some_sub
{
    my $x = shift;
    $foo  = $x;
    print "\$x is $x\n\$foo is $foo\n";
}

my $node = B::XPath->fetch_root( \&some_sub );

for my $bar ($node->match( '//gvsv[@NAME="foo"]' ))
{
    printf(
        "Found global '%s' at %s:%d\n  (defined at %s:%d)\n",
        map { $bar->$_ } qw( NAME find_file find_line FILE LINE )
    );
}

I'm sure you're on the edge of your seat for the output:

$ perl find_global_name.pl
Found global 'foo' at find_global_name.pl:13
  (defined at /usr/lib/perl5/5.8.6/vars.pm:35)
Found global 'foo' at find_global_name.pl:14
  (defined at /usr/lib/perl5/5.8.6/vars.pm:35

There are two drawbacks (besides the fact that it's a proof of concept and not releasable yet): Class::XPath has little axis support and you have to know an awful lot about the structure of the optree for which you want to search. I think the latter is solvable, but it will require more thought.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.