#!/usr/bin/perl -w ## ## Programmer: Craig Stuart Sapp ## Creation Date: Fri Jul 23 04:43:31 PDT 1999 ## Last Modified: Fri Jul 23 04:43:36 PDT 1999 ## Filename: /home/httpd/html/perl/examples ## Syntax: Perl 5 ## ## Description: A demonstration of how to get the first word ## in each line. And a demonstration of how to ## pass parameters to a PERL subroutine. ## $file = "This is a test\n"; $file .= "1 is a test\n"; $file .= "2 is a test\n"; $file .= "3 is a test\n"; $file .= "4 is a test\n"; $file .= "5 is a test\n"; print "Original data:\n"; print $file; print "\n\nFirst words from data:\n"; print join(" ", firstwords($file)), "\n"; sub firstwords { my ($data) = @_; return $data =~ /\s*(\S+)[^\n]*/g; }