In principle, this approach works, but I'd strongly recommend replacing the
'eval' action with 'lcall'. Firstly, 'eval' compiles the code before each
execution, while with 'lcall' the code is compiled just once at sec
startup. This makes 'eval' much slower. But more importantly, the code of
'eval' action might not even compile when you are using variables with
unpredictable content inside the code. To illustrate this, what if the
context SAMPLE_CONTEXT contains the following lines in its event store:

line "
line text

Invoking the code with 'eval' action would then result in the following
compilation error:

Evaluating code 'my @list = split(/\n/,"line "
line test");     return grep { $_ ne test } @list;' and setting variable
'%new_list'
Bareword found where operator expected at (eval 6) line 2, near ""line "
line"
(Missing operator before line?)
Error evaluating code 'my @list = split(/\n/,"line "
line test");     return grep { $_ ne test } @list;': Can't find string
terminator '"' anywhere before EOF at (eval 6) line 2.


On the other hand, if you replace with 'eval' with 'lcall', you avoid such
issues:

lcall %new_list %list %remove_item -> ( sub { \
    my @list = split(/\n/,$_[0]); \
    return grep { $_ ne $_[1] } @list; \
} );\

With 'lcall', the Perl code is compiled when the rule is parsed at sec
startup, and it is invoked through a code pointer during all future
executions. Also, the %list and %remove_item parameters are passed to the
function as flat strings, without any interpretation by Perl. Inside the
function, the value of %list is mapped to $_[0] and %remove_item to $_[1].
In short, I would recommend 'lcall' over 'eval' for context event store
processing.

As for 'eval', I would use it only for executing short and simple Perl
statements with predictable content (one good example is loading modules at
SEC startup which only happens once). Also, 'eval' might be good for
compiling short and simple functions, so that they can later be invoked
with 'call'.

Hope this helps,
risto


2015-10-05 12:41 GMT+03:00 Bond Masuda <bond.mas...@jlbond.com>:

> I'm storing a variety of things in a context. I want to be able to
> select a specific item in the event store and delete only that item.
>
> Right now, I'm accomplishing this by:
>
> assign %remove_item sample_item_I_want_to_remove; \
> empty SAMPLE_CONTEXT %list; \
> eval %new_list ( \
>     my @list = split(/\n/,"%list"); \
>     return grep { $_ ne %remove_item } @list; \
> );\
> add SAMPLE_CONTEXT %new_list
>
> Is there a better way than removing the entire list, edit the list, and
> then writing it all back into the context event store?
>
> Thanks,
> Bond
>
>
>
> ------------------------------------------------------------------------------
> _______________________________________________
> Simple-evcorr-users mailing list
> Simple-evcorr-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/simple-evcorr-users
>
------------------------------------------------------------------------------
_______________________________________________
Simple-evcorr-users mailing list
Simple-evcorr-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/simple-evcorr-users

Reply via email to