parsing perl matching brackets

J

julian

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";
}
}
 
T

tuser

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 ]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top