千一鬼斗 2023-03-27 16:58 采纳率: 100%
浏览 89
已结题

perl代码解释说明

实习生入职,只学过一点C被分到研发部,带我的人给我了两段perl代码,让我研究,实际我只会看一些基本架构,麻烦在座的各位帮我解释一下,我也好学习一下,前面的几句注释是我自己写的,也就仅限于此了,

img

#! /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" ;



  • 写回答

4条回答 默认 最新

  • 社区专家-Monster-XH 2023-03-27 17:06
    关注

    基于Monster 组和GPT的调写:

    
    #! /usr/bin/perl  # 指定解释器为Perl
    
    use IO::Dir;   # 导入IO::Dir模块,用于打开目录句柄
    use Encode;    # 导入Encode模块,用于编码转换
    use File::Copy;    # 导入File::Copy模块,用于复制文件
    use strict;    # 开启严格模式,强制声明变量
    
    sub changeFileEncoding   # 定义子函数changeFileEncoding
    {
        die "Error data!" if not defined $_ [0] or not defined $_ [1]; # 判断参数是否存在,如果不存在则输出错误信息并退出
    
        my ($file, $base) = @_; # 将源文件路径和目标文件路径分别赋值给$file和$base变量
        my $dir_fh = IO::Dir->new($file) or die "Failed to open file : $file\n"; # 打开源文件目录句柄,如果失败则输出错误信息并退出
        while (defined(my $filename = $dir_fh->read))   # 使用while循环遍历源文件目录中的所有文件名
        {
            print ">> Current task $filename.\n";   # 输出当前处理的文件名
    
            if ($filename eq '.' || $filename eq '..')   # 如果文件名是"."或"..",则跳过当前循环
            {
                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    # 如果文件不是目录
                {
                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>)    # 使用for循环遍历源文件中的每一行
                    {
                        my $l = encode("gbk", decode("utf-8", $_)); # 将源文件中的每一行进行编码转换
                        print $writeGBK $l; # 将转换后的编码写入目标文件
                    }
                    close $readUTF; # 关闭源文件
                    close $writeGBK;    # 关闭目标文件
                }
                else
                {
                    copy($inputFilePath, $outputFilePath); # 将源文件复制到目标文件
                }
                }
    }
    }
    }
    
    &changeFileEncoding( shift ,  shift );  # 调用子函数changeFileEncoding
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 4月4日
  • 已采纳回答 3月27日
  • 创建了问题 3月27日

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效