Saturday, 15 August 2015

Dynamic variables in perl -


I'm thinking how I can do in Perl, what I usually do in Lisp:

  (defvar * verbose-level * 0) (defun my-function (... and key ((verbose-level * verbose-level *) * verbose-level *) ...). ..)   

This means that my-function is run on the basis of the current state, but I can pass it to a different level which All calls will also affect:

  (defun f1 (and key ((Verbose-Lev * Verbose-level *) * Verbose-level *)) (format t "~ S: ~ S = ~ S ~%" 'f1' verbose-level * verbose-level *) (F2: verbose-level 1) (Format T "~ S: ~ S = ~ S ~%" 'F1' * Verbose-level * * Verbose-level *) (F2: Verbose-level (1+ * verbose-level *)) (format t " ~ S: ~ S = ~ S ~% '' F1 '* Verbose-level * * verbose-level *) (Diffin F2 (& amp;; key ((verbose-level * verbose-level * ) * Verbose-level *)) (format T "~ S: ~ S = ~ S ~%" 'F2' * Verbose-level * Verbose-level *)) [17] & gt; (F1) F1: * VERBOSE-LEVEL * = 0 F2: * VERBOSE-LEVEL * = 1 F1: * VERBOSE-LEVEL * = 0 F2: * VERBOSE-LEVEL * = 1 F1: * VERBOSE-LEVEL * = Zero Zero [18] & gt; (F1: Verbose-level 4) F1: * VERBOSE-LEVEL * = 4 F2: * VERBOSE-LEVEL * = 1 F1: * VERBOSE-LEVEL * = 4 F2: * VERBOSE-LEVEL * = 5 F1: * VERBOSE -LEVEL * = 4   

(Note that variable binding is returned when it comes out - even from unusual - functions).

How can I do this in Pearl?

Example, in misc.pm , I have our $ verbose = 0; . How do I write a work that binds the $ verbose to its argument and can restore its value in return?

The Perl concept of global variables is similar to the special variable in CL.

You can type "shadow" local :

  our $ var = 1; With the value of a global variable ("before") function; {# Block creates a new area local $ var = 2; Ceremony ("inside"); } Funk ("after"); Output:  
  before: 1 inside: 2 after: 1     Pre> 

If you select Local < / Code> value, then the new value is seen in the entire dynamic scope, that is, all the functions are said in. Any way (errors, returns, etc.), after leaving the scope, the old value is restored Tail calls do not extend the dynamic area, but the scope is counted as exit.

Meditation Given that the complete variable name of the global variable is from a different package, you do something like

  local $ other :: package :: var = 3; other :: package :: fancy ( "Away from a package, away");   

This is usually used to provide configuration for packages with a functional (non-OO) interface. And.

No comments:

Post a Comment