如何将switch语句转换为Perl

时间:2015-10-15 09:50:21

标签: perl csh

我正在将一个csh脚本移植到Perl。我在Perl中做一个switch语句。我不确定这是否正确,基于在Perl中不再使用switch语句的不同注释。如果这是对的,你能告诉我一个想法吗? 同样在switch语句中我们使用'when'还是'case'?

这是csh代码:

set machine = c16991
set pgMachine = lc0140


if ( ! -e /abc/site/home/$USER/.userauthentication) then
echo "-F- .userauthentication file must be created in /abc/site/home/$USER "
echo "-I- .userauthentication file format: <emailaddress> <unix pwd>. 
echo "-I- Please make sure /abc/site/home/$USER/.userauthentication  permission is set to 000"
exit
endif

set permissionCheck = `ls -ltra /abc/site/home/$USER/.userauthentication |  awk '{print $1}' | 
if ($permissionCheck != 'DASHrwDASHDASHDASHDASHDASHDASHDASH') then
echo "-F- /abc/site/home/$USER/.userauthentication permission is set to    $permission1"
exit
endif


@ i = 1

while ($i <= $#argv) 
   switch ($argv[$i])
   case -block:
     shift
     set DBB1 = $argv[$i]
     shift
     breaksw
   case -tag:
     shift
     set tag = $argv[$i]
     shift 
     breaksw
   case -local:
  shift
     set local = $argv[$i]
     shift
     breaksw
   case -ar:
     shift
     set arType = $argv[$i]
     shift
     breaksw  

   default:
     echo "-E- Invalid switch -> {$argv[$i]} found!"
     goto usage
    exit
   endsw
end



if ($local == 'y') then
   if ($tag == "")then
   echo "-F- Please enter tag value to proceed!"
   exit
   endif
endif


set DBB1 = $DBB

### grab data locally

set shipLogFile = "$WARD/ship/log/${DBB1}.ship.log"
set shipUsername = `grep "Username:" $shipLogFile | sed 's/.*Username: //' |    sed 's/;.*//'`
set reviewCloseFile = "$WARD/ship/ip/${DBB1}/swizzled/review/${DBB1}.close"
set accept10SumFile = "$WARD/ship/ip/${DBB1}/swizzled/pds/logs/${DBB1}.ccdo_accept10.iss.log.sum"
set shipDate = `zgrep "::RUNTIME:: SHIP end time/date:" $shipLogFile | sed 's/.*time\/date\://g' | sed 's#"##g'`

else


 ###grab data from archive [DEFAULT]

if ($shipTag == "") then
if ($DBB1 == "") then
# grab DEFAULT hip value from running WARD, $DBB
set DBB1 = $DBB
endif
# grab DEFAULT ship tag value for archive from the latest tag
set shipTag = `ls -t $PROJ_ARCHIVE/noa/${DBB1}/ip_handoff_noa | grep  "^$STEPPING" | grep RTL | grep -v RTL0 | grep -v "_TEMP" | head -n 1`
else
if ($DBB1 == "") then
# grab DEFAULT hip value from running WARD, $DBB
set DBB1 = $DBB
endif
endif

set shipUsername = `zgrep "User Name:"    $PROJ_ARCHIVE/noa/${DBB1}/ip_handoff_noa/$shipTag/${DBB1}.ip_handoff_noa.manifes t.gz | sed 's/.*\.//g' | sed 's/^ \+\| \+$//g'`
set shipLogFile =  "$PROJ_ARCHIVE/noa/${DBB1}/ship_noa/${shipTag}/ship/log/${DBB1}.ship.log"
set reviewCloseFile =   "$PROJ_ARCHIVE/noa/${DBB1}/iphandoff_review_noa/${shipTag}/review/${DBB1}.close. gz"
set accept10SumFile =    "$PROJ_ARCHIVE/noa/${DBB1}/ipqa_noa/${shipTag}/pds/logs/${DBB1}.ccdo_accept10.is s.log.sum.gz"
set shipDate = `zgrep "Current Date:"  $PROJ_ARCHIVE/noa/${DBB1}/ship_noa/${shipTag}/${DBB1}.ship_noa.manifest.gz | sed 's/.*\. //g'`

 endif






 ### create /tmp/transpose_$$.pl script

 touch /tmp/transpose_$$.pl; rm /tmp/transpose_$$.pl

 echo '#\!/usr/intel/pkgs/perl/5.8.5/bin/perl -w' >> /tmp/transpose_$$.pl
 echo 'use strict;' >> /tmp/transpose_$$.pl
 echo 'use English;' >> /tmp/transpose_$$.pl
 echo '(our $PROG_NAME = $0) =~ s#^.*/##;' >> /tmp/transpose_$$.pl
 echo 'my $file = shift;' >> /tmp/transpose_$$.pl
 echo 'open (FILE, $file) or die "***E: Error opening $file for reading: $!\n";' >> /tmp/transpose_$$.pl
 echo 'my @lines;' >> /tmp/transpose_$$.pl
 echo 'while (<FILE>){' >> /tmp/transpose_$$.pl
 echo '    chomp $_;' >> /tmp/transpose_$$.pl
 echo '    push (@lines, $_);' >> /tmp/transpose_$$.pl
 echo '}' >> /tmp/transpose_$$.pl
 echo 'print "@lines";' >> /tmp/transpose_$$.pl
 echo 'print "\n";' >> /tmp/transpose_$$.pl
 echo '1;' >> /tmp/transpose_$$.pl
 chmod 740 /tmp/transpose_$$.pl

这是Perl代码:

#!/usr/bin/perl

 use strict;
 use warnings;

 use Data::Dumper; ##print Dumper()
 use feature qw(switch);

  my $machine = c16991;
  my $pgMachine =lc0140;


  if ( ! -e /abc/site/home/$USER/.userauthentication) 
  system (echo "-F- .userauthentication file must be created in  /abc/site/home/$USER" )
  system (echo "-I- .userauthentication file format: <emailaddress> <unix pwd>.")
  system (echo "-I- Please make sure /abc/site/home/$USER/.userauthentication permission is set to 000")
  exit
  endif

  my $permissionCheck = `ls -ltra /nfs/site/home/$USER/.userauthentication | awk '{print $1}' 
    if ($permissionCheck != 'DASHrwDASHDASHDASHDASHDASHDASHDASH')
    system(echo "-F- /abc/site/home/$USER/.userauthentication permission is         set to $permission1")
    exit
    endif

  @ i = 1

 given ($i <= $#argv) 
 switch ($argv[$i])
  when(block):
  return $argv[$i]

 when(tag):
  return $argv[$i]

 when (local):

  return $argv[$i]

 when (ar):
  return $argv[$i]

 default:
  system(echo "-E- Invalid switch -> {$argv[$i]} found!")
  goto usage
  exit
  endsw
 end

2 个答案:

答案 0 :(得分:1)

您的given-when似乎正在尝试处理命令行参数。它可以工作,但根据您的需要,最好使用各种GetOpt模块中的一个。

GetOpt::Long是核心,并且会做你想做的事情:

#/usr/bin/env perl
use strict;
use warnings;

use Getopt::Long;

my %opt; 

my $DBB1; 
my $tag;
my $local; 

GetOptions ( "block=s" => \$DBB1,
             "tag=s" => \$tag,
             "local=s" => \$local ) or die "Invalid option specified";

print $tag,"\n";

这允许您:

myscript.pl --tag=fish
myscript.pl --tag fish
myscript.pl -tag fish

并将其设置为$tag。如果您使用无效的选择,它会告诉您。

我还建议您过度使用system和反对。您不需要system ( "echo ..." );,但可以print "Something\n";。 (或使用自动插入换行符的say

同样ls - 由于几个原因它很糟糕。解析ls本质上很困难,并且有一堆边缘情况可以让你绊倒。无论如何你不应该这样做。

但特别是当你产生ls然后产生awk时 - 特别是我可以告诉 - 只是为了获得单个文件的权限。如果您需要展开路径,可以使用glob(但不要)。为了得到你想要的东西,你可以使用stat

my $perms = ( stat "/nfs/site/home/$ENV{'USER'}/.userauthentication" )[2] & 07777;
if ( $perms == 0600 ) { 
    print "Is user-rw, no access to anyone else\n";
}

答案 1 :(得分:0)

我为您的脚本执行了翻译脚本。我 建议在另一个perl脚本中将字段提供为SELECT WorkHours.EmpID, WorkHours.ProjectID, Employees.FirstName FROM WorkHours INNER JOIN Projects ON WorkHours.ProjectID = Projects.ProjectID INNER JOIN Employees ON WorkHours.EmpID = Employees.EmpID GROUP BY WorkHours.ProjectID, WorkHours.EmpID 。可能有更好的方法来做你想做的事。

以下脚本不会尝试找出您希望chomp设置的内容。或者尝试纠正不良处理。它只是向您展示了一个更好的转换(不包括我的风格怪癖),您尝试使用这么多系统调用和未转换的csh。

  • -e - 带有引用路径 - 可以检查是否存在。
  • die是您退出时出错的方法。
  • File::stat::stat会为您提供权限
  • Getopt::Long会为您完成所有选项解析。
  • 你不需要为awk,grep或sed外壳。这就是Perl的工作方式:

这是代码:

$logFile