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:

Creating Filters

As well as the three standard filehandles, Perl provides a special filehandle called ARGV. This reads the names of files from the command line and opens them all, or if there is nothing specified on the command line, it reads from standard input. Actually, the @ARGV array holds any text after the program's name on the command line, and takes each file in turn. This is often used to create filters, which read in data from one or more files, process it, and produce output on STDOUT.

Because it is used so commonly, Perl provides an abbreviation for : an empty diamond, or <>. We can make our line counter a little more flexible by using this filehandle:

#!/usr/bin/perl

# nl2.plx

use warnings;

use strict;

my $lineno = 1;

while (<>) {

   print $lineno++;

   print ": $_";

}

Now Perl expects us to give the name of the file on the command line:

> perl nl2.plx nlexample.txt

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 #######

>

We can actually place a fair number of files on the command line, and they'll all be processed together. For example:

> perl nl2.plx nlexample.txt nl2.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 #######

6: #!/usr/bin/perl

7: # nl2.plx

8: use warnings;

9: use strict;

10:

11: my $lineno = 1;

12:

13: while (<>) {

14:     print $lineno++;

15:     print ": $_";

16: }

If we need to find out the name of the file we're currently reading, it's stored in the special variable $ARGV. We can use this to reset the counter when the file changes.

Try it out : Numbering Lines in Multiple Files

By detecting when $ARGV changes, we can reset the counter and display the name of the new file:

#!/usr/bin/perl

# nl3.plx

use warnings;

use strict;

my $lineno;

my $current = "";

while (<>) {

   if ($current ne $ARGV) {

      $current = $ARGV;

      print "\n\t\tFile: $ARGV\n\n";

      $lineno=1;

}

   print $lineno++;

   print ": $_";

}

And now we can run this on our example file and itself:

> perl nl3.plx nlexample.txt nl3.plx

File: nlexample.txt

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 #######

File: nl3.plx

1: #!/usr/bin/perl

2: # nl3.plx

3: use warnings;

4: use strict;

5:

6: my $lineno;

7: my $current = "";

8:

9: while (<>) {

10:      if ($current ne $ARGV) {

11:             $current = $ARGV;

12:             print "\n\t\tFile: $ARGV\n\n";

13:             $lineno=1;

14:     }

15:

16:     print $lineno++;

17:     print ": $_";

18: }

>



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. Purchase Today: $299
 
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