#!/usr/bin/perl # # 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); 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" as a parameter &sub3(@date); exit; # Subroutine Section ************************************ # Subroutine Sub2 sub sub2{ $date1 = `date`; #return $date1 to the main calling routine $date1; } # Subroutine sub3 sub sub3 { # Get input from the default array variable @_ chop; ($week,$month,$day,$time,$zone,$year) = @_; print "Thank you for being with us on this day, $week, $month $day th at $time$zone in the year $year \n"; } # End Subroutine Section ************************** exit;