Important alert: (current site time 7/15/2013 9:27:50 PM EDT)
 

VB icon

case_tags.pl

Email
Submitted on: 2/14/2002 1:54:42 PM
By: Incipient Cipher  
Level: Beginner
User Rating: Unrated
Compatibility: 5.0 (all versions)
Views: 7572
(About the author)
 
     Convert HTML tags to lowercase or uppercase. Fully commented. Shows usage of subroutines and passing/returning values. You can use wildcard characters in the filename. If the path/filename has a space in it try using a '?' character in the space.
 
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
=**************************************
= for :case_tags.pl
=**************************************
Do what you like. Just remember where you got it.
Use at your own risk.
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
=**************************************
= Name: case_tags.pl
= Description:Convert HTML tags to lowercase or uppercase. Fully commented. Shows usage of subroutines and passing/returning values. You can use wildcard characters in the filename. If the path/filename has a space in it try using a '?' character in the space.
= By: Incipient Cipher
=
=This code is copyrighted and has= limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=282&lngWId=6=for details.=**************************************

#!/usr/bin/perl
	##########################################################################
	# FILENAME : case_tags.pl
	# WRITTEN BY : Casey Gilmer (icphr || Incipient Cipher)
	# DESCRIPTION: Open files listed as argument on commandline for reading.
	# You can also use wildcard '* or ?' characters to do 
	# multiple files.
	# Does not work with filenames or paths that have spaces in 
	# them but you could try using a '?' character in the 
	# spaces if possible.
	# This is mostly for an example of how to use and pass 
	# arguments to subroutines.
	##########################################################################
	#
	
	# Execute the 'main' subroutine.
	###############################
	#
	&main;
	
	
	# This is where it all starts. {8-)
	###################################
	#
	sub main{
	 	# Find case switch and assign to '$case'
		my $case = &findSwitch;
		
		# Loop thru each argument
		foreach $i (1..$#ARGV){
			my $htmlFile = $ARGV[$i]; # Assign next argument to variable.
			print "$htmlFile\n";	 # Print filename being worked on.
			
			# Call subroutine to open file for read
			my @fileContents = &readFile($htmlFile);
			
			# Call subroutine to open file for write
			&writeFile($htmlFile, $case, @fileContents);
		}
		
		# Let user know its finished.
		print "\n\tFile Conversion Completed\n\n";
	}
		
	
	# Open file for read.
	###########################
	#
	sub readFile{
		my ($file) = @_; # Place parameter value in '$file'.
		open HTM_FILE, $file or die "Cannot open $file for read: $!\n";
		
		my @contents = <HTM_FILE>; # Throw contents of file into the array
		close HTM_FILE; # Close the file-handle so we can open
																 # it for writing.
	 return @contents;
	}
	
	
	
	# Open file for write.
	#######################
	#
	sub writeFile{
		my ($file, $c, @contents) = @_; # Assign parameter values to variables.
		
		open HTM_FILE, ">$file" or die "Cannot open $file for write: $!\n";
		
		# Read each element of the array and assign it to '$str'
		my $str;# make '$str' visible for this sub only.
		foreach $str (@contents){ 
			if($c eq 'lower'){
				$str =~ s/<(.*?)>/<\L$1>/g; # Search for tags and change to lower-case.
			}elsif($c eq 'upper'){
				$str =~ s/<(.*?)>/<\U$1>/g;
			}
	
			print HTM_FILE $str; # Write string to file
		}
		
		# Close file-handle
		close HTM_FILE; 
	}
	
	
	# Find case switch
	####################
	#
	sub findSwitch{
		my $switch;# Variable are visible only to this subroutine.
		my $e;
		
		$e = $ARGV[0]; # First argument should be the switch.
		if($e =~ /^-l/ig){ # Check for switch that was entered.
			delete $ARGV[0]; # Remove the switch from the array so as not
			return $switch = 'lower'; # to confuse it for a file.
		}elsif($e =~ /^-u/ig){
			delete $ARGV[0];
			return $switch = 'upper'; # Assign switch-type to variable and return
		}elsif($e =~ /^-h/ig){ # the value.
			&usage;
		}
	
		
		# No switch given. Display usage
		if($switch eq ""){
			 &usage;
		}
	}
	
	
	# How to use case_tags.pl
	##########################
	#
	sub usage{
		print "\n";
		print "USAGE: $0\n";
		print "Changes HTML tags from uppercase to lower case and vice-versa.\n\n";
		print "$0 [OPTION] FILE...\n";
		print "\n";
		print "\t-l\tChange tags to lower-case.\n";
		print "\t-u\tChange tags to upper-case.\n";
		print "\t-h\tDisplay this help.\n\n";
		exit(0);# Exit script.
	}


Other 2 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.