Hello and welcome to the Webmasters Forums!. This is the best place to get webmasters resources for free. Get $2 for free today, read more - Make your payment today. Download premium and professional templates for free. Get free web hosting without ads, read more. You can get lot more by simply join with this forum. To gain full access to the forums you must sign up for a free account.


Post Reply  Post Thread 

Taste of Perl

Post Bank
Posting Manager
******

Posts: 995
Group: Forum Team
Joined: Sep 2006
Status: Online
Make money from now. You can make money just for posting on this forum. Every discussions on this community gives you more money. $2 minimum payout. So get your payment today, SignIn with this forum.

Signin to Remove this Post

bomber
Junior Member
*


Posts: 34
Group: Registered
Joined: Sep 2006
Status: Offline
Reputation: 0
Points: 250 (Donate)
Post: #1

Smile Taste of Perl


Quite useful Perl programs can be short. Suppose we want to change the same text in many files. Instead of editing each possible file or constructing some cryptic find, awk, or sed commands, you could issue a single command:

Example: Amazing Perl One-Liner That Substitutes Text In Multiple Files

perl -e 's/gopher/World Wide Web/gi' -p -i.bak *.html

This command, issued at the Unix prompt, executes the short Perl program specified in single quotes. This program consists of one perl operation; it substitutes for original word "gopher" the phrase "World Wide Web", (globally, ignoring case). The re mainder of the Unix command indicates that the perl program should run for each file ending in ".html" in the current directory. If any file "blah.html" needs changing, a backup of the original is made as file "blah.html.bak". Programming Perl lists additional handy one-liners.

For those accustomed to "classic" procedural programming, the "amazing one-liner" above can be expanded in Perl in a style more like C or Pascal:

Example: Global Substitution, The Scenic Route

Code:
#!/usr/local/bin/perl -w
    # File:  go2www    
    # This Perl program in classic programming style changes
    # the string "gopher" to "World Wide Web" in all files
    # specified on the command line.
    # 19950926 gkj
    $original='gopher';
    $replacement="World Wide Web";
    $nchanges = 0;
    # The input record separator is defined by Perl global
    # variable $/.  It can be anything, including multiple
    # characters.  Normally it is "\n", newline.  Here, we
    # say there is no record separator, so the whole file
    # is read as one long record, newlines included.
    undef $/;

    # Suppose this program was invoked with the command
    #     go2www ax.html  big.basket.html  candle.html
    # Then builtin list @ARGV would contain three elments
    # ('ax.html', 'big.basket.html', 'candle.html')
    # These could be accessed as $ARGV[0] $ARGV[1] $ARGV[2]

    foreach $file (@ARGV) {
        if (! open(INPUT,"<$file") ) {
            print STDERR "Can't open input file $bakfile\n";
            next;
        }

        # Read input file as one long record.
        $data=<INPUT>;
        close INPUT;

        if ($data =~ s/$original/$replacement/gi) {
            $bakfile = "$file.bak";
            # Abort if can't backup original or output.
            if (! rename($file,$bakfile)) {
                die "Can't rename $file $!";
            }
            if (! open(OUTPUT,">$file") ) {
                die "Can't open output file $file\n";
            }
            print OUTPUT $data;
            close OUTPUT;
            print STDERR "$file changed\n";
            $nchanges++;
        }

        else {  print STDERR "$file not changed\n"; }
    }
    print STDERR "$nchanges files changed.\n";
    exit(0);


Questions:

1. What do you guess that the "!" means, as in:

if (! open(OUTPUT,">$file") ) {
die "Can't open output file $file\n";
}

2. What does the ">" probably mean here? Compare with "open(INPUT ...)".
3. What does "die" do?
4. Some languages use "IF ... THEN DO ... END; ELSE IF ... THEN DO ... END". How is this notated in Perl?
5. What does $nchanges++ do?

The Perl Creed is, "There is more than one way!" This noble freedom of expression however results in the first of the four Perl Paradoxes: Perl programs are easy to write but not always easy to read. For example, the following lines are equivalent!

if ($x == 0) {$y = 10;} else {$y = 20;}
$y = $x==0 ? 10 : 20;
$y = 20; $y = 10 if $x==0;
unless ($x == 0) {$y=20} else {$y=10}
if ($x) {$y=20} else {$y=10}
$y = (10,20)[$x != 0];

19-09-2006 12:11 AM
Find all posts by this user Quote this message in a reply
Post Reply  Post Thread 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Simplest Perl CGI geekyone 1 221 15-04-2008 04:31 PM
Last Post: ivenms
  MySQL with Perl maddog39 4 618 09-08-2007 07:04 AM
Last Post: walsh
Smile Testing Perl Programs bomber 0 613 19-09-2006 12:39 AM
Last Post: bomber
Smile Storing and Running Perl Programs bomber 0 673 19-09-2006 12:13 AM
Last Post: bomber
Smile State of Perl bomber 0 673 19-09-2006 12:09 AM
Last Post: bomber
Smile What Is Perl? bomber 0 633 19-09-2006 12:07 AM
Last Post: bomber

View a Printable Version
Send this Thread to a Friend
Subscribe to this Thread | Add Thread to Favorites
Rate This Thread:

Forum Jump:

Sign In to Remove Ads

Download 1000's of web templates. Unlimited access!
World's Best Web Hosting
Website of the Month

Create-a-Page for Free
SOTM June 2008


Accepting Submissions
for July 2008
Resources

Recommended Sites:



Visit our Sponsors!

Current time: 30-08-2008, 07:05 AM


Copyright © 2002-2008 MyBB Group
Powered By MyBB