Re: Different way of getting Standard input



On Dec 31, 2007 2:45 AM, Prabu Ayyappan <prabu.ayyappan@xxxxxxxxx> wrote:
Hi All,

What is the difference in Accepting the following form of standard input?

1) $a = <STDIN>;
2) $b = <stdin>;
3) $c = <>;

Now check,
print $a $b $c;


What is actually happening?

The first two are the same thing (but you should use STDIN for
consistency's sake). The third is similar, but does some magic things
as well. If there are entries in @ARGV (the variable that holds what
was passed on the command line) it will treat those entries as file
names and attempt to open and read from them (setting $ARGV to the
current file being read). If there is nothing in @ARGV, it will use
the STDIN. This mimics the normal filter-style-program from UNIX
(e.g. grep, wc, sort, etc). For instance, this is a primitive version
of grep:

#!/usr/bin/perl

use strict;
use warnings;

die "$0 needs a pattern to match with" unless @ARGV;

#remove the pattern from @ARGV
my $pattern_string = shift;

#compile the pattern
#WARNING: this is bad, we should check that
#$pattern_string does not use any of the
#forms that allow execution of code
#before compiling it
my $pattern = qr/$pattern_string/;

#if there is more than one file to grep
#then print the file name and the line
my $print_filename = @ARGV > 1;

#loop over each line from STDIN or the files in @ARGV
#print the lines that match the pattern
while (<>) {
if (/$pattern/) {
print "$ARGV:" if $print_filename;
print;
}
}

and here is hot it is used:

cowens@amans:~$ perl grep.pl perl grep.pl
#!/usr/bin/perl
cowens@amans:~$ cat grep.pl | perl grep.pl print
#then print the file name and the line
my $print_filename = @ARGV > 1;
#print the lines that match the pattern
print "$ARGV:" if $print_filename;
print;
cowens@amans:~$ perl grep.pl perl grep.pl bench.pl
grep.pl:#!/usr/bin/perl
bench.pl:#!/usr/local/ActivePerl-5.10/bin/perl

Compare that output with the output of the real grep in similar situations:

cowens@amans:~$ grep perl grep.pl
#!/usr/bin/perl
cowens@amans:~$ cat grep.pl | grep print
#then print the file name and the line
my $print_filename = @ARGV > 1;
#print the lines that match the pattern
print "$ARGV:" if $print_filename;
print;
cowens@amans:~$ grep perl grep.pl bench.pl
grep.pl:#!/usr/bin/perl
bench.pl:#!/usr/local/ActivePerl-5.10/bin/perl

snip
Will this be written to some standard input file?
snip

STDIN is the "standard input file handle", it is attached to fileno 0
by default.

snip
If so In windows where this will be written?
snip

If you are asking where the print is written (since it is the only
write you show), it is written to STDOUT file (fileno 1 by default).
.



Relevant Pages

  • Re: system calls (mv + grep) within Perl script
    ... > if you have the array, using grep is cleaner. ... The above program does the same thing, except it assigns the pattern ... neither the built-in Perl grep nor the ... I don't exactly see what you are suggesting. ...
    (comp.lang.perl.misc)
  • RE: How to reinvent grep with perl?
    ... Perl works better than grep because the grep statement you give below does ... not find any instances of the pattern and perl finds quite a few. ... How to reinvent grep with perl? ...
    (perl.beginners)
  • Using perl to parse specific lines by date...
    ... While I've already done this with a simple shell script using grep, ... was trying to figure out how I can do the same thing in perl. ... `date +%d/%b` instead of 26/Dec to the pattern matching if statement. ...
    (perl.beginners)
  • How to reinvent grep with perl?
    ... to make grep search for a certain pattern. ... How can I make perl print the file file name? ...
    (perl.beginners)
  • Re: Net::DNS
    ... function because you still use $rr within the foreach, ... > So I can find the all these object methods in the perl docs listed below? ... > the return value of 'grep'. ... > perldoc perlboot ...
    (perl.beginners)