Options Parsing

 

Source.

use Getopt::Long qw(:config no_ignore_case);	# for GetOptions
use Pod::Usage;		# for pod2usage

my $bOptionHelp;	# for Option Help {boolean}
my $bOptionMan;		# for Option Man {boolean}
my @aOptionFilePath;	# for Option File path list {array}
my $sOptionDirPath;	# for Option Directory path {string}
my $bOptionSubDir;	# for Option Sub directory tracing {boolean}

fnProcess();

sub fnProcess
{
	#
	### Example: fnProcess()
	### http://open-source.tistory.com
	### by fckorea.
	### Code written by fckorea. [2013-02-03]
	#
	
	$bOptionHelp = 1 if($#ARGV == -1);
	
	return if(!fnOptionsParser());
	
	if(@aOptionFilePath)
	{
		# Processing to using File path list
		print "This is my file path list\n";
		print "$_\n" foreach(@aOptionFilePath);
	}
	
	if($sOptionDirPath ne undef)
	{
		# Processing to using Directory path
		print "This is my directory path\n";
		print "$sOptionDirPath";
		print " with sub directory" if $bOptionSubDir;
		print "\n";
	}
}

###============================== Option parsing functions ==============================###
sub fnOptionsParser ### (void) return ValidateOption[bool]
{
	#
	### Example: fnOptionsParser()
	### http://open-source.tistory.com
	### by fckorea.
	### Code written by fckorea. [2013-02-03]
	#

	GetOptions(
		"help|h|?" => \$bOptionHelp,		# Help
		"man|m" => \$bOptionMan,		# Man
		"file|f=s{1,}" => \@aOptionFilePath,	# Log file path
		"dir|d=s" => \$sOptionDirPath,		# Log directory path
		"sub|s" => \$bOptionSubDir		# Sub directory tracing
	) or pod2usage(1);
	pod2usage(1) if $bOptionHelp;
	pod2usage(-verbose => 2) if $bOptionMan;

	return 1;
}

__END__

=head1 NAME

{PROGRAMNAME} - {PROGRAMTITLE} - by fckorea.

=head1 SYNOPSIS

{PROGRAMNAME} [options]

	{OPTIONS}
	ex.) {PROGRAMNAME} -f "test.txt"
	     {PROGRAMNAME} -f "test.txt" "test2.txt"

	{OPTIONS}
	ex.)
		Directory
		    {PROGRAMNAME} -d "test"
		SubDirectory
		    {PROGRAMNAME} -s -d "test"


=head1 OPTIONS

=over 8

=item B<-h, help>

Print a brief help message and exits.

=item B<-m, man>

Prints the manual page and exits.

=item B<-d, Directory>

{OPTION DESCRIPTION}

=item B<-f, File>

{OPTION DESCRIPTION}

=item B<-s, sub>

{OPTION DESCRIPTION}

=back

=head1 DESCRIPTION

B {PROGRAM DESCRIPTION}

=cut

 

 

'Perl' 카테고리의 다른 글

[ Perl::Package ] Logger  (0) 2013.03.26
[ Perl ] Data Types  (0) 2013.02.17
[ Perl::Function ] fnGetDirFileList  (0) 2013.02.08

Message Logger Package.

 

Source.

#################################################
#	Message Logger v1.1 - by fckorea.
#   http://open-source.tistory.com
#	Variables are changed naming. [2014-01-20]
#################################################
#
#   Logger is file or printing message package.
#
#   Log file name format
#	%HOSTNAME%: Hostname of this system.
#	%DATE%: Current date.
#	%TIME%: Current time.
#
#   Message format
#	%DATE%: Current date.
#		%YEAR%: Current Year.
#		%MONTH%: Current Month.
#		%DAY%: Current Day.
#	%TIME%: Current time.
#		%HOUR%: Current Hour.
#		%MIN%: Current Minute.
#		%SEC%: Current Second.
#	%TYPE%: Message Type. [Info, Error, Etc...]
#	%MESSAGE%: Message contents.
#
#   Logging Output Type[integer]
#	1: Output message to console only.
#	10: Output message to file only.
#	11: Output message to console, file.
#
#################################################
package Logger;
use Time::localtime;	# for localtime
use Sys::Hostname;	# for hostname

