John Verber
John Verber

Reputation: 755

best way to break out of <STDIN>

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;
}

Upvotes: 1

Views: 3577

Answers (1)

Mark
Mark

Reputation: 1098

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.

Upvotes: 3

Related Questions