Skip to content

Instantly share code, notes, and snippets.

@tzccinct
Last active January 10, 2021 23:47
Show Gist options
  • Save tzccinct/80a0b9fe68d1ec17c29b3a3ab4eebedc to your computer and use it in GitHub Desktop.
Save tzccinct/80a0b9fe68d1ec17c29b3a3ab4eebedc to your computer and use it in GitHub Desktop.
Perl スクリプト テンプレート (Perl script template)
#!/usr/bin/env perl
use v5.14;
use warnings;
use utf8;
use open ':std', IN => ':encoding(UTF-8)', OUT => ':utf8';
use Getopt::Long qw(HelpMessage VersionMessage :config posix_default no_ignore_case bundling);
use Pod::Usage qw(pod2usage);
use Data::Dumper;
our $VERSION = '0.01';
local $Data::Dumper::Indent = 1;
local $Data::Dumper::Sortkeys = 1;
local $Data::Dumper::Terse = 1;
my %opt = (
debug => 0,
verbose => 0,
);
GetOptions(
\%opt, qw/
debug|d+
verbose|v+
/,
'help|h' => \&HelpMessage,
'version|V' => \&VersionMessage,
) or pod2usage(-verbose => 1);
main() unless caller;
sub main {
say "日本語";
say DumperStr({'キー' => '値'}) if $opt{debug};
}
sub DumperStr {
my $str = Dumper \@_;
$str =~ s/\\x\{(\w{4})\}/pack('U', hex($1))/eg;
return $str;
}
__END__
=encoding utf8
=head1 NAME
_template.pl
=head1 SYNOPSIS
$ _template.pl
=head1 DESCRIPTION
=head1 OPTIONS
=over
=item B<-d, --debug>
=item B<-h, --help>
=item B<-v, --verbose>
=item B<-V, --version>
=back
=head1 AUTHOR
=cut

備考

use v5.14;
use warnings;

use v5.12以上で暗黙のuse strict を含む。

use open ':std', IN => ':encoding(UTF-8)', OUT => ':utf8';

入力に:utf8 を指定するのはセキュリティ上問題がある。

use Getopt::Long qw(HelpMessage VersionMessage :config posix_default no_ignore_case bundling);

-? の代わりに-h を使いたい、-v を--version ではなく--verbose に使いたいため、auto_help, auto_version は使わず。

参考

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment