Upgrade to Pro — share decks privately, control downloads, hide ads and more …

redirect and pipe in perl5

redirect and pipe in perl5

kichijojipm 6, 2016.01.15

Shoichi Kaji

January 15, 2016
Tweet

More Decks by Shoichi Kaji

Other Decks in Technology

Transcript

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

    App::RemoteCommand • App::FatPacker::Simple
  2. ໨࣍ • cmd1 > file.txt • cmd1 | cmd2 •

    ࣮ફྫ • further reading
  3. > # 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͢Δ
  4. >& # me 2>&1 open STDERR, ">&", \*STDOUT; # me

    > file.txt 2>&1 open STDOUT, ">", "file.txt" or die $!; open STDERR, ">&", \*STDOUT; ͋ΔϑΝΠϧϋϯυϧʹϦμΠϨΫτ͢Δʹ͸
 طଘϑΝΠϧϋϯυϧΛ“&”ͱͱ΋ʹopen͢Δ
  5. 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
  6. | # 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; }
  7. |- # 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;
  8. 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
  9. 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;
  10. 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;
  11. further reading • ྫղUnixϓϩάϥϛϯάڭࣨ,
 ෌Ӭ࿨ਓ, ݖ౻ࠀ඙, 2007 • perldoc -f

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

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