Thanks Henrik,
useful and detailed info as usual.
In this case I've to manage a puppet4 version of a function, params_lookup 
which is widely used in all my old modules, so it's impossible to change 
the params passed to it without changing all those modules.
I fear I'll need to make a system function, even if not recommended.
Do you mind to give a look to it, once I complete it, in order to avoid 
possible bad side effects?

Thanks as usual
al

On Monday, July 20, 2015 at 4:10:20 PM UTC+2, henrik lindberg wrote:
>
> On 2015-19-07 18:14, Alessandro Franceschi wrote: 
> > This is a silly question that will take to guys like Henrik less than a 
> > nanosecond to reply, I suppose, but I'm struggling hard with this and 
> > haven't found a way out. 
> > 
> > I need to get the value of the compiler variable module_name from within 
> > a Puppet 4 function. 
> > I've tried various approach, the one most resembling the approach in 
> > Puppet 3 function is: 
> > mod =  closure_scope['module_name'] 
> > but it doesn't work. 
> > 
> > Any hint? 
> > Your nanosecond is worth at least a sunday afternoon of mine. 
> > 
>
> The 'closure scope' is the scope the function is defined in. 
> You can also get access to 'calling scope' by creating a 'system 
> function' instead of a regular function. 
>
> A 'system function' is special as it has more power, but it also comes 
> with more responsibility. 
>
> Ideally functions should not ever need anything but the arguments given 
> to them. It is good practice to write such pure functions. (i.e. 
> consider requiring that $module_name is given to the function). 
>
> A 'system function' is written like this: 
>
> Puppet::Functions.create_function(:inline_epp, 
> Puppet::Functions::InternalFunction) do 
>
>    dispatch :inline_epp do 
>      scope_param() 
>      param 'String', :template 
>      optional_param 'Hash[Pattern[/^\w+$/], Any]', :parameters 
>    end 
>
>    def inline_epp(scope, template, parameters = nil) 
>      Puppet::Pops::Evaluator::EppEvaluator.inline_epp(scope, template, 
> parameters) 
>    end 
> end 
>
> If you cannot spot it immediately, there are two things you need to do 
> in your function to make it a 'system function': 
> * Use create_function(:name, Puppet::Functions::InternalFunction) 
> instead of just create_functon(:name). 
> * Make the dispatcher inject the calling scope by calling 
> 'scope_param()' in the dispatchers definition of parameters. 
>
> Then, use the calling scope to get the calling scope's variables. 
>
> The reason it is bad to directly get variables from a "calling scope" is 
> that it makes it hard to write general functions. What if you in a 
> module want to provide a function that wraps calls to a function that 
> picks up $module_name, or $calling_module from scope? 
>
> Thus, use 'InternalFunction' and 'scope_param()' sparingly, if at all. 
>
> - henrik 
>
> -- 
>
> Visit my Blog "Puppet on the Edge" 
> http://puppet-on-the-edge.blogspot.se/ 
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/puppet-dev/2534c29c-72d7-4bbe-84c8-debe3b59f1c8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to