Code Examples
This section contains a few bits of code for things you might want to do with Plugins.

Write messages to the Plugin Log

1
# Import the LRR logging module
2
use LANraragi::Utils::Logging qw(get_plugin_logger);
3
# Use the logger to output status - they'll be passed to a specialized logfile and written to STDOUT.
4
my $logger = get_plugin_logger();
5
6
$plugin->debug("This message will only show if LRR is in Debug Mode")
7
$plugin->info("You know me the fighting freak Knuckles and we're at Pumpkin Hill");
8
$plugin->warn("You ready?");
9
$plugin->error("Oh no");
Copied!
The logger is a preconfigured Mojo::Log object.

Make requests to a remote WebService

Mojo::UserAgent is a full-featured HTTP client coming with LRR you can use.
1
use Mojo::UserAgent;
2
3
my $ua = $lrr_info->{user_agent};
4
5
#Get HTML from a simple GET request
6
$ua->get("http://example.com")->result->body;
7
8
#Make a POST request and get the JSON result
9
my $rep = $ua->post(
10
"https://jsonplaceholder.typicode.com/" => json => {
11
stage => "Meteor Herd",
12
location => [ [ "space", "colony" ] ],
13
emeralds => 3
14
}
15
)->result;
16
17
#JSON decoded to a perl object
18
my $jsonresponse = $rep->json;
19
20
#JSON decoded to a string
21
my $textrep = $rep->body;
Copied!

Read values in the LRR Database

1
my $redis = LANraragi::Model::Config->get_redis;
2
3
my $value = $redis->get("key");
Copied!
This uses the excellent Perl binding library for Redis.

Extract files from the archive being examined

If you're running 0.5.2 or later:
1
use LANraragi::Utils::Archive qw(is_file_in_archive extract_file_from_archive);
2
3
# Check if info.json is in the archive located at $file and get its precise path
4
my $info_path = is_file_in_archive($file, "info.json");
5
if ($info_path) {
6
7
#Extract info.json
8
my $filepath = extract_file_from_archive($file, $info_path);
9
10
#Do whatever you need with the extracted file
11
open( my $fh, '<:encoding(UTF-8)', $filepath )
12
or return ( error => "Could not open $filepath!" );
13
14
while ( my $row = <$fh> ) {
15
#...
16
}
17
18
#Delete it
19
unlink $filepath;
20
}
Copied!