#!/usr/bin/perl use strict; #Declare Functions # #Declare Variables # # Variables used in Main Section my $file = ""; # for file name my $dir = ""; # for directory name my $pat = ""; # for search pattern my $count = 0; # for counting number of pattern occurences my @word = []; # array of words my @lines = []; # array of lines my $line = ""; # element in array lines my $word = ""; # element in array word my @new2 = []; # array of second pattern my @new3 = []; # array of third pattern my $new2 = ""; # intial element of array of second pattern my $d = ""; # for evaluating elements in a date array my @date = []; # array of dates my $date = ""; # initialize date element string my $pat2 = ""; # initialize seoncd pateern element string my $date1 = ""; # initialize date alternative format element string my $num = ""; # initialize numeric value element string my $new2 = ""; # initialize new second pattern element string # # Usage: perlprocfile file_name # file_name will be the name of the file to process # { #Opening files and reading data and doing something with it. $file = shift(@ARGV); # Check for existence of file if the NOT (bang operand) is true # that means there was no filename passed if(!$file ) { print "Usage: perlprocfile file_name \n"; print " You forgot to enter a file to process \n"; exit; } open(FILE,$file) || die "Couldn't open file $! \n"; chop(@lines = ); close(FILE); print "Reading File $file ....Wait\n"; print "The contents of the file are:\n"; print "\n"; foreach $line (@lines){ print $line, "\n"; } print "\n"; print "Checking for bear's....\n"; #Searching for a particular pattern or string #Parsing Strings # initialize count to 0 and set pattern to search for as "bear" $count = 0; $pat = "bear"; foreach $line (@lines) { @word = split(/\s/,$line); foreach $word(@word){ if($word eq $pat){ $count++; } } } print "There are ", $count, " `$pat` in the file. \n"; } print "\n"; # How to use standard Unix commands in Perl print "Current Working directory is ", `pwd`, "\n"; $dir = `pwd`; $pat2 = "/"; @new2 = split(/$pat2/,$dir); @new3 = split(/\//,$dir); #print @new2, "\n"; #print @new3, "\n"; print "Splitting path into different directory names ...\n"; foreach $new2 (@new2) { print $new2, "\n"; } print "\n"; # While loop $num = 10; print "Counting number of sub directories to current directory ....\n"; while($num > $#new2) { $num--; } print "Total count = ", $num, "\n"; print "\n"; # Use of subroutines, passing data to and fro, etc. # Call Subroutine sub2 passing no parameters. &sub2; # Variable $date1 is evaluated in Sub2 print "Today is ", $date1,"\n"; print "Splitting date into it's constituents ...\n"; @date = split(/\s/,$date1); foreach $d (@date){ print "d = ", $d,"\n"; } print "\n"; # How to pass variables to subroutines? # Call Subroutine sub3 passing the "date array" as a parameter &sub3(@date); exit; # # End Main Section # # # Subroutine Section ************************************ # Subroutine Sub2 # Returns today's date from built in `date` system function # as an array. # sub sub2{ $date1 = `date`; #return $date1 to the main calling routine $date1; } # ******* End Subroutine sub2 # # Subroutine sub3 # Note input is passed in as an array which is a split string, because of # a slight bug in the algorithm, we need the $filler because a single digit # as in 3 Febuary will be changed to a SPACE character in the filler array. # You can see this in the previous output of this program. # Notice how variable are declared as locally as possible. They are defined # here instead of in the main section. # sub sub3 { my $day_of_week = ""; # initialize day of week element string my $month = ""; # initialize month element string my $filler = ""; # initialize filler element string my $day_number = ""; # initialize day number element string my $time = ""; # initialize time element string my $zone = ""; # initialize zone element string my $year = ""; # initialize year element string # Get input from the default array variable @_ ($day_of_week, $month,$filler,$day_number , $time,$zone,$year) = @_; print "Thank you for being with us on this day number $day_number \n"; print "in month $month on this day $day_of_week at $time $zone"; print " in the year $year \n"; } # End Subroutine sub3 ************************** # # End Subroutine Section ************************** exit;