Perl
  Home arrow Perl arrow Page 9 - Perl 101 (Part 4) - Mind Games
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PERL

Perl 101 (Part 4) - Mind Games
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 11
    2000-06-29


    Table of Contents:
  • Perl 101 (Part 4) - Mind Games
  • Handle With Care
  • Different Strokes
  • A Little Brainwashing
  • Die! Die! Die!
  • Testing Times
  • Popguns And Pushpins
  • Shifting Things Around
  • The Real World
  • Miscellaneous Stuff

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Perl 101 (Part 4) - Mind Games - The Real World
    ( Page 9 of 10 )

    Now, while all this is well and good, you're probably wondering just how useful all this is. Well, we've put together a little case study for you, based on our real-world experiences, which should help to put everything you've learned today in context [do note that for purposes of illustration, we've over-simplified some aspects of the case study, and completely omitted others].

    As a content production house, we have a substantial content catalog on a variety of subjects, much of it stored in ASCII text files for easy retrieval. Some time ago, we decided to use sections of this content on a customer's Web site, and so had to come up with a method of reading our data files, and generating HTML output from them. Since Perl is particularly suited to text processing, we decided to use it to turn our raw data into the output the customer needed.

    Here's a sample raw data file, which contains a movie review and related information - let's call it "sample.dat":
    The Insider
    1999
    Al Pacino and Russell Crowe
    Michael Mann
    178.00
    5
    This is the body of the review.
    
    It could consist of multiple paragraphs.
    
    We're not including the entire review here...you've probably already seen
    the movie. If you haven't, you should!

    Within the data block, there are two main components - the header, which contains information about the title, the cast and director, and the length; and the review itself. We know that the first six lines of the file are restricted to header information, and anything following those six lines can be considered a part of the review.

    And here's the script we wrote to extract data from this file, HTML-ize it and display it:
    #!/usr/bin/perl
    
    # open the data file
    open(DATA, "sample.dat") || die ("Unable to open file!\n");
    
    # read it into an array
    @data = <DATA>;
    
    # clean it up - remove the line breaks
    foreach $line (@data)
    	{
    	chomp $line;
    	}
    
    # extract the header - the first six lines
    @header = splice(@data, 0, 6);
    
    # join the remaining data with HTML line breaks
    $review = join("<BR>", @data);
    
    # print output
    print "<HTML>\n<HEAD>\n</HEAD>\n<BODY>\n";
    
    print "\"$header[0]\" stars $header[2] and was directed by $header[3].\n";
    
    print "<P>\n";
    
    print "Here's what we thought:\n<P>\n";
    print $review;
    
    print "Length: $header[4] minutes\n<BR>\n";
    print "Our rating: $header[5]\n<BR>\n";
    
    print "</BODY>\n</HTML>\n";

    And here's the output:
    <HTML>
    <HEAD>
    </HEAD>
    <BODY>
    "The Insider" stars Al Pacino and Russell Crowe and was directed by Michael
    Mann.
    <P>
    Here's what we thought:
    <P>
    This is the body of the review.<BR><BR>It could consist of multiple
    paragraphs.<BR><BR>We're not including the entire review here...you've
    probably already seen the movie. If you haven't, you should!<BR>
    Length: 178.00 minutes
    <BR>
    Our rating: 5
    <BR>
    </BODY>
    </HTML>

    As you can see, Perl makes it easy to extract data from a file, massage it into the format you want, and use it to generate another file.

    This article copyright Melonfire 2000. All rights reserved.

     
     
    >>> More Perl Articles          >>> More By Vikram Vaswani and Harish Kamath, (c) Melonfire
     

       

    PERL ARTICLES

    - More Perl Bits
    - Perl, Bit by Bit
    - Basic Charting with Perl
    - Using Getopt::Long: More Command Line Option...
    - Command Line Options in Perl: Using Getopt::...
    - Web Access with LWP
    - More Templating Tools for Perl
    - Site Layout with Perl Templating Tools
    - Build a Perl RSS Aggregator with Templating ...
    - Looping, Security, and Templating Tools
    - Perl: Bon Voyage Lists and Hashes
    - Templating Tools
    - Perl: Number Crunching
    - Perl Debuggers in Detail
    - Debugging Perl





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT