Skip Menu |

This queue is for tickets about the Dist-Zilla-Plugin-Extras CPAN distribution.

Report information
The Basics
Id: 105208
Status: resolved
Priority: 0/
Queue: Dist-Zilla-Plugin-Extras

People
Owner: Nobody in particular
Requestors: VDB [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



Subject: Plugin will be more useful if it save parameters for later use
Plugin will be more useful if it saves parameters instead of throwing them away. Possible use case is: There are few plugins which process templates (e. g. [GatherDir::Template], [GenerateFile], and may be some others. Template is plain text with perl code injections, usually enclosed into double braces {{ ... }}. Within perl code, $dist is a reference to Dist::Zilla object, so using $dist it is possible to reach any plugin (including this one) and retrieve its parameters, e. g.: In dist.ini: [Extras] Name1 = Value1 Name2 = Value2 In template: some text {{ my ( $extras ) = grep( $_->plugin_name eq 'Extras', $dist->plugins ); $extras->args->{ Name 1 }; }}
Subject: patch.diff
diff --git a/lib/Dist/Zilla/Plugin/Extras.pm b/lib/Dist/Zilla/Plugin/Extras.pm index 2a2927b..bddffda 100644 --- a/lib/Dist/Zilla/Plugin/Extras.pm +++ b/lib/Dist/Zilla/Plugin/Extras.pm @@ -6,6 +6,11 @@ with 'Dist::Zilla::Role::Plugin'; use namespace::autoclean; +has args => ( + is => 'ro', + default => sub { {}; }, +); + sub BUILDARGS { my ($class, @arg) = @_; my %copy = ref $arg[0] ? %{$arg[0]} : @arg; @@ -16,6 +21,7 @@ sub BUILDARGS { return { zilla => $zilla, plugin_name => $name, + args => \%copy, } }
On 2015-06-13 06:30:04, VDB wrote: Show quoted text
> Plugin will be more useful if it saves parameters instead of throwing > them away. Possible use case is: > > There are few plugins which process templates (e. g. > [GatherDir::Template], [GenerateFile], and may be some others. > Template is plain text with perl code injections, usually enclosed > into double braces {{ ... }}. Within perl code, $dist is a reference > to Dist::Zilla object, so using $dist it is possible to reach any > plugin (including this one) and retrieve its parameters, e. g.:
It sounds like you're trying to do something -- what exactly is unknown -- that almost certainly can be accomplished in a simpler and easier way. Why not ask on the mailing list or irc channel for advice?
It looks to me that VDB wants to put some parameters in dist.ini, but not in a more specific plugin (or he does not want to create a dedicated plugin to store the parameters), and thus the Extras plugin is used, because the Extras plugin is like a bag where you can put random parameters in. Template variables is one such example. I'm accepting the patch and have released 0.03. On Sat Jun 13 13:13:42 2015, ETHER wrote: Show quoted text
> On 2015-06-13 06:30:04, VDB wrote:
> > Plugin will be more useful if it saves parameters instead of throwing > > them away. Possible use case is: > > > > There are few plugins which process templates (e. g. > > [GatherDir::Template], [GenerateFile], and may be some others. > > Template is plain text with perl code injections, usually enclosed > > into double braces {{ ... }}. Within perl code, $dist is a reference > > to Dist::Zilla object, so using $dist it is possible to reach any > > plugin (including this one) and retrieve its parameters, e. g.:
> > It sounds like you're trying to do something -- what exactly is > unknown -- that almost certainly can be accomplished in a simpler and > easier way. Why not ask on the mailing list or irc channel for advice?
On 2015-06-13 23:32:25, SHARYANTO wrote: Show quoted text
> It looks to me that VDB wants to put some parameters in dist.ini, but > not in a more specific plugin (or he does not want to create a > dedicated plugin to store the parameters), and thus the Extras plugin > is used, because the Extras plugin is like a bag where you can put > random parameters in. Template variables is one such example.
If one wants to pass parameters to a template, surely they should be passed to the plugin that also has the template? What the OP is asking for is bizarre so I'm trying to understand his usecase.
Use case is something like this: ditst.ini: [Extras/MyParams] param1 = value1 param2 = value2 [GenerateFile/Foo] filename = lib/Foo.pm content = {{ use Tool.pm; Tool::foo( $plugin ); }} content_is_template = 1 [GenerateFile/Bar] filename = lib/Bar.pm content = {{ use Tool.pm; Tool::bar( $plugin ); }} content_is_template = 1 Tool.pm: ... sub foo( $plugin ) { my $myparams = $plugin->zilla->plugin_named( 'MyParams' ); my $param1 = $myparams->args->{ param1 }; my $param2 = $myparams->args->{ param2 }; $plugin->log( "Generating with $param1 and $param2..." ); return "...$param1...$param2..."; }; sub bar( $plugin ) { ... }; Of course, I can pass "value1" and "value2" directly to foo() and bar(), but I would have to duplicate them, which is not good for maintenance. Using Extras allows me to specify parameters once, and use them many times in various places. It seems plugin Run can also utilize Extras parameters.