Last active
December 14, 2015 07:38
-
-
Save tauty/90f3039847ccb1f104de to your computer and use it in GitHub Desktop.
Perl to Java sample code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# perl part start | |
# | |
use strict; | |
use warnings; | |
use IPC::Open2; | |
my $cmd = "java InteractiveSample"; | |
# コマンドを入出力モードで起動する | |
my $pid = open2(*READ, *WRITE, $cmd); | |
my @number = (1 .. 100); | |
foreach ( @number ) { | |
# コマンドの標準入力にデータを送る | |
print WRITE "$_\n"; | |
# コマンドの標準出力からデータを読み出す | |
my $s = <READ>; | |
print "$_ -> $s"; | |
} | |
close(WRITE); | |
close(READ); | |
# | |
# perl part end | |
# | |
/** | |
* Java part start | |
*/ | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
public class InteractiveSample { | |
public static void main(String[] args) throws IOException { | |
int sum = 0; | |
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); | |
for (String line; null != (line = reader.readLine()); ) { | |
sum += Integer.parseInt(line); | |
System.out.println(sum); | |
} | |
} | |
} | |
/** | |
* Java part end | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment