home news blogs forums events research newsletter whitepapers careers


Network Computing Network Computing Network Computing
HOT PICKS

IMMERSE YOURSELF:

SOA

  |

Data Center

  |

802.11n

  |

Data Privacy

  |
APO  |

Virtualization

  |

NAC

  |

Security

  |

Network Mgmt

  |

Enterprise Apps

  |

Storage & Servers





Beginning PERL

July 24, 2000

Brought to you by:



This manuscript is an abridged version of a chapter from the Wrox Press book Beginning PERL.

Our expert, Simon Cozens, will enlighten you with our latest network design manual chapter, "Beginning Perl." See how to handle files, directories, data and more.


Table of contents:

Reading Lines

Now that we can open a file, we can then move on to reading the file one line at a time. We do this by replacing the STDIN filehandle in with our new filehandle, to get . Just as reads a single line from the keyboard, reads one line from a filehandle. This <...> construction is called the diamond operator, or readline operator:

Try It Out : Numbering Lines

We'll use the construct in conjunction with a while loop to go through each line in a file. So then, to print a file with line numbers added, you can say something like this:

#!/usr/bin/perl

# nl.plx

use warnings;

use strict;

open FILE, "nlexample.txt" or die $!;

my $lineno = 1;

while () {

   print $lineno++;

   print ": $_";

}

Now, create the file nlexample.txt with the following contents:

One day you're going to have to face

   A deep dark truthful mirror,

And it's gonna tell you things that I still

   Love you too much to say.

####### Elvis Costello, Spike, 1988 #######

This is what you should see when you run the program:

> perl nl.plx

1: One day you're going to have to face

2:      A deep dark truthful mirror,

3: And it's gonna tell you things that I still

4:      Love you too much to say.

5. ####### Elvis Costello, Spike, 1988 #######

>

How It Works

We begin by opening our file and making sure it was opened correctly:

open FILE, "nlexample.txt" or die $!;

Since we're expecting our line numbers to start at one, we'll initialize our counter:

my $lineno = 1;

Now we read each line from the file in turn, which we do with a little magic:

while () {

This syntax is actually equivalent to:

while (defined ($_ = )) {

That is, we read a line from a file and assign it to $_, and we see whether it is defined. If it is, we do whatever's in the loop. If not, we are probably at the end of the file so we need to come out of the loop. This gives us a nice, easy way of setting $_ to each line in turn.

As we have a new line, we print out its line number and advance the counter:

   print $lineno++;

Finally, we print out the line in question:

   print ": $_";

There's no need to add a newline since we didn't bother chomping the incoming line. Of course, using a statement modifier, we can make this even more concise:

open FILE, "nlexample.txt" or die $!;

my $lineno = 1;

print $lineno++, ": $_" while

But since we're going to want to expand the capabilities of our program -adding more operations to the body of the loop - we're probably better off with the original format.



PAGE: 1 I 2 I 3 I 4 I 5 I 6 I 7 I NEXT PAGE
 





Ready to take that job and shove it?

Function:

Keyword(s):

State:
SPONSOR
RECENT JOB POSTINGS
CAREER NEWS
Go beyond Google and get vertical. These specialized search sites will help you find the business information you need -- fast.

Ari Balogh was named to the post of chief technology officer as the companys for a "realignment" of employees.










InformationWeek U.S. IT Salary Survey 2008
Salaries for business technology professionals are falling. Here's what you need to know in order to make good hiring decisions and personal career choices. Download Today
 
ROLLING RIGHT ALONG
Follow key Network Computing Reviews from conception to completion. This Week: Holistic APM.



Network Computing Reports Emerging Enterprise Podcast Series: Secrets to Success








TechSearch


Microsite of the Week


Powerful Information at Your Fingertips



techweb
Online Communities TechWebInformationWeekLight ReadingIntelligent EnterprisebMightyNetwork ComputingDark ReadingDigital LibraryWall Street & Technology
Byte & SwitchNo JitterInternet EvolutionLight Reading's Cable Digital NewsContentinopleUnStrungBank Systems & TechnologyAdvanced TradingInsurance & Technology
Face-to-Face Events
InteropWeb 2.0 ExpoWeb 2.0 SummitVoiceConBlack HatCSISoftwareEntrprise 2.0 ConferenceGTEC
Mobile Business Expo
InformationWeek 500 ConferenceBuy Side Trading XchangeBuy Side Trading SummitBank Executive SummitInsurance Executive SummitTelcoTVEthernet ExpoOptical Expo
Magazines  
InformationWeekWall Street & TechnologyInsurance & TechnologyBank Systems & TechnologyAdvanced TradingMSDNTechNetSmart EnterpriseThe Architecture JournalDatabase Magazine
 
Research & Analyst Services  
Heavy ReadingInformationWeek ReportsInformationWeek Analytics
 
   
   
App Infrastructure   |   Messaging & Collaboration   |   Network & Systems Mgmt   |   Network Infrastructure   |   Security  |   Storage & Servers   |   Wireless   |   Enterprise Apps
About Us  |  Contact Us  |  Site Map  |  Technology Marketing Solutions  |   Briefing Centers
Copyright © 2008  United Business Media LLC  |  Privacy Statement  |  Terms of Service  |  Your California Privacy Rights