parsing perl matching brackets

Discussion in 'Perl Misc' started by julian@ukonline.co.uk, Jun 6, 2006.

  1. Guest

    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";
    }
    }
    , Jun 6, 2006
    #1
    1. Advertising

  2. tuser Guest

    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 ]
    tuser, Jun 6, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. GIMME
    Replies:
    2
    Views:
    863
    GIMME
    Feb 11, 2004
  2. enrique
    Replies:
    3
    Views:
    12,708
    Alan Moore
    Feb 8, 2005
  3. NevilleDNZ

    RegEx for matching brackets

    NevilleDNZ, May 2, 2008, in forum: Python
    Replies:
    5
    Views:
    1,023
    NevilleDNZ
    May 4, 2008
  4. Marc Bissonnette

    Pattern matching : not matching problem

    Marc Bissonnette, Jan 8, 2004, in forum: Perl Misc
    Replies:
    9
    Views:
    220
    Marc Bissonnette
    Jan 13, 2004
  5. Bobby Chamness
    Replies:
    2
    Views:
    212
    Xicheng Jia
    May 3, 2007
Loading...

Share This Page