The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

scriptx-eg-noop - The minimalistic ScriptX-based script that does nothing (demos viewing logs, loading plugins, putting plugin handler at another event, putting multiple handlers)

VERSION

This document describes version 0.000004 of scriptx-eg-noop (from Perl distribution ScriptX), released on 2020-10-01.

SYNOPSIS

% scriptx-eg-noop

DESCRIPTION

This script does nothing. It loads ScriptX but does not specify any plugins, including any plugin that runs code or do interesting stuffs. It then calls the run() static method.

To see the logs being produced to the screen, you can use Log::ger::Screen:

% PERL5OPT=-MLog::ger::Screen TRACE=1 scriptx-eg-noop
[scriptx] -> run_event(name=run)
[scriptx] <- run_event(name=run)

We can see that run() is executed but no handlers are being executed.

We can load additional plugins using the SCRIPTX_IMPORT or SCRIPTX_IMPORT_JSON environment variable, so we do not have to modify the script to add behaviors. Let's load the simplest plugin ScriptX::Noop which just logs a message:

% PERL5OPT=-MLog::ger::Screen TRACE=1 SCRIPTX_IMPORT=-Noop scriptx-eg-noop
[scriptx] Reading env variable SCRIPTX_IMPORT ...
[scriptx] -> run_event(name=activate_plugin)
[scriptx] Running on_success ...
[scriptx] Loading module ScriptX::Noop ...
[scriptx] <- run_event(name=activate_plugin)
[scriptx] -> run_event(name=run)
[scriptx] [event run] [1/1] -> handler Noop ...
Hello from the Noop plugin
[scriptx] [event run] [1/1] <- handler Noop: [200] (success)
[scriptx] <- run_event(name=run)

You see that there are two events being run. The first one is activate_plugin (to add a chance for another plugin to act when a plugin is loaded, for example see ScriptX::DisablePlugin). The second one is run, where the Noop plugin registers a handler for.

Let's load another plugin ScriptX::Debug::DumpStash:

% PERL5OPT=-MLog::ger::Screen TRACE=1 SCRIPTX_IMPORT=-Noop,-Debug::DumpStash scriptx-eg-noop
...
[scriptx] -> run_event({name=>"run"})
[scriptx] [event before_run] [1/1] -> handler Debug::DumpStash ...
{ event => "before_run" }
[scriptx] [event before_run] [1/1] <- handler Debug::DumpStash: [200,"OK"] (success)
[scriptx] [event run] [1/1] -> handler Noop ...
Hello from the Noop plugin
[scriptx] [event run] [1/1] <- handler Noop: [200,"OK"] (success)
[scriptx] <- run_event(name=run)

You see the that Debug::DumpStash plugin registers a handler for before_run event and dumps the content of the stash. In ScriptX, you can put a plugin's handler at arbitrary event and priority. For example, to dump stash in the after_run event instead:

% PERL5OPT=-MLog::ger::Screen TRACE=1 SCRIPTX_IMPORT=-Noop,-Debug::DumpStash@after_run scriptx-eg-noop
[scriptx] -> run_event({name=>"run"})
[scriptx] [event run] [1/1] -> handler Noop ...
Hello from the Noop plugin
[scriptx] [event run] [1/1] <- handler Noop: [200,"OK"] (success)
[scriptx] [event after_run] [1/1] -> handler Debug::DumpStash ...
{ event => "after_run" }
[scriptx] [event after_run] [1/1] <- handler Debug::DumpStash: [200,"OK"] (success)
[scriptx] <- run_event(name=run)

You can also dump stash in several places, since you can add handler(s) of a plugin multiple times:

% PERL5OPT=-MLog::ger::Screen TRACE=1 SCRIPTX_IMPORT="-Noop,-Debug::DumpStash@before_run,-Debug::DumpStash@after_run" scriptx-eg-noop
...
[scriptx] [event before_run] [1/1] -> handler Debug::DumpStash ...
{
  event    => "before_run", # {0}
  handlers => {
                activate_plugin => [],                                    # .{0}
                after_activate_plugin => [],                              # .{1}
                after_run => [["Debug::DumpStash", 99, sub { ... }, 2]],  # .{2}
                before_activate_plugin => [],                             # .{3}
                before_run => [["Debug::DumpStash", 99, sub { ... }, 1]], # .{4}
                run => [["Noop", 90, sub { ... }, 0]],                    # .{5}
              },            # {1}
  plugins  => {
                "Debug::DumpStash" => bless({}, "ScriptX::Debug::DumpStash"), # .{0}
                "Noop" => bless({}, "ScriptX::Noop"),                         # .{1}
              },            # {2}
}
[scriptx] [event before_run] [1/1] <- handler Debug::DumpStash: [200,"OK"] (success)
[scriptx] [event run] [1/1] -> handler Noop ...
[ScriptX::Noop] Hello from the Noop plugin
[scriptx] [event run] [1/1] <- handler Noop: [200,"OK"] (success)
[scriptx] [event after_run] [1/1] -> handler Debug::DumpStash ...
{
  event    => "after_run", # {0}
  handlers => {
                activate_plugin => [],                                    # .{0}
                after_activate_plugin => [],                              # .{1}
                after_run => [["Debug::DumpStash", 99, sub { ... }, 2]],  # .{2}
                before_activate_plugin => [],                             # .{3}
                before_run => [["Debug::DumpStash", 99, sub { ... }, 1]], # .{4}
                run => [["Noop", 90, sub { ... }, 0]],                    # .{5}
              },           # {1}
  plugins  => {
                "Debug::DumpStash" => bless({}, "ScriptX::Debug::DumpStash"), # .{0}
                "Noop" => bless({}, "ScriptX::Noop"),                         # .{1}
              },           # {2}
}
[scriptx] [event after_run] [1/1] <- handler Debug::DumpStash: [200,"OK"] (success)
[scriptx] <- run_event(name=run)

HOMEPAGE

Please visit the project's homepage at https://metacpan.org/release/ScriptX.

SOURCE

Source repository is at https://github.com/perlancar/perl-ScriptX.

BUGS

Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=ScriptX

When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.

AUTHOR

perlancar <perlancar@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2020 by perlancar@cpan.org.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.