实习生入职,只学过一点C被分到研发部,带我的人给我了两段perl代码,让我研究,实际我只会看一些基本架构,麻烦在座的各位帮我解释一下,我也好学习一下,前面的几句注释是我自己写的,也就仅限于此了,
#! /usr/bin/perl
use IO::Dir;
use Encode;
use File::Copy;
use strict;
sub changeFileEncoding
{
die "Error data!" if not defined $_ [0] or not defined $_ [1];
my ( $file , $base ) = @_ ;
my $dir_fh = IO::Dir->new( $file ) or die "Failed to open file : $file\n" ;
while ( defined ( my $filename = $dir_fh -> read ))//循环函数,定义了文件名就开始循环以下代码
{
print ">> Current task $filename.\n" ;//输出文件名
if ( $filename eq '.' || $filename eq '..' )//if else条件循环函数:如果文件名是.或者..那么进行以下操作
{ next ; }
else
{
my $inputFilePath = "$file\\$filename" ;
my $fileTrans = quotemeta $file ;
my $trans ;
if ( $inputFilePath =~ / $fileTrans /)
{
$trans = $';
}
my $outputFilePath = "$base$trans" ;
print "Output file path is: $outputFilePath.\n" ;
if (-d $inputFilePath )
{
if (-e $outputFilePath )
{
changeFileEncoding( $inputFilePath , $outputFilePath );
}
else
{
print "Mkdir $outputFilePath\n" ;
mkdir $outputFilePath ;
changeFileEncoding( $inputFilePath , $outputFilePath );
}
}
else
{
my @line = split /\./, $outputFilePath ;
if ( $line [-1] eq 'h' || $line [-1] eq 'cpp' )
{
open my $readUTF , "<" , $inputFilePath or die "Failed to open file $inputFilePath. \n" ;
open my $writeGBK , ">>" , $outputFilePath or die "Failed to open write file $outputFilePath. \n" ;
for (< $readUTF >)
{
my $l = encode( "gbk" , decode( "utf-8" , $_ ));
print $writeGBK $l ;
}
close $readUTF ;
close $writeGBK ;
}
else
{
copy( $inputFilePath , $outputFilePath );
}
}
}
}
}
&changeFileEncoding( shift , shift );
还有一个是这个:
#! /usr/bin/perl
use strict;
use warnings;
use File::Basename;
my $fullname = '/workfile/demo/perl_test.pl' ;
my @suffixlist =qw(.pl .txt .sv .v);
my ( $name , $path , $suffix )=fileparse( $fullname , @suffixlist );
print "name=$name\n" ;
print "path=$path\n" ;
print "suffix=$suffix\n" ;
$name =fileparse( $fullname , @suffixlist );
print "name=$name\n" ;
my $Basename =basename( $fullname , @suffixlist );
print "Basename=$Basename\n" ;
my $dirname = dirname( $fullname );
print "dirname=$dirname\n" ;