Thread: [Parseperl-discuss] Good way to determine if a certain bareword is used?
Brought to you by:
adamkennedy
From: Torsten S. <kaf...@gm...> - 2006-10-11 22:35:00
|
Aloha, I recently started using PPI to begin work on a module that finds out which version of Gtk2/gtk+ a given bunch of code requires. To do that, I need to know if, for example, the bareword "Gtk2::Gdk::DisplayManager" is used anywhere. Here's what I came up with: $document->find_any(sub { $_[1]->isa ("PPI::Statement") and $_[1]->find_any(sub { $_[1]->isa ("PPI::Token::Word") and $_[1] eq "Gtk2::Gdk::DisplayManager" }) }); Is this a good way to express the requirements? Is it correct? Is there a better way? -- Keep up the great work! -Torsten |
From: Chris D. <ch...@cl...> - 2006-10-12 01:18:49
|
Hi Torsten, It's easier than that, I think: $document->find_any(sub { $_[1]->isa('PPI::Token::Word') and $_[1] eq 'Gtk2::Gdk::DisplayManager' }); Chris On Oct 11, 2006, at 5:34 PM, Torsten Schoenfeld wrote: > Aloha, > > I recently started using PPI to begin work on a module that finds out > which version of Gtk2/gtk+ a given bunch of code requires. To do > that, > I need to know if, for example, the bareword > "Gtk2::Gdk::DisplayManager" > is used anywhere. Here's what I came up with: > > $document->find_any(sub { > $_[1]->isa ("PPI::Statement") and > $_[1]->find_any(sub { > $_[1]->isa ("PPI::Token::Word") and > $_[1] eq "Gtk2::Gdk::DisplayManager" > }) > }); > > Is this a good way to express the requirements? Is it correct? Is > there a better way? > > -- > Keep up the great work! > -Torsten > > > ---------------------------------------------------------------------- > --- > Using Tomcat but need to do more? Need to support web services, > security? > Get stuff done quickly with pre-integrated technology to make your > job easier > Download IBM WebSphere Application Server v.1.0.1 based on Apache > Geronimo > http://sel.as-us.falkag.net/sel? > cmd=lnk&kid=120709&bid=263057&dat=121642 > _______________________________________________ > Parseperl-discuss mailing list > Par...@li... > https://lists.sourceforge.net/lists/listinfo/parseperl-discuss > -- Chris Dolan, Software Developer, Clotho Advanced Media Inc. 608-294-7900, fax 294-7025, 1435 E Main St, Madison WI 53703 vCard: http://www.chrisdolan.net/ChrisDolan.vcf Clotho Advanced Media, Inc. - Creators of MediaLandscape Software (http://www.media-landscape.com/) and partners in the revolutionary Croquet project (http://www.opencroquet.org/) |
From: Adam K. <ad...@ph...> - 2006-10-12 02:56:00
|
Torsten Schoenfeld wrote: > Aloha, > > I recently started using PPI to begin work on a module that finds out > which version of Gtk2/gtk+ a given bunch of code requires. To do that, > I need to know if, for example, the bareword "Gtk2::Gdk::DisplayManager" > is used anywhere. Here's what I came up with: > > $document->find_any(sub { > $_[1]->isa ("PPI::Statement") and > $_[1]->find_any(sub { > $_[1]->isa ("PPI::Token::Word") and > $_[1] eq "Gtk2::Gdk::DisplayManager" > }) > }); > > Is this a good way to express the requirements? Is it correct? Is > there a better way? If you are just looking for the word, then the top part isn't necesary. Just this will be enough. $document->find_any( sub { $_[1]->isa('PPI::Token::Word') and $_[1] eq 'Gtk2::Gdk::DisplayManager' } ); I'm not entirely sure why you have the PPI::Statement phrase there, but if you've been copying code from Perl::Critic that might be some sort of an optimisation hack? Adam K |
From: Torsten S. <kaf...@gm...> - 2006-10-12 16:08:12
|
On Thu, 2006-10-12 at 12:53 +1000, Adam Kennedy wrote: > If you are just looking for the word, then the top part isn't necesary. > > Just this will be enough. > > $document->find_any( sub { > $_[1]->isa('PPI::Token::Word') > and > $_[1] eq 'Gtk2::Gdk::DisplayManager' > } ); > > I'm not entirely sure why you have the PPI::Statement phrase there, but > if you've been copying code from Perl::Critic that might be some sort of > an optimisation hack? Hmm, I don't know how it happened anymore, but for some reason my early testing seemed to suggest that find_any doesn't "recurse into" statements. Anyway, thanks to you, Dan, and Chris for the help. Works perfectly. -- Bye, -Torsten |
From: Dan B. <mr....@gm...> - 2006-10-12 09:24:49
|
On 10/11/06, Torsten Schoenfeld <kaf...@gm...> wrote: > Aloha, > > I recently started using PPI to begin work on a module that finds out > which version of Gtk2/gtk+ a given bunch of code requires. To do that, > I need to know if, for example, the bareword "Gtk2::Gdk::DisplayManager" > is used anywhere. Here's what I came up with: > > $document->find_any(sub { > $_[1]->isa ("PPI::Statement") and > $_[1]->find_any(sub { > $_[1]->isa ("PPI::Token::Word") and > $_[1] eq "Gtk2::Gdk::DisplayManager" > }) > }); > > Is this a good way to express the requirements? Is it correct? Is > there a better way? A simpler (if not better) way would be to use PPIx::XPath like so: use PPIx::XPath; my @toks = PPIx::XPath->new($document) ->match('//PPI::Token::Word[.="Gtk2::Gdk::DisplayManager"]'); And that should return you the appropriate nodes. HTH Dan Brook |