###============================== Constructor ==============================###
sub new	### (self[, Logging file name{string}[, Logging console msg format{string}[, Logging file msg format{string}[, Log date format{string}[, Log time format{string}]]]]]) return self;
{
	#
	### Example: my $clsLogger = Logger->new();
	### Example: my $clsLogger = Logger->new("Result_Logger_%HOSTNAME%_%DATE%.log");
	### Example: my $clsLogger = Logger->new("Result_Logger_%HOSTNAME%_%DATE%.log", "%TIME%\t[%TYPE%] %MESSAGE%");
	### Example: my $clsLogger = Logger->new("Result_Logger_%HOSTNAME%_%DATE%.log", "%TIME%\t[%TYPE%] %MESSAGE%", "%DATE% %TIME%\t[%TYPE%] %MESSAGE%");
	### Example: my $clsLogger = Logger->new("Result_Logger_%HOSTNAME%_%DATE%.log", "%TIME%\t[%TYPE%] %MESSAGE%", "%DATE% %TIME%\t[%TYPE%] %MESSAGE%", "%YEAR%/%MONTH%/%DAY%", "%HOUR%:%MIN%:%SEC%");
	### http://open-source.tistory.com
	### by fckorea.
	### Code written by fckorea. [2013-02-03]
	#
	my ($l_clsSelf, $l_sFileName, $l_sConsoleMsgFormat, $l_sFileMsgFormat, $l_sLogDateFormat, $l_sLogTimeFormat) = @_;
	
	if($l_sFileName eq '')
	{
		$l_sFileName = (sprintf "Result_%s_%04d-%02d-%02d.log", hostname, localtime->year() + 1900, localtime->mon() + 1, localtime->mday());
	}
	elsif(index($l_sFileName, '%') > -1)
	{
		$sDATE = (sprintf "%04d-%02d-%02d", localtime->year() + 1900, localtime->mon() + 1, localtime->mday());
		$sTIME = (sprintf "%02d%02d%02d", localtime->hour(), localtime->min(), localtime->sec());
		$sHOSTNAME = hostname;
		
		$l_sFileName =~ s/%DATE%/$sDATE/g;
		$l_sFileName =~ s/%TIME%/$sTIME/g;
		$l_sFileName =~ s/%HOSTNAME%/$sHOSTNAME/g;
	}
	
	$l_sConsoleMsgFormat = "\t[%TYPE%] %MESSAGE%" if($l_sConsoleMsgFormat eq undef);	
	$l_sFileMsgFormat = "%DATE%\t%TIME%: [%TYPE%] %MESSAGE%" if($l_sFileMsgFormat eq undef);
	
	$l_sLogDateFormat = "%YEAR%-%MONTH%-%DAY%" if($l_sLogDateFormat eq undef);
	$l_sLogTimeFormat = "%HOUR%:%MIN%:%SEC%" if($l_sLogTimeFormat eq undef);
	
	my $self = {
		g_hFileLogger => undef,
		g_sLoggingFileName => $l_sFileName,
		g_sLoggingConsoleMsgFormat => $l_sConsoleMsgFormat,
		g_sLoggingFileMsgFormat => $l_sFileMsgFormat, 
		g_sLogDateFormat => $l_sLogDateFormat,
		g_sLogTimeFormat => $l_sLogTimeFormat
	};
	
	bless $self, $l_clsSelf;
	
	return $self;
}

###============================== Package info functions ==============================###
sub fnGetLogFileName	### (self) return g_sLoggingFileName;
{
	#
	### http://open-source.tistory.com
	### by fckorea.
	### Code written by fckorea. [2013-02-03]
	#

	my $l_hSelf = $_[0];
	return $l_hSelf->{g_sLoggingFileName};
}

