Both sides previous revision Previous revision Next revision | Previous revision Next revisionBoth sides next revision |
user_guide:howto:scripting [2019/03/13 16:26] – [Advanced techniques] gawrilow | user_guide:howto:scripting [2019/03/28 10:53] – put $ into '' '' benmuell |
---|
? ''local ref $s=sub { ... }'' | ? ''local ref $s=sub { ... }'' |
: Temporarily replace the implementation of a subroutine passed by reference | : Temporarily replace the implementation of a subroutine passed by reference |
? ''local bless $obj;'' \\ ''local bless $obj, "Package";%%'' | ? ''local bless $obj;'' \\ ''%%local bless $obj, "Package";%%'' |
: Temporarily change the type of an object passed by reference | : Temporarily change the type of an object passed by reference |
-- Some basic array modifications can be made temporarily, they are automatically undone when the localization scope is left. Arrays can reside in package or local variables or be passed by reference: | -- Some basic array modifications can be made temporarily, they are automatically undone when the localization scope is left. Arrays can reside in package or local variables or be passed by reference: |
? ''local with ($scope) { local $x=1; }'' | ? ''local with ($scope) { local $x=1; }'' |
:: Changes made within the block will be reverted when the ''$scope'' scalar is destroyed; this can happen at any later point in time, far away from the block or subroutine enclosing this place. The same scalar can be used repeatedly in the same or different ''with'' blocks; all reverting actions will be accumulated and applied in reverse order. It must have an undefined value before first use. | :: Changes made within the block will be reverted when the ''$scope'' scalar is destroyed; this can happen at any later point in time, far away from the block or subroutine enclosing this place. The same scalar can be used repeatedly in the same or different ''with'' blocks; all reverting actions will be accumulated and applied in reverse order. It must have an undefined value before first use. |
.. There is one session-scope guard ''$Scope->locals'' used, among others, by [[:general#preferences|prefer_now]] commands and for removing temporary properties from big objects. If you want to introduce temporary changes lasting until the next interactive shell input or the completion of a script started in [[#calling|batch mode]], use ''local with($Scope->locals) { ... }'' If you want to introduce a nested localization scope with shorter lifetime within your script, create a temporary Scope object: ''local $Scope=new Scope();'' | .. There is one session-scope guard ''%%$Scope->locals%%'' used, among others, by [[:general#preferences|prefer_now]] commands and for removing temporary properties from big objects. |
| .. If you want to introduce temporary changes lasting until the next interactive shell input or the completion of a script started in [[#calling|batch mode]], \\ use ''%%local with($Scope->locals) { ... }%%'' |
| .. If you want to introduce a nested localization scope with shorter lifetime within your script, create a temporary Scope object: ''local $Scope=new Scope();'' |
| |
===== Most important interfaces ===== | ===== Most important interfaces ===== |
:: like above, but adding (or replacing) properties | :: like above, but adding (or replacing) properties |
? ''%%$p->copy_permuted(VERTICES => $v)%%'' | ? ''%%$p->copy_permuted(VERTICES => $v)%%'' |
:: first finds a permutation mapping the listed properties of the source object $p to the given values (here: ''%%$p->VERTICES%%'' => ''$v''), then applies it to all source properties and stores the results in the new object. Invariant properties are copied verbatim. If no suitable permutation can be found, an exception is raised. | :: first finds a permutation mapping the listed properties of the source object ''$p'' to the given values (here: ''%%$p->VERTICES%%'' => ''$v''), then applies it to all source properties and stores the results in the new object. Invariant properties are copied verbatim. If no suitable permutation can be found, an exception is raised. |
| |
=== Name and Description === | === Name and Description === |