Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Extending Perl::PIE - how to have Perl snippets executed ?

by Anonymous Monk
on Jul 08, 2009 at 16:19 UTC ( [id://778325]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I'm extending Perl::Pie package to include more deterministic rules that can be triggered in some certain time moments (new seconds, every 5 secs, etc...). Rules consists of conditions that must be true, so actions in the list get executed.

I've add some simple time related functions (new_second, new_hour, ...) and variables ($Week_day, $Day, $Hour) in the additional time.pl file. I've included it in main file with

require 'time.pl';

and declared all functions in it as main::function. I can use them easily in main program, but have problems if I try to use those functions as parts of condition or actions of rules...

I try to use them as "eval $str" from some Perl::PIE package, where $str is written in xml rules config file, but it seems that it is not working at all. I think I need to run them in "context of the main" program but this is obviously not happening... I've debugged code and it doesn't get into function at all. I must be missing some basic things about dealing with global variables and functions...

I have this in xml config :
<rule name="print status every 5 secs"> <condition attribute='main::new_second(5)' type="time" trigger="1"> +1</condition> <action attribute="\n****************print status every 5 secs: New +_Second: Weekday:$Wday Time:$Hour:$Minute:$Second Time_Now:$Time_Now +Date:$Mday/$Month/$Year" type="print">1</action> </rule>


and when I try to eval('main::new_second(5)') nothing happens, if I do the same from main program it works...

I'd kindly as for some help or some pointers to similar solutions ...

Thanks in advance,

regards,

Rob.

Replies are listed 'Best First'.
Re: Extending Perl::PIE - how to have Perl snippets executed ?
by spazm (Monk) on Jul 08, 2009 at 21:14 UTC
    what is Perl::PIE? Can you provide a link, I can't find any details.

    Please also include the code you've written or tried thus far.

    thanks

      Hi,

      I guess it would be too much to include code. You can find Perl::Pie here :
      http://www.pre-emptive.net/doco/pie-perl-inference-engine

      The problem is that I do eval from Perl::PIE package extensions and it doesn't work, while it works in main program when used directly without eval...

      Small Example:
      main program:
      ... use Pie::KnowledgeBase; use Pie::Engine; use Pie::KnowledgeBase::Dumper; use Data::Dumper; require 'time_utilities.pl'; # For misc. functions (e.g. time/da +te stamp routines) use Time::HiRes('gettimeofday'); use vars qw($Time $Second $Minute $Hour $Mday $Wday $Day $Month $Year) +; ...


      then I have somewhere in Perl::PIE packages snippet:
      ... print " ----------------- " . $ref->{attribute} . "\n" +; ...


      where $ref->{attribute} includes string to print that is defined in xml config :
      <rule name="print status every 5 secs"> <condition attribute='main::new_second(5)' type="time" trigger="1"> +1</condition> <action attribute="\n****************print status every 5 secs: New +_Second: Weekday:$Wday Time:$Hour:$Minute:$Second Time_Now:$Time_Now +Date:$Mday/$Month/$Year" type="print">1</action> </rule>

      and I don't get variables substituted in printed text by their value, so I get exactly literally the same string back :
      "\n****************print status every 5 secs: New_Second: Weekday:$Wday Time:$Hour:$Minute:$Second Time_Now:$Time_Now Date:$Mday/$Month/$Year"

      What should I do to print real values of variables and not literally their names ?

      Thanks in advance,

      Rob.

        Rob,

        Thanks for the update. I don't have time to look at it currently, but a perl rules engine sounds interesting.

        It looks like you need something to interpolate your string. So, something like changing your print routine to include an eval of the string?

        I don't think the code below works, but may prove helpful? You may also find yourself doing a search on the returned string to fill in values... blech.

        $string =~ s/\$(\w+)/ ${$1} /xeg;
        orig:
        print " ----------------- " . $ref->{attribute} . "\n"
        new:
        my $string = $ref->{attribute}; my $output = eval $string; if($@){ die "problem with eval: $@"; } print " ----------------- $output\n";

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://778325]
Approved by ELISHEVA
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-19 17:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found