| 1 | package Full::Script; | ||
| 2 | 0 | ▶ | use Full::Pragmata qw(:v1); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 3 | use parent qw(Full::Pragmata); | ||
| 4 | |||
| 5 | our $VERSION = '1.004'; # VERSION | ||
| 6 | our $AUTHORITY = 'cpan:TEAM'; # AUTHORITY | ||
| 7 | |||
| 8 | use utf8; | ||
| 9 | |||
| 10 | =encoding utf8 | ||
| 11 | |||
| 12 | =head1 NAME | ||
| 13 | |||
| 14 | Full::Script - common preƤmble for Perl scripts | ||
| 15 | |||
| 16 | =head1 SYNOPSIS | ||
| 17 | |||
| 18 | #!/usr/bin/env perl | ||
| 19 | use Full::Script; | ||
| 20 | $log->infof('Starting'); | ||
| 21 | await $loop->delay_future(after => 1); | ||
| 22 | |||
| 23 | =head1 DESCRIPTION | ||
| 24 | |||
| 25 | Loads common modules and applies language syntax and other features | ||
| 26 | as described in L<Full::Pragmata>. | ||
| 27 | |||
| 28 | The intention is to use this as the first line in every script or cron job | ||
| 29 | so that we have consistent configuration and language features. It also | ||
| 30 | allows us to apply new standards across the entire codebase without having | ||
| 31 | to modify the code itself. | ||
| 32 | |||
| 33 | =cut | ||
| 34 | |||
| 35 | use Log::Any::Adapter; | ||
| 36 | use Time::Moment; | ||
| 37 | use Time::Moment::Role::Strptime; | ||
| 38 | use Time::Moment::Role::TimeZone; | ||
| 39 | use Time::Moment; | ||
| 40 | use Role::Tiny; | ||
| 41 | |||
| 42 | # Extend Time::Moment to include ->strptime and some basic timezone support. | ||
| 43 | Role::Tiny->apply_roles_to_package('Time::Moment', qw( | ||
| 44 | Time::Moment::Role::Strptime | ||
| 45 | Time::Moment::Role::TimeZone | ||
| 46 | )); | ||
| 47 | |||
| 48 | sub import ($called_on, $version, %args) { | ||
| 49 | my $pkg = $args{target} // caller(0); | ||
| 50 | |||
| 51 | # Ensure we have a sensible encoding layer - scripts that expect | ||
| 52 | # to work with binary data would need to remove this layer (and if | ||
| 53 | # we do that often, we'll add an option for it). | ||
| 54 | binmode STDOUT, ':encoding(UTF-8)'; | ||
| 55 | binmode STDERR, ':encoding(UTF-8)'; | ||
| 56 | binmode STDIN, ':encoding(UTF-8)'; | ||
| 57 | STDOUT->autoflush(1); | ||
| 58 | |||
| 59 | Log::Any::Adapter->import('Stderr', log_level => $ENV{LOG_LEVEL} // 'info'); | ||
| 60 | |||
| 61 | # Apply pragmata | ||
| 62 | return $called_on->next::method( | ||
| 63 | $version, | ||
| 64 | %args, | ||
| 65 | target => $pkg, | ||
| 66 | ); | ||
| 67 | } | ||
| 68 | |||
| 69 | 1; | ||
| 70 | |||
| 71 | __END__ | ||