Lower and underscore identifiers to CamelCase identifiers

Below you’ll find a perl script that will let you replace identifiers written with lower case and underscore characters with lowerCamelCase or UpperCamelCase identifiers.

Any improvement much appreciated !

#!/usr/bin/perl

use strict;
use warnings;

use File::Basename;
use Getopt::Long;
use File::Copy;

# List of ignored keywords
my @predefinedIgnoredKeywords = qw( is_int fetch_array );

# Get Perl script name
my $script_name = fileparse( $0 );

# Display usage
sub usage
{
	print "Unknown option: @_\n" if @_;
	print "Synopsis: $script_name replaces identifiers written with lower case and underscore (this_is_an_identifier)\n";
	print "          by lowerCamelCase (thisIsAnIdentifier) or UpperCamelCase (ThisIsAnIdentifier) identifiers\n";
	print "\n";
	print "Usage: $script_name [OPTIONS]\n\n";
	print "\n";
	print "OPTIONS:\n";
	print "       --filename  Reads from  (default: read from STDIN)\n";
	print "       --show                Show modified identifiers in stream (default)\n";
	print "       --list                List modified identifiers\n";
	print "       --replace             Replace identifiers in stream\n";
	print "       --lower-case          Replace with lowerCamelCase (default)\n";
	print "       --upper-case          Replace with UpperCamelCase\n";
	print "       --in-place            If --filename specified, write modifications to specified filename\n";
	print "       --ignore  Ignore the identifier \n";
	print "       --help\n";
	exit;
}

my $filename;
my $show;
my $list;
my $replace;
my $in_place;
my $upper_case;
my @userIgnoredKeywords;
my $help;

# Usage
usage() if ( @ARGV  \$filename,
		'show'             => \$show,
		'list'             => \$list,
		'replace'          => \$replace,
		'in-place'         => \$in_place,
		'upper-case'       => \$upper_case,
		'ignore-keyword=s' => \@userIgnoredKeywords,
		'help|?'           => \$help
		)
	or defined $help
	);

my $ignoredKeywords = join "|", ( @predefinedIgnoredKeywords, @userIgnoredKeywords );

$show = 1 if not $show and not $list and not $replace;

sub lowerCamelCase
{
	lcfirst join '', map ucfirst, split '_', $_[0];
}

sub upperCamelCase
{
	join '', map ucfirst, split '_', $_[0];
}

my %identifiers;

sub parseStream
{
	my $INPUT  = $_[ 0 ];
	my $OUTPUT = $_[ 1 ];

	while (  )
	{
		# Get matches and count them
		my $count=(my @var)=/[a-z]+_[a-z_]*/g;
		my @authorized = grep ! /$ignoredKeywords/, @var;
	
		if ( $show )
		{
			print $OUTPUT $_;
			if ( $count )
			{
				print $OUTPUT join( "\n", @authorized)."\n" if @authorized;
			}
		}
		elsif ( $list )
		{
			if ( $count )
			{
				if ( @authorized )
				{
					foreach my $identifier ( @authorized )
					{
						$identifiers{ $identifier }++;
					}
				}
			}
		}
		elsif ( $replace )
		{
			if ( $count )
			{
				foreach my $identifier ( @authorized )
				{
					my $newIdentifier = $upper_case ? &upperCamelCase( $identifier ) : &lowerCamelCase( $identifier );
					s/$identifier/$newIdentifier/g;
				}
				print $OUTPUT $_;
			}
			else
			{
				print $OUTPUT $_;
			}
		}
	}
}

if ( $filename )
{
	open INPUT, $filename or die "Cannot open file '$filename' !";
	if ( $in_place )
	{
		my $temporary_filename = dirname( $filename) . "/." . basename( $filename ) . ".camelCase.tmp";
		open OUTPUT, ">", $temporary_filename or die "Cannot open temporary filename for '$temporary_filename' !";
		&parseStream( *INPUT, *OUTPUT, $_ );
		close OUTPUT;
		move( $temporary_filename, $filename ) or die "Cannot move '$temporary_filename' to '$filename'\n";
	}
	else
	{
		&parseStream( *INPUT, *STDOUT, $_ );
	}
	close INPUT;
}
else
{
	&parseStream( *STDIN, *STDOUT, $_ );
}

if ( $list )
{
	foreach my $identifier ( sort keys %identifiers )
	{
		print STDOUT "$identifier\n";
	}
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s