redirect and pipe in perl5

redirect and pipe in perl5

kichijojipm 6, 2016.01.15

E4967f90a08dadff9cc185b863cd99a7?s=128

Shoichi Kaji

January 15, 2016
Tweet

Transcript

  1. ϦμΠϨΫτͱ ύΠϓΛ perl5Ͱ΍Δ Shoichi Kaji

  2. me • Shoichi Kaji • cpan module • cpm •

    App::RemoteCommand • App::FatPacker::Simple
  3. cmd > file.txt cmd1 | cmd2

  4. ϦμΠϨΫτͱύΠϓΛperl5Ͱ • ͳͥʁ • perl5͸Α͘better shellͱݺ͹ΕΔɻ
 ΑͬͯshellͰͰ͖Δ͜ͱΛperl5Ͱ΋Ͱ͖ΔΑ͏ʹ͠ ͓͖͍ͯͨɻ • perl5ͷεϨου͸࢖͑ͳ͍ɻฒྻԽ͍ͨ͠ͱ͖͸

    forkʹΑͬͯෳ਺ϓϩηεΛ্ཱͪ͛Δͷ͕ී௨ɻ
 ͦͷͱ͖IPCͷҰखஈͱͯ͠pipeΛ஌͓͖͍ͬͯͨɻ
  5. ໨࣍ • cmd1 > file.txt • cmd1 | cmd2 •

    ࣮ફྫ • further reading
  6. > # me > file.txt open STDOUT, ">", "file.txt" or

    die $!; # me 2> file.txt open STDERR, ">", "file.txt" or die $!; # me < file.txt open STDIN, "<", "file.txt" or die $!; # me >> file.txt open STDOUT, ">>", "file.txt" or die $!; ϑΝΠϧʹϦμΠϨΫτ͢Δʹ͸
 طଘϑΝΠϧϋϯυϧΛ΋͏Ұ౓open͢Δ
  7. >& # me 2>&1 open STDERR, ">&", \*STDOUT; # me

    > file.txt 2>&1 open STDOUT, ">", "file.txt" or die $!; open STDERR, ">&", \*STDOUT; ͋ΔϑΝΠϧϋϯυϧʹϦμΠϨΫτ͢Δʹ͸
 طଘϑΝΠϧϋϯυϧΛ“&”ͱͱ΋ʹopen͢Δ
  8. ex open STDOUT, ">", "file.txt" or die $!; open STDERR,

    ">&", \*STDOUT; say "hello world"; # print to file.txt warn "oops\n"; # print to file.txt too
  9. pipe pipe my $read, my $write; $read $write process

  10. pipe $read $write ࢠprocess $read $write ਌process fork

  11. | # ls -al | me pipe my $read, my

    $write; my $pid = fork // die $!; if ($pid == 0) { # ls -al close $read; open STDOUT, ">&", $write; exec "ls", "-al"; exit 255; } else { # me close $write; my @got = <$read>; warn "got: $_" for @got; waitpid $pid, 0; close $read; } # me | wc -l pipe my $read, my $write; my $pid = fork // die $!; if ($pid == 0) { # wc -l close $write; open STDIN, "<&", $read; exec "wc", "-l"; exit 255; } else { # me close $read; say {$write} "line1"; say {$write} "line2"; close $write; waitpid $pid, 0; }
  12. None
  13. |- # me | wc -l open my $fh, "|-",

    "wc", "-l" or die $!; say {$fh} "line1"; say {$fh} "line2"; close $fh; # automatically call wait() # ls -al | me open my $fh, "-|", "ls", "-al" or die $!; my @got = <$fh>; close $fh; # automatically call wait() warn "got: $_" for @got;
  14. None
  15. ࣮ફྫ

  16. system, qx// • system, qx//͸shellΛىಈ͢ΔՄೳੑ͕͋Δɻ • ͳΔ΂͘ආ͚Δ΂͖͕ͩɺshellͷه๏͕ϥΫͰ Α͔ͭͬͯ͘͠·͏ɻͦ͜Λ… $ perl

    -e 'system "sleep infinity >/dev/null"' & $ ps f PID TTY STAT TIME COMMAND 11016 pts/3 Ss 0:00 -zsh 11289 pts/3 SN 0:00 \_ perl -e system "sleep infinity >/dev/null" 11333 pts/3 SN 0:00 | \_ sh -c sleep infinity >/dev/null 11334 pts/3 SN 0:00 | \_ sleep infinity
  17. system, qx// # ͜͏Ͱ͸ͳ͘... system "git clone git://github.com/git/git.git >/dev/null 2>&1";

    # ͜͏ my $pid = fork // die $!; if ($pid == 0) { open STDOUT, ">", "/dev/null"; open STDERR, ">&", \*STDOUT; exec "git", "clone", "git://github.com/git/git.git"; } waitpid $pid, 0;
  18. pipe with select use IO::Select; use POSIX 'strftime'; STDOUT->autoflush(1); my

    @cmd = ("perl", "-E", '$|++; for (1..10) { say "$$: $_"; sleep int(rand 5); }’); # @cmdͷग़ྗΛ͖ͨ΋ͷ͔Β࣌ࠁΛઌ಄ʹ͚ͭͯग़ྗ open my $fh0, "-|", @cmd or die $!; open my $fh1, "-|", @cmd or die $!; my $select = IO::Select->new($fh0, $fh1); while (1) { last if $select->count == 0; my @ready = $select->can_read(1); for my $fh (@ready) { my $line = <$fh>; defined $line or do { $select->remove($fh); next }; my $time = strftime("%F %T", localtime); print "$time: $line"; } } close $_ for $fh0, $fh1;
  19. further reading • ྫղUnixϓϩάϥϛϯάڭࣨ,
 ෌Ӭ࿨ਓ, ݖ౻ࠀ඙, 2007 • perldoc -f

    open • Unix Programming with Perl 2, kazuho oku, http://www.slideshare.net/kazuho/unix- programming-with-perl-2
  20. further reading • App::cpm (me) • Proclet (kazeburo) • Server::Starter

    (kazuho) • System::Command (book) • Daemon::Control (symkat)