Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > parsing perl matching brackets

Reply
Thread Tools

parsing perl matching brackets

 
 
julian@ukonline.co.uk
Guest
Posts: n/a
 
      06-06-2006
I am trying to grab parts perl from another script to print out. I
have a script called source and I want to grab all the actions when a
condition is met.

so using the following as a source script my main script looks for the
command "if ($State eq "start")" and I want to return the block of code
that would be run if that condition was meet in this case

{
foreach my $f (qw (jane))
{
print "hello";
}
}

How can I do this taking account that formatting might change and I
want to handel comented out {} and escaped {}

#source script
my $State = "none";

print $State;

if ($State eq "start")
{
foreach my $f (qw (jane))
{
print "hello";
}
}

if ($State eq "end"){
foreach my $f (qw (fred peter))
{
print "good bye";
}
}

 
Reply With Quote
 
 
 
 
tuser
Guest
Posts: n/a
 
      06-06-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I am trying to grab parts perl from another script to print out. I
> have a script called source and I want to grab all the actions when a
> condition is met.
>
> so using the following as a source script my main script looks for the
> command "if ($State eq "start")" and I want to return the block of code
> that would be run if that condition was meet in this case


[ snipped ]

> How can I do this taking account that formatting might change and I
> want to handel comented out {} and escaped {}


It might be far fetched and I haven't yet tried it myself, but if you
are after the syntactical components of another perl script, you could
try PPI.pm (see http://search.cpan.org/~adamk/PPI-1.115/lib/PPI.pm)

On the other hand, if all you want is to execute parts of a another
perl script (and that "part" of the script may, of course, contain a
condition, i.e. if-statement), then a simple eval suffices:

use strict;
use warnings;

my $part_perl_script = 'if ($State eq "start") { $result = "Ok" }';

chomp(my $State = <>); # read in the state

my $result = '* empty *';
eval $part_perl_script;
print "\$result = '$result'\n";

[ snipped ]

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
RegEx for matching brackets NevilleDNZ Python 5 05-04-2008 01:38 AM
Help with Pattern matching. Matching multiple lines from while reading from a file. Bobby Chamness Perl Misc 2 05-03-2007 06:02 PM
java.util.regexp: matching square brackets enrique Java 3 02-08-2005 06:57 PM
Perl expression for parsing CSV (ignoring parsing commas when in double quotes) GIMME Perl 2 02-11-2004 05:40 PM
Pattern matching : not matching problem Marc Bissonnette Perl Misc 9 01-13-2004 05:52 PM



Advertisments