in reply to Re: Searching and Replacing
in thread Searching and Replacing

A minor but (IMO) important quibble: <STDIN> and <> are not identical constructs. If you use the script without command-line arguments, they are, but if a file is specified in @ARGV, reading from the magic filehandle (<>) will read from that file, while reading from STDIN (<STDIN>) will read from the standard input, aka the terminal.

Example code:

#!/usr/bin/perl use strict; my $color; print "What is your favorite color? "; chomp ($color = <STDIN>); print <>; print "Just to check, your favorite color is $color, right? "; $_ = <STDIN>; if ( /^y/i ) { print "Phew!\n" } else { print "I don't believe you!\n"}

If run thus

perl myscript.pl random_file.txt
this should give us

What is your favorite color? Blue
[ contents of random_file.txt ]
Just to check, your favorite color is Blue, right?Yes!
Phew!

This is occasionally very useful behavior--I wouldn't be at all surprised if it was pretty much what you need, Satira.

And of course, in real quibble land, tachyon didn't mean an infinite loop, he meant a loop until the filehandle reaches EOF. :-) If you're looping on <>, this will take care of itself unless you do something foolish; if you're looping on STDIN, you can break out by giving the locally appropriate EOF character (control-D on most Unices and MacPerl, control-Z on Windows I think...).



If God had meant us to fly, he would *never* have give us the railroads.
    --Michael Flanders

Replies are listed 'Best First'.
Re: Re: Re: Searching and Replacing
by tachyon (Chancellor) on Jul 04, 2001 at 15:24 UTC

    Thanks for that ChemBoy - I was unaware of the <> and @ARGV behaviour. File another pearl. Sure it's not technically an infinite loop as you point out but having a way to break out of it is a good idea. Much better than control ?D ?Z ?X ?C or even the ever faithful Ctl-Alt-Del ;-)

    print "Your call is important to us please hold...\n"; for (;;) { while (1) { print "I committed a technical inexactitude\n" until 0; } } print "Did you wait long!\n";

    tachyon

    s&&rsenoyhcatreve&&&s&n\w+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print