sub fnGetConsoleMsgFormat	### (self) return g_sLoggingConsoleMsgFormat;
{
	#
	### http://open-source.tistory.com
	### by fckorea.
	### Code written by fckorea. [2013-02-03]
	#

	my $l_hSelf = $_[0];
	return $l_hSelf->{g_sLoggingConsoleMsgFormat};
}

sub fnGetFileMsgFormat	### (self) return g_sLoggingFileMsgFormat;
{
	#
	### http://open-source.tistory.com
	### by fckorea.
	### Code written by fckorea. [2013-02-03]
	#
	
	my $l_hSelf = $_[0];
	return $l_hSelf->{g_sLoggingFileMsgFormat};
}

sub fnGetLogDateFormat	### (self) return g_sLogDateFormat;
{
	#
	### http://open-source.tistory.com
	### by fckorea.
	### Code written by fckorea. [2013-02-03]
	#
	
	my $l_hSelf = $_[0];
	return $l_hSelf->{g_sLogDateFormat};
}

sub fnGetLogTimeFormat	### (self) return g_sLogTimeFormat;
{
	#
	### http://open-source.tistory.com
	### by fckorea.
	### Code written by fckorea. [2013-02-03]
	#
	
	my $l_hSelf = $_[0];
	return $l_hSelf->{g_sLogTimeFormat};
}

sub fnLogFileReset	### (self) return void;
{
	#
	### http://open-source.tistory.com
	### by fckorea.
	### Code written by fckorea. [2013-02-03]
	#
	
	my $l_hSelf = $_[0];
	open($g_hFileLogger, ">$l_hSelf->{g_sLoggingFileName}");
	close($g_hFileLogger) if ($g_hFileLogger);
}
	

###============================== Logger functions ==============================###
sub fnLogging	### (self, Logging Message[string], Message Type{string}, Output Type{integer}[, Temporary Message Format{string}[, Temporary Console Message Format{string}]]) return void;
{
	#
	### Example: $clsLogger->fnLogging("####### My Logger Start!!! #######", "", 11, "%DATE% %TIME%\t%MESSAGE%", "%TIME%\t%MESSAGE%");
	### Example: $clsLogger->fnLogging("No Printing ;)\n", "Error", 0);
	### Example: $clsLogger->fnLogging("Test Message in Console only!!!\n", "Info", 1);
	### Example: $clsLogger->fnLogging("Test Message in File only!!!\n", "Info", 10);
	### Example: $clsLogger->fnLogging("Test Message in Console & File!!!\n", ";)", 11);
	### http://open-source.tistory.com
	### by fckorea.
	### Code written by fckorea. [2013-02-03]
	#
	
	my ($l_hSelf, $l_sLoggingMsg, $l_sLoggingMsgType, $l_iLoggingMsgOutputType, $l_sTempMessageFormat, $l_sTempConsoleMessageFormat) = @_;
	
	my $l_sYear = (sprintf "%04d", localtime->year() + 1900);
	my $l_sMonth = (sprintf "%02d", localtime->mon() + 1);
	my $l_sDay = (sprintf "%02d", localtime->mday());
	
	my $l_sDate = $l_hSelf->{g_sLogDateFormat};
	$l_sDate =~ s/%YEAR%/$l_sYear/g;
	$l_sDate =~ s/%MONTH%/$l_sMonth/g;
	$l_sDate =~ s/%DAY%/$l_sDay/g;
	
	my $l_sHour = (sprintf "%02d", localtime->hour());
	my $l_sMin = (sprintf "%02d", localtime->min());
	my $l_sSec = (sprintf "%02d", localtime->sec());
	
	my $l_sTime = $l_hSelf->{g_sLogTimeFormat};
	$l_sTime =~ s/%HOUR%/$l_sHour/g;
	$l_sTime =~ s/%MIN%/$l_sMin/g;
	$l_sTime =~ s/%SEC%/$l_sSec/g;

	if($l_iLoggingMsgOutputType % 10)
	{
		# for Console
		my $l_sConsoleMsg = $l_hSelf->{g_sLoggingConsoleMsgFormat};
		$l_sConsoleMsg = $l_sTempMessageFormat if($l_sTempMessageFormat ne undef);
		$l_sConsoleMsg = $l_sTempConsoleMessageFormat if($l_sTempConsoleMessageFormat ne undef);
		
		# Log data mapping
		$l_sConsoleMsg =~ s/%DATE%/$l_sDate/g;
		$l_sConsoleMsg =~ s/%TIME%/$l_sTime/g;
		$l_sConsoleMsg =~ s/%TYPE%//g if($l_sLoggingMsgType eq undef);
		$l_sConsoleMsg =~ s/%TYPE%/$l_sLoggingMsgType/g;
		$l_sConsoleMsg =~ s/%MESSAGE%/$l_sLoggingMsg/g;
		
		$l_hSelf->fnPrint($l_sConsoleMsg, 1);
	}
	
	if(int($l_iLoggingMsgOutputType / 10))
	{
		# for File
		my $l_sFileMsg = $l_hSelf->{g_sLoggingFileMsgFormat};
		$l_sFileMsg = $l_sTempMessageFormat if($l_sTempMessageFormat ne undef);
		
		# Log data mapping
		$l_sFileMsg =~ s/%DATE%/$l_sDate/g;
		$l_sFileMsg =~ s/%TIME%/$l_sTime/g;
		$l_sFileMsg =~ s/%TYPE%//g if($l_sLoggingMsgType eq undef);
		$l_sFileMsg =~ s/%TYPE%/$l_sLoggingMsgType/g;
		$l_sFileMsg =~ s/%MESSAGE%/$l_sLoggingMsg/g;
		
		$l_hSelf->fnPrint($l_sFileMsg, 10);
	}
}

