Perl - ファイル読み込み キーボード入力 端末 TTY

特に高度なことはやっていない作業効率化用のperlのサンプルです。
perlはJAVAとかと同じくC系の構文ですけど、lastだったりelsifだったり、配列とハッシュの記号がわからなくなったり、忘れるんですよね。

みなさんも仕事を半分の時間で終わらせて、残りの時間は呆けながら休みの余暇について考えて下さいね!

サンプルは随時更新:2017/07/27

◆ファイル読み込みと出力
perlと言えば正規表現を使ったテキスト編集が主に使う理由ですよね。下記サンプルは引数チェックとファイル読み込みのみです。

#!/usr/bin/perl
use POSIX;
use feature "switch";

#引数・オプション格納変数
$Hogehage_File = "";
$Opt_A = "";

#内部変数
$tmpstr = "";
@Array_A = ();
%Hash_A = ();

sub check_argument
{
    my(@argv) = @_;

    while( $#argv >= 0 ) {
        if( $argv[0] =~ /^-a/ ) {
            $Opt_A = $argv[1];
            shift(@argv);
        } else {
            last;
        }
        shift(@argv);
    }

    $Hogehage_File = $argv[0];
    if( ! -f $Hogehage_File ) {
        print(STDERR "File:$Hogehage_file is not a file or not found.\n");
        &print_usage;
        exit(1);
    }

    return(0);
}

sub print_usage
{
    print(STDERR "Usage: aiueo.pl [-a AAA] ほげはげファイル\n");

    return(0);
}

### MAIN
&check_argument(@ARGV);

printf("Opt_A:%s\n", $Opt_A);
printf("Hogehage_File:%s\n", $Hogehage_File);

open(FILE, "$Hogehage_File");
while(<FILE>) {
    if( /^$/ ) {
        next;
    }
    chomp();

    @Array_A = split(/\t/);

    printf("[%s][%s][%s]\n", $Array_A[0], $Array_A[1], $Array_A[2]);
}
close(FILE);

exit(0);

◆キーボードからの文字読み込み・端末(TTY)関連
perlでやるような事とは言えないですが、キーボードからの入力に対してあれこれするサンプルです。おそらくUnix系OSならどんなものでも動くと思いますが、端末サイズを取得する部分はうまく動くかどうかわかりません。
ちなみにこのサンプルはどんなものかというと、ハゲ『彡⌒ミ』をNethack(Rogue)風(まぁ、viですね^^;)に動かします。(仕事中に何作ってんだ。あ、もちろんやる事やった余りの時間と待機時間でですよ。作業効率化にもなりますし。)

#!/usr/bin/perl

use Term::Cap
require 'sys/ioctl.ph';
use feature "switch";

$MYC = "彡⌒ミ";
$MYW = 6;
$MYX = 0;
$MYY = 0;
$screen_lines = 0;
$screen_cols = 0;
$trash = "";
$key = "";

my $termios = new POSIX::Termios;
$termios->getattr;
my $ospeed = $termios->getospeed;

$terminal = Tgetent Term::Cap { TERM => undef, OSPEED => $ospeed};
$terminal->Trequire(qw/ce ku kd/);

die "no TIOCGWINSZ" unless defined &TIOCGWINSZ;
open(TTY, "+</dev/tty") or die "no tty: $!";
unless( ioctl(TTY, &TIOCGWINSZ, $winsize='') ) {
    die sprintf "$0: ioctl TIOCGWINSZ (%08x: $!)\n", &TIOCGWINSZ;
}
($screen_lines, $screen_cols, $trash, $trash) = unpack('S4', $winsize);
system "stty -echo cbreak </dev/tty >/dev/tty 2>&1";

print $terminal->Tputs('cl');
print $terminal->Tputs('vi');
print $terminal->Tgoto('cm', $MYX, $MYY);
print $terminal->Tputs('sc');
print $terminal->Tpad($MYC, 1);
print $terminal->Tputs('cl');

while( $key ne "q" ) {
    $key = getc(TTY);
    given( $key ) {
        when( /^h$/ )
        {
            if( $MYX > 0 ) {
                $MYX--;
            }
        }
        when( /^j$/ )
        {
            if( $MYY < $screen_lines - 1 ) {
                $MYY++;
            }
        }
        when( /^k$/ )
        {
            if( $MYY > 0 ) {
                $MYY--;
            }
        }
        when( /^l$/ )
        {
            if( $MYX + $MYW - 1 < $screen_cols - 1 ) {
                $MYX++;
            }
        }
        default
        {
            continue;
        }
    }

    print $terminal->Tputs('im');
    for( $i = 1 ; $i <= $MYW ; $i++ ) {
        print $terminal->Tputs('dc');
        print $terminal->Tpad(" ", 1);
    }
    print $terminal->Tputs('ei');

    print $terminal->Tgoto('cm', $MYX, $MYY);
    print $terminal->Tputs('sc');
    print $terminal->Tpad($MYC, 1);
    print $terminal->Tputs('cl');
}
print "\n";

print $terminal->Tputs('ve');

system "stty echo -cbreak </dev/tty >/dev/tty 2>&1";
close(TTY);

exit(0);