Welcome to WebProNews Breaking eBusiness and Search News
Advertise | Newsletter | Sitemap | News Feeds News Feed 
 WebProNews Search Part of the iEntry network iEntry inc. 

Perl Input

A.P. Lawrence
Expert Author
Published: 2004-01-28

WebProNews RSS Feed


Perl has wonderful I/O capabilities. I'm only going to cover input here: reading from files or standard input. There are two ways to do that (actually a lot more than two, but this is supposed to be introductory material): you can open a specific file, or you can pass files on the command line and either open them individually or just ignore the whole thing and pretend everything is coming from STDIN.

Let's just take a look at some simple examples. Create files a, b and c with text you will recognize, and try this:



Run that and "a" will be displayed. That was easy enough. Now the command line way:



If you called that t.pl, run ./t.pl a b c and you'll see each of your files go by. Notice that I didn't bother to close F. There are cases where being sloppy like that could get you in trouble, but in this simple situation, Perl happily closes the previous file for you when it sees you re-using F.

Now the magic angle bracket way:



Try that both by itself (type lines to it), and as ./t.pl a b c, and finally as cat a b c | ./t.pl. You'll see that whatever you do, it reads the lines or the files. When you give it file names, they act like they came from STDIN. If you don't, it reads STDIN. If you want to read BOTH the files and STDIN, do ./t.pl a b c -.

If you want, you can do things when each file ends:



And of course you can accumulate all the lines instead:



Now we're going to get much trickier. We're not going to read unless there is something ready to read. This can be useful if you need to do other things while waiting for input, or if you don't want lack of input to hold you up forever. The code will use Perl's "select" command, which just passes to the system level select() call. Perl's "select" is one of those overloaded functions that does very different things depending on how many arguments you give it, so don't get confused here. This is the usage that tells us whether or not filehandles are ready for io. We also use the "fileno" command to get the actual file number of STDIN. The "vec" is used to turn "$rin" into a bit structure that is what the system level select() is looking for; if STDIN is file number 0 as it would be if not redirected, then the first bit of $rin get sets to 1 before we call the Perl select (you don't have to understand that to use it). The structure gets changed by select(), so we protect it with the "$rout=$rin"; $rout will get changed, not $rin. It all sounds very complicated, but it really isn't bad in usage:



(I'm making this code artificially simple. This works, but isn't the way you would probably do it in reality.)

Try that first with just ./t.pl. You'll see it print 0 every two seconds. If you type something, it only gets read when you press enter (buffered input). Use CTRL-C or your INTR character to quit this.

0
0
0
hello
1
0 hello

0
0


Now try it like this: ./t.pl a. My "a" contains "This is A":

0
0
0
hello
1
1 This is A

1
0 hello

0


Nothing happened until we gave it a line on STDIN. Then, and only then, did Perl rearrange things so that the file "a" came through STDIN before our line. That makes sense, because until you try to read STDIN, Perl has no idea what your intentions toward the files on the command line are. You might want to process them specifically as we did in the second little script.

Single character input is a little trickier, because you first need to convince your OS that it should be feeding you that way. Exactly how you do that depends on the heritage of your system; it's going to be a "stty" command of some sort. This works on my Mac OS X box:



The angle brackets have one more trick up their sleeves:



That opens every file that begins with "a", "b' or "c". Use any wild card characters you need just like you would at the command line.

This should give you some feel for the flexibility of Perl input.

Receive Our Daily Email of Breaking eBusiness News


About the Author:
A.P. Lawrence provides SCO Unix and Linux consulting services http://www.pcunix.com

WebProNews RSS Feed

More Articles

Contact WebProNews
Advertisement





TOP NEWS

Targeted Information for Business
WebProNews is part of the iEntry network

Internet Business: Marketing: Small Business:
WebProNews MarketingNewz SmallBusinessNewz
WebProWorld AdvertisingDay PromoteNews
EcommNewz SalesNewz EntrepreneurNewz

Software: Search Engines: Web Design:
WebMasterFree Jayde B2B DesignNewz
NetworkingFiles SearchZA FlashNewz
SecurityConfig SearchNewz WebSiteNotes

Developer: IT Management: Security:
DevWebPro ITManagement SecurityProNews
DevNewz SysAdminNews SecurityConfig
TheDevWeb NetworkingFiles NetworkNewz

The iEntry Network consists of over 100 web publications reaching millions of Internet Professionals. Contact us to advertise.
eBUSINESS RESOURCES






 Advertise | Contact Us | Corporate | Newsletter | Sitemap | Submit an Article | News Feeds
 WebProNews is an iEntry, Inc. ® publication - $line) { echo $line ; } ?> All Rights Reserved
About WebProNews
WebProNews is the number one source for eBusiness News. Over 5 million eBusiness professionals read WebProNews and other iEntry business and tech publications.

WebProNews provides real-time coverage of internet business.

Free Email Newsletters:
WebProNews SearchNewz
WebProWorld DevWebPro
Marketing SecurityNews
Plus over 100 other newsletters!

Send me relevant info on products and services.


WebProWorld
Ten most recent posts.

NetworkingFiles
Featured Software

WebProNews in the News
View all recent mentions of WebProNews from around the world!

Recent Articles On ...
Google eBusiness
Yahoo Ask Jeeves
MSN Blogs
Search Engines Blogging
Affiliate Programs Marketing
eCommerce Advertising
eBay Sun Microsystems
AOL Adsense
Microsoft Adwords
Oracle IBM
Amazon Apple
SEM Mac
SEO iPod
Adsense XBox
PR Adobe



iEntry.com WebProWorld RSS Feed WebProWorld Contact WebProNews Print Version Email a friend Bookmark us