home
NEWS       BLOGS       FORUMS       NEWSLETTERS       RESEARCH       EVENTS       DIGITAL LIBRARY       CAREERS  
Network Computing Network Computing Powered by InformationWeek Business Technology Network

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:

Try It Out : Fortune Cookie Dispenser

The fortune cookies file for the UNIX fortune program - as well as some 'tagline' generators for e-mail and news articles - consist of paragraphs separated by a percent sign on a line of its own, like this:

We all agree on the necessity of compromise. We just can't agree on

when it's necessary to compromise.

    -- Larry Wall

%

All language designers are arrogant. Goes with the territory...

    -- Larry Wall

%

Oh, get a hold of yourself. Nobody's proposing that we parse English.

    -- Larry Wall

%

Now I'm being shot at from both sides. That means I *must* be right.

    -- Larry Wall

%

Save this as quotes.dat and then write a program to pick a random quote from the file:

#!/usr/bin/perl

# fortune.plx

use warnings;

use strict;

$/ = "\n%\n";

open QUOTES, "quotes.dat" or die $!;

my @file = ;

my $random = rand(@file);

my $fortune = $file[$random];

chomp $fortune;

print $fortune, "\n";

This is what you get (or might get - it is random, after all):

> perl fortune.plx

Now I'm being shot at from both sides. That means I *must* be right.

     -- Larry Wall

>

How It Works

Once we've set our record separator appropriately, most of the work is already done for us. This is how we change it:

$/ = "\n%\n";

Now a 'line' is everything up to a newline character and then a percent sign on its own and then another new line, and when we read the file into an array, it ends up looking something like this:

my @file = (

   "We all agree on the necessity of compromise. We just can't agree on

when it's necessary to compromise.\n -- Larry Wall\n%\n",

   "All language designers are arrogant. Goes with the territory...\n -- Larry Wall\n%\n",

   ...

);

We want a random line from the file. The operator for this is rand:

my $random = rand(@file);

my $fortune = $file[$random];

rand produces a random number between zero and the number given as an argument. What's the argument we give it? As you know, an array in a scalar context gives you the number of elements in the array. rand actually generates a fractional number, but when we look it up in the array, as we've seen in Chapter 3, Perl ignores the fractional part. Actually, it's more likely that in existing code you'll see those two statements combined into one, like this:

my $fortune = $file[rand @file];

 Now we have our fortune, but it still has the record separator on the end, so we need to chomp to remove it:

chomp $fortune ;

Finally, we can print it back out, remembering that we need to put a new line on the end:

print $fortune, "\n";

Reading Paragraphs at a Time

If you set the input record separator, $/, to the empty string, "", Perl reads in a paragraph at a time. Paragraphs must be separated by a completely blank line, with no spaces on it at all. Of course, you can use split or similar to extract individual lines from each paragraph. This program creates a 'paragraph summary' by printing out the first line of each paragraph in a file:



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



InformationWeek Business Technology Network
InformationWeekInformationWeek 500InformationWeek 500 ConferenceInformationWeek AnalyticsInformationWeek CIO
InformationWeek EventsInformationWeek ReportsInformationWeek MagazinebMightyByte and SwitchDark Reading
Digital LibraryIntelligent EnterpriseInternet EvolutionNetwork ComputingNo JitterPlug Into The Cloud
space
Techweb Events Network
InteropVoiceConWeb 2.0 ExpoWeb 2.0 SummitEnterprise 2.0 ConferenceMobile Business ExpoSoftware ConferenceCSI - Computer Security Institute
Black HatGTECEnergy CampMashup CampStartup Camp
space
Light Reading Communications Network
Light ReadingLight Reading EuropeUnstrungLight Reading's Cable Digital NewsConstantinopleInternet EvolutionPyramid Research
Heavy ReadingLight Reading Live!Light Reading InsiderEthernet ExpoOptical ExpoTeleco TVTower Technology Summit
space
Financial Technology Network
Advanced TradingBank Systems & TechnologyInsurance & TechnologyWall Street & TechnologyAccelerating Wall StreetBank Systems & Technology Executive SummitBuyside Trading SummitInsurance & Technology Executive Summit
space
Microsoft Technology Network
MSDN MagazineTechNetThe Architecture Journal
space


App Infrastructure   |   Messaging & Collaboration   |   Network & Systems Mgmt   |   Network Infrastructure   |   Security  |   Storage & Servers   |   Wireless   |   Enterprise Apps
About Us  |  Contact Us  |  Site Map  |  Technology Marketing Solutions  |  Advertising Contacts  |   Briefing Centers
Copyright © 2008  United Business Media LLC  |  Privacy Statement  |  Terms of Service  |  Your California Privacy Rights