Lexical::Persistence(3)User Contributed Perl DocumentatioLnexical::Persistence(3) NNAAMMEE Lexical::Persistence − Persistent lexical variable values for arbitrary calls. SSYYNNOOPPSSIISS #!/usr/bin/perl use Lexical::Persistence; my $persistence = Lexical::Persistence−>new(); foreach my $number (qw(one two three four five)) { $persistence−>call(\&target, number => $number); } exit; sub target { my $arg_number; # Parameter. my $narf_x++; # Persistent. my $_i++; # Dynamic. my $j++; # Persistent. print "arg_number($arg_number) narf_x($narf_x) _i($_i) j($j)\n"; } DDEESSCCRRIIPPTTIIOONN Lexical::Persistence objects encapsulate persistent data. Lexical variables in the functions they call are used to access this persistent data. The usual constructor, _n_e_w_(_), creates new persistence objects. The persistence object’s _c_a_l_l_(_) method is used to call functions within their persistent contexts. By default, lexicals without a leading underscore are persistent while ones with the underscore are not. _p_a_r_s_e___v_a_r_i_a_b_l_e_(_) may be overridden to change this behavior. A single Lexical::Persistence object can encapsulate multiple persis‐ tent contexts. Each context is configured with a _s_e_t___c_o_n_t_e_x_t_(_) call. By default, _p_a_r_s_e___v_a_r_i_a_b_l_e_(_) determines the context to use by examining the characters leading up to the first underscore in a variable’s name. The _g_e_t___m_e_m_b_e_r___r_e_f_(_) returns a reference to the persistent value for a given lexical variable. The lexical will be aliased to the referenced value returned by this method. By default, _p_u_s_h___a_r_g___c_o_n_t_e_x_t_(_) translates named function parameters into values within the "arg" context. The parameters are then avail‐ able as $arg_name lexicals within _c_a_l_l_(_)’s target function. _p_o_p___a_r_g___c_o_n_t_e_x_t_(_) is used to restore a previous argument context after a target function returns. A helper method, _w_r_a_p_(_), returns a coderef that, when called normally, does _c_a_l_l_(_) magic internally. By default, lexicals without prefixes persist in a catch‐all context named "_". The underscore is used because it’s _p_a_r_s_e___v_a_r_i_a_b_l_e_(_)’s con‐ text/member separator. The _i_n_i_t_i_a_l_i_z_e___c_o_n_t_e_x_t_s_(_) member is called dur‐ ing _n_e_w_(_) to create initial contexts such as "_". The _g_e_t___c_o_n_t_e_x_t_(_) accessor can be used to fetch a named context hash for more detailed manipulation of its values. nneeww Create a new lexical persistence object. iinniittiiaalliizzee__ccoonntteexxttss Set whatever standard contexts belong to this class. By default, a catch‐all conext is set. sseett__ccoonntteexxtt NNAAMMEE,, HHAASSHH Store a context HASH within the persistence object, keyed on a NAME. Contexts stored in the object will be shared among all the functions called through this object. _p_a_r_s_e___v_a_r_i_a_b_l_e_(_) will choose which lexicals are persistent and the names of their contexts. ggeett__ccoonntteexxtt NNAAMMEE Returns a context hash associated with a particular context name. Autovivifies the context if it doesn’t already exist. ccaallll CCOODDEERREEFF,, PPAARRAAMMEETTEERR__LLIISSTT Call CODEREF with lexical persistence. The PARAMETER_LIST is passed to the callee in the usual Perl way. It may also be stored in an "argument context", as determined by _p_u_s_h___a_r_g___c_o_n_t_e_x_t_(_). Lexical variables within the callee will be restored from the current context. _p_a_r_s_e___v_a_r_i_a_b_l_e_(_) determines which variables are aliased and which contexts they belong to. wwrraapp CCOODDEERREEFF Wrap a function or anonymous CODEREF such that it’s transparently called via _c_a_l_l_(_). ppaarrssee__vvaarriiaabbllee VVAARRIIAABBLLEE__NNAAMMEE Determines whether a VARIABLE_NAME is persistent. If it is, return the variable’s sigil ("$", "@" or "%"), the context name where its persis‐ tent value lives, and the member within that context where the value is stored. On the other hand, it returns nothing if VARIABLE_NAME is not persis‐ tent. ggeett__mmeemmbbeerr__rreeff SSIIGGIILL,, CCOONNTTEEXXTT__NNAAMMEE,, MMEEMMBBEERR__NNAAMMEE Returns a reference to a persistent value. The SIGIL defines the mem‐ ber’s type. The CONTEXT_NAME and MEMBER_NAME describe where to find the persistent value. Scalar values are stored internally as scalars to be consistent with how most people store scalars. The persistent value is created if it doesn’t exist. The initial value is undef or empty, depending on its type. ppuusshh__aarrgg__ccoonntteexxtt PPAARRAAMMEETTEERR__LLIISSTT Convert a PARAMETER_LIST into members of an argument context. By default, the context is named "arg", so $arg_foo will contain the value of the "foo" parameter. ppoopp__aarrgg__ccoonntteexxtt OOLLDD__AARRGG__CCOONNTTEEXXTT Restore the OLD_ARG_CONTEXT after a target function is called. The OLD_ARG_CONTEXT was returned by _p_u_s_h___a_r_g___c_o_n_t_e_x_t_(_). BBUUGGSS Read them at http://rt.cpan.org/Public/Dist/Display.html?Name=lexi‐ cal−persistence Report them at http://rt.cpan.org/Public/Bug/Report.html?Queue=lexi‐ cal−persistence SSEEEE AALLSSOO POE::Stage, Devel::LexAlias, PadWalker, Catalyst::Controller::BindLex. LLIICCEENNSSEE Lexical::Persistence in copyright 2006 by Rocco Caputo. All rights reserved. Lexical::Persistence is free software. It is released under the same terms as Perl itself. AACCKKNNOOWWLLEEDDGGEEMMEENNTTSS Thanks to Matt Trout and Yuval Kogman for lots of inspiration. They were the devil and the other devil sitting on my shoulders. Nick Perez convinced me to make this a class rather than persist with the original, functional design. irc://irc.perl.org/poe for support and feedback. perl v5.8.6 2006‐11‐14 Lexical::Persistence(3)