1

I'm trying to break out of stdin on a flag of some sort but when I run my code I still have to use control d to break out. Could someone please gimme a pointer here thanks:

while(my $line = <STDIN>){
chomp $line;
last if $line == 0;
push @stdin, $line;
}
4

1 回答 1

3

UPDATED (twice)

I'd go with using a string compare of one type or another. == is a numeric compare and forces both sides of the comparison to numeric. Almost any string other than a pure number winds up being equal to 0.

while (my $line = <STDIN>) {
   chomp $line;

   # use the string comparison operator...
   last if $line eq "0";
   # or use a match operator...
   # last if $line =~ m/^0$/;
   # or match on some special number
   # last if $line == 3.1415926;

   push @stdin, $line;
}

I tested this in windows:

perl -e "while(my $line = <STDIN>){chomp $line;last if $line =~ m'^[.]$'}END{print @l}"

and unix:

perl -e 'while(my $line = <STDIN>){chomp $line;last if $line=~m/^[.]$/;push@l,$line}END{print@l,$/}'

OLD ANSWER

Ctrl + Z in Windows? The flag that is already built-in. On Unix you'd use Ctrl + D.

于 2012-08-08T18:30:44.930 回答