sub pp_syslog {
my $message = $_[0];
my $level = $_[1];
my $stderr = $_[2];
my $ppname = $0;
$ppname =~ s/[^\/]*\///g;
$message =~ s/\n//g;
# ファイルは存在するように
if (! -e "$path_ActiveLog") {
open(F,"> $path_ActiveLog");
close(F);
}
my $ret_ = open(FILE_TMPLOG,">>$path_ActiveLog");
if (defined($ret_)) {
my $date_ = &formatting_date(&init_unique_prefix());
print FILE_TMPLOG "$date_ $level $ppname : $message\n";
close(FILE_TMPLOG);
}
openlog($ppname, "ndelay", "local7");
syslog($level, $message);
closelog();
if ($stderr == 1) {
warn "$message\n";
}
}
#------------------------
#
#------------------------
my $MainPath = "/local/path/files";
my @AllFileList = &GetAllFileList($MainPath);
print map{ $_ . "\n" }@AllFileList;
#------------------------
#
#------------------------
sub GetAllFileList {
my $Path = $_[0];
opendir(OPEN_DIR, $Path) or die "Cannot open directory. \-\> $Path \n";
my @AllFileList = grep{ /^[^\.]/ }readdir(OPEN_DIR);
@AllFileList = map{ s/$_/$Path\/$_/; $_; }@AllFileList;
close(OPEN_DIR);
return(@AllFileList);
}
.
.
指定したdirectoryから file nameを取得するsub routine。
sub routine内部でfull pathを付け加えて返しているのが特徴。
.
미리 지정한 directory에서 file명을 배열 안에 넣어주는 sub routine.
sub routine 안에서 full path를 파일명 앞에 붙여서 return하고 있는 부분이 특징.
my $MainPath = $ARGV[0];
my $OutputPath = $ARGV[1];
my $CopyYesNo = $ARGV[2];
my $CopyFilePath = $ARGV[3];
if( ($MainPath =~ /^$/) or ($MainPath !~ /\//) or
($OutputPath =~ /^$/) or ($OutputPath !~ /\//) ){
print "Please check path that you have inputted.\n";
&show_usage();
exit(0);
}
unless($CopyYesNo =~ /^$/){
unless($CopyYesNo =~ /\-c$/){
print "Please check the copy option. -> -c \n";
&show_usage();
exit(0);
}
if( ($CopyFilePath !~ /\//) or ($CopyFilePath =~ /^$/) ){
print "Please check the path. -> $CopyFilePath \n";
&show_usage();
exit(0);
}
}
.
.
perl script実行時にいろんな形でoptionを使うことができる。
例えば、-c を入力しないと実行できないようにしたり、-d を入力して directory pathを入力するようにもできる。
但し、option入力時の間違いを防ぐため、若干の工夫が必要となる。
上記はそのこつを簡単に纏めたものである。
.
perl script를 사용하기 전 자신이 따로 쓰고싶은 옵션을 지정할 수 있다.
예를 들어 실행 전에 -c 를 입력하지 않으면 실행이 안된다거나, -d 를 입력해 directory path를 지정할 수 있도록 하는 것이 그 예이다.
하지만 이 역시 사람이 입력하는 것이기에 틀리기 쉬운 법.
간단한 error처리를 위와같이 하면 된다.
#!/usr/local/bin/perl
#
#-------------------
# Sort by hash
#-------------------
my @TempArr = ( '6|6|1,1,0,0,1,1,1,0,0,0,0......',
'4|6|1,1,0,0,1,1,1,0,0,0,0......',
'4|5|1,1,0,0,1,1,1,0,0,0,0......',
'4|4|1,1,0,0,1,1,1,0,0,0,0......',
'5|6|1,1,0,0,1,1,1,0,0,0,0......',
'5|5|1,1,0,0,1,1,1,0,0,0,0......',
'6|7|1,1,0,0,1,1,1,0,0,0,0......' );
my @Sorted = sort SortFunc @TempArr;
foreach my $c (@Sorted) {
print $c . "\n";
}
exit(0);
#-------------------
# Sub routine
#-------------------
sub SortFunc {
my @lA = split(/\|/, $a);
my @lB = split(/\|/, $b);
if ($lA[0] != $lB[0]) { return ($lA[0] <=> $lB[0]); }
return ($lA[1] <=> $lB[1]);
}
.
.
Sub routineを使い、2Column目の値でSortするスクリプト。
普通のsortだと先頭の値か、交尾の値が基準となってsortされるため、
2column目の値からのsortがかなりややこしくなる。
下記のscriptはsub routineを使い、sortしたいcolumnを自由に選んでsortすることができる。
.
Sub routine을 이용해 2Column째 수치 등을 sort할 수 있슴.
그냥 sort를 사용하면 맨 앞이나 맨 뒤의 수치밖에sort가 안되기 때문에
2column의 데이터를 sort하려면 script가 꽤나 길어지게 됨.
때문에 아래 scrupt를 사용하면 간단하게 sort가 가능해짐.
.