sub fnPrint	### (self, Logging Message{string}, Output Type{integer}) return void;
{
	#
	### http://open-source.tistory.com
	### by fckorea.
	### Code written by fckorea. [2013-02-03]
	#
	
	my ($l_hSelf, $l_sPrintMsg, $l_iPrintMsgOutputType) = @_;
	my $l_hFileLogger;
	
	if($l_iPrintMsgOutputType % 10)
	{
		print $l_sPrintMsg;
		print "\n" if not $l_sPrintMsg =~ m/.*(\r\n|\r|\n)$/;
	}
	
	if(int($l_iPrintMsgOutputType / 10))
	{
		open($l_hFileLogger, ">>$l_hSelf->{g_sLoggingFileName}");
		
		print $l_hFileLogger $l_sPrintMsg;
		print $l_hFileLogger "\n" if not $l_sPrintMsg =~ m/.*(\r\n|\r|\n)$/;
		
		close($l_hFileLogger) if ($l_hFileLogger);
	}
}

1;	### End Of Package;

 

Usage.

use Logger;

my $clsLogger = Logger->new("Result_Logger_%HOSTNAME%_%DATE%.log", "%TIME%\t[%TYPE%] %MESSAGE%", "%DATE% %TIME%\t[%TYPE%] %MESSAGE%", "%YEAR%/%MONTH%/%DAY%", "%HOUR%:%MIN%:%SEC%");

$clsLogger->fnLogging("####### My Logger Start!!! #######", "", 11, "%DATE% %TIME%\t%MESSAGE%", "%TIME%\t%MESSAGE%");
$clsLogger->fnLogging("No Printing ;)\n", "Error", 0);
$clsLogger->fnLogging("Test Message in Console only!!!\n", "Info", 1);
$clsLogger->fnLogging("Test Message in File only!!!\n", "Info", 10);
$clsLogger->fnLogging("Test Message in Console & File!!!\n", ";)", 11);
$clsLogger->fnLogging("####### My Logger Terminated... #######", "", 11, "%DATE% %TIME%\t%MESSAGE%", "%TIME%\t%MESSAGE%");

 

Result.

2013/03/26 19:03:07	####### My Logger Start!!! #######
2013/03/26 19:03:07	[Info] Test Message in File only!!!
2013/03/26 19:03:07	[;)] Test Message in Console & File!!!
2013/03/26 19:03:07	####### My Logger Terminated... #######

'Perl' 카테고리의 다른 글

[ Perl::Function ] fnOptionsParser  (0) 2013.04.18
[ Perl ] Data Types  (0) 2013.02.17
[ Perl::Function ] fnGetDirFileList  (0) 2013.02.08

Perl Data Types.

 

# Scalar
$sScalar = "fckorea";

# Array
@aArray = ( "fckorea", "open-source", "Perl" );

# Hash
%hashHash = ( "name" => "fckorea", "age" => "?", "blog" => "open-source.tistory.com" );

 

'Perl' 카테고리의 다른 글

[ Perl::Function ] fnOptionsParser  (0) 2013.04.18
[ Perl::Package ] Logger  (0) 2013.03.26
[ Perl::Function ] fnGetDirFileList  (0) 2013.02.08

Getting directory, File list from path.

 

Source.

use File::Find;			# for File find
use Cwd 'abs_path';		# for Absolutely path

sub fnGetDirFileList ### (Directory path{string}[, Find extension{string}[, Tracing sub-directory{bool}[, Type{string}]]]) return FileList{Array};
{
	#
	### Example: &fnGetDirFileList("C:\\Test", "txt|in?(i|f)", 1, "f")
	### Example: &fnGetDirFileList("C:\\Test", ".*", 1, "f")
	### Example: &fnGetDirFileList("C:\\Test", "", 1, "d")
	### Example: &fnGetDirFileList("C:\\Test", "txt|in?(i|f)", 1, "a")
	### Example: &fnGetDirFileList("test", "in?(i|f)", 1, "a")
	### http://open-source.tistory.com
	### by fckorea.
	### Code written by fckorea. [2013-02-03]
	#

	my ($sDirectory, $sExtension, $bSubDirectory, $sCheck) = @_;
	my $sRegex;
	my @aDirFileList;

	# Extension except
	return undef if(($sDirectory eq undef) or (-f abs_path($sDirectory)));
	$sExtension = ".*" if($sExtension eq undef);

	# Exception
	$bSubDirectory = 0 if($bSubDirectory eq undef);
	$sCheck = "f" if(($sCheck eq undef) or (grep{ $_ ne $sCheck } ("a", "d", "f")));
	$sCheck = lc($sCheck);

	# Absolutely path setting.
	$sDirectory = abs_path($sDirectory);

	if($bSubDirectory)
	{
		find( sub { push @aDirFileList, $File::Find::name if ((-f $File::Find::name) and (($File::Find::name =~ m/\.($sExtension)$/i) or ($sExtension eq ".*"))); }, $sDirectory) if(($sCheck eq "f") or ($sCheck eq "a"));
		find( sub { push @aDirFileList, $File::Find::name if ((-d $File::Find::name) and ($File::Find::name ne $sDirectory));}, $sDirectory) if(($sCheck eq "d") or ($sCheck eq "a"));
	}
	else
	{
		opendir(hDIR, $sDirectory) or die $!;

		while(my $sFile = readdir(hDIR))
		{
			push @aDirFileList, "$sDirectory/$sFile" if((-f "$sDirectory/$sFile") and (($sFile ne ".") and ($sFile ne "..")) and (($sCheck eq "f") or ($sCheck eq "a")) and (($sFile =~ m/\.($sExtension)$/i) or ($sExtension eq ".*")));
			push @aDirFileList, "$sDirectory/$sFile" if((-d "$sDirectory/$sFile") and (($sFile ne ".") and ($sFile ne "..")) and (($sCheck eq "d") or ($sCheck eq "a")));
		}
	}

	# General Path ('\' => '/' change)
	for (my $i = 0 ; $i <= $#aDirFileList; $i++)
	{
		$aDirFileList[$i] =~ s/\\/\//g;
	}

	return @aDirFileList;
}

 

Usage.

# Sample
my @alist = fnGetDirFileList("test", undef, 1, "f");

print "$_\n" foreach(@alist);

 

'Perl' 카테고리의 다른 글

[ Perl::Function ] fnOptionsParser  (0) 2013.04.18
[ Perl::Package ] Logger  (0) 2013.03.26
[ Perl ] Data Types  (0) 2013.02.17

+ Recent posts