Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 12 Nov 2001 17:15:38 -0800
From:      "Christopher R. Maden" <crism@maden.org>
To:        Chris Aitken <chris@ideal.net.au>, freebsd-questions@FreeBSD.ORG
Subject:   Re: Perl on FreeBSD
Message-ID:  <5.1.0.14.0.20011112171053.00a63d90@mail.maden.org>
In-Reply-To: <20011113005355.GA36434@mercury.itworks.com.au>
References:  <5.1.0.14.2.20011113111839.03949520@mail.ideal.net.au> <5.1.0.14.2.20011113111839.03949520@mail.ideal.net.au>

next in thread | previous in thread | raw e-mail | index | archive | help

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

At 16:53 12-11-2001, Gavin Cameron wrote:
>On Tue, Nov 13, 2001 at 11:19:47AM +1100, Chris Aitken wrote:
> > What I want to be able to do is take the output of a FreeBSD command, and
> > pipe the output to a perl script so I can use that data....
> >
> > For example. If I ran the following at command line,
> >
> > # echo "Blah Blah Blah" | perlscript.pl
>
>#!/usr/bin/perl -w
>
>while (<>)
>{
>   print;
>}
>
>Transfers standard input to standard output.  Replace print inside the while
>loop to do whatever you want. The line read in is stored in $_ in this
>example.

Unfortunately, this is the obfuscated Perl way, and may not demonstrate 
clearly to Chris what's going on.

First, when you pipe data into another process like that, the process sees 
the data on standard input, or STDIN.

Perl uses the <fh> operator to read one line from a filehandle fh. STDIN is 
one predefined filehandle, so you can use <STDIN> to read one line from 
STDIN.  STDIN is also the default, so Perl hackers like to just say <> 
because it's easier to type.  It is not always easier for someone else to read.

The while loop will keep running until its argument is false.  The <fh> 
operator returns false when there is nothing left to read, so while ( 
<STDIN> ) { ... } will execute the block until all of the input has been 
consumed.

As Gavin notes, the <fh> operator assigns the line read to the $_ built-in 
variable.  You can print that explicitly with print $_;, or you can use 
Gavin's shortcut, which is that print always prints $_ unless told 
otherwise.  So a more verbose version of Gavin's script is

#!/usr/bin/perl -w

use strict; # always use strict

while ( <STDIN> ) # Read one line at a time from input or console.
{
   print $_;  # Print the line that was read to stdout.
}

HTH,
Chris
- -- 
Christopher R. Maden, Principal Consultant, HMM Consulting Int'l, Inc.
DTDs/schemas - conversion - ebooks - publishing - Web - B2B - training
<URL: http://www.hmmci.com/ > <URL: http://crism.maden.org/consulting/ >
PGP Fingerprint: BBA6 4085 DED0 E176 D6D4  5DFC AC52 F825 AFEC 58DA
-----BEGIN PGP SIGNATURE-----
Version: PGP Personal Privacy 6.5.8

iQA/AwUBO/B0OqxS+CWv7FjaEQJMOACg1fxvFBS9ZpnJW30KuBe6er6FbZIAoKgd
FEbT3mH8PfKxj1J8Jo6F4fAE
=52h0
-----END PGP SIGNATURE-----


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?5.1.0.14.0.20011112171053.00a63d90>