现在的位置: 首页 > 综合 > 正文

perl读写文件和命令行解析

2017年12月10日 ⁄ 综合 ⁄ 共 4768字 ⁄ 字号 评论关闭

 

一 读写文件

实例:  

use strict;
use warnings;

sub open_display_file
{
  
# the filename should be passed in as a parameter
  my $filename = shift;
  
# open file to the handle <FILE>
  open(FILE, $filename|| die "Could not read from $filename, program halting.";
  
# read the first line, and chomp off the newline
  chomp(my $firstline = <FILE>);
  
print $firstline;
  
# read other into array
  my @other = <FILE>;
  
print @other;
  
close FILE;  
}

# a test to show how to call my function
&open_display_file('test.txt');

 注释:

1)handle句柄,概念类似C++中的资源句柄,常用的打开文件时返回句柄。句柄使用类似<handle>,系统默认的输入输出句柄为<STDIN>,<STDOUT>和<STDERR>。
2)open(FILE, $filename)打开文件到句柄<FILE>中;
3)chomp去除string中的newline(\n)标志;
4)my $firstline = <FILE> 读取一行到变量;
5)my @other = <FILE> 读取所有的到数组;
6)close FILE 关闭句柄;

7)在if中可以使用一下option来判断文件:

8)打开文件时控制为读写属性,如下: 

 

二 命令行解析

1)调用perl的时候使用-s选项,例如perl -s commandline.pl -a -b=12 -c=foo -d=bar,然后在文件中使用$a,$b,$c,$d选项,实例如下:

#commandline.pl

use strict;
use warnings;

my $usage = <<EOU;
usage 
: [-a] [-b=num] [-c=str1] [-d=str2]
    
-== a option
    
-== b option
    
-== c option
    
-== d option
EOU

print $usage;

if(our $a) {print"a option is used!\n";}
if(our $b) {print"b option is used! and the value is $b\n";}
if(our $c) {print"c option is used! and the value is $c\n";}
if(our $d) {print"d option is used! and the value is $d\n";}

#calling
#perl -s commandline.pl -a -b=12 -c=foo -d=bar
#output
#usage : [-a] [-b=num] [-c=str1] [-d=str2]
#       -a == a option
#       -b == b option
#       -c == c option
#       -d == d option
#a option is used!
#b option is used! and the value is 12
#c option is used! and the value is foo
#d option is used! and the value is bar

  

2)使用Getopt::Long或Getopt::Std, 在perl文件开始使用use Getopt::Long;GetOptions("a!", "b=i", "c=s", "d:s");,然后在文件中使用选项$opt_a$opt_b,$opt_c,$opt_d, 调用为perl commandline2.pl -a -b=12 -c=foo -d=bar实例如下:

#commandline2.pl

use strict;
use warnings;

my $usage = <<EOU;
usage 
: [-a] [-b=num] [-c=str1] [-d=str2]
    
-== a option
    
-== b option
    
-== c option
    
-== d option
EOU

print $usage;

use Getopt::Long;
#use Getopt::Std;
GetOptions("a!", "b=i", "c=s", "d:s");

if(our $opt_a) {print"a option is used!\n";}
if(our $opt_b) {print"b option is used! and the value is $opt_b\n";}
if(our $opt_c) {print"c option is used! and the value is $opt_c\n";}
if(our $opt_d) {print"d option is used! and the value is $opt_d\n";}

#calling
#perl commandline2.pl -a -b=12 -c=foo -d=bar
#output
#usage : [-a] [-b=num] [-c=str1] [-d=str2]
#       -a == a option
#       -b == b option
#       -c == c option
#       -d == d option
#a option is used!
#b option is used! and the value is 12
#c option is used! and the value is foo
#d option is used! and the value is bar

 

3) 自己解析,调用为perl commandline3.pl -a -b12 -cfoo -dbar,如下:

use strict;
use warnings;

my $usage = <<EOU;
usage 
: [-a] [-bnum] [-cstr1] [-dstr2]
    
-== a option
    
-== b option
    
-== c option
    
-== d option
EOU

print $usage;

my $a = 0;
my $b = 0;
my $c = 0;
my $d = 0;

foreach my $arg (@ARGV)
{
    
if ($arg =~/^-a$/i){$a = 1;}
    
elsif ($arg =~/^-b(.*)$/i) {$b = $1;}
    
elsif ($arg =~/^-c(.*)$/i) {$c = $1;}
    
elsif ($arg =~/^-d(.*)$/i) {$d = $1;}
}

if($a) {print"a option is used!\n";}
if($b) {print"b option is used! and the value is $b\n";}
if($c) {print"c option is used! and the value is $c\n";}
if($d) {print"d option is used! and the value is $d\n";}

#calling
#perl commandline3.pl -a -b12 -cfoo -dbar
#output
#usage : [-a] [-bnum] [-cstr1] [-dstr2]
#       -a == a option
#       -b == b option
#       -c == c option
#       -d == d option
#a option is used!
#b option is used! and the value is 12
#c option is used! and the value is foo
#d option is used! and the value is bar

  

4) 在windows还可以封装为cmd文件,调用为commandline3.cmd -a -b12 -cfoo -dbar,如下:

@rem = '
@echo off
perl -S commandline3.cmd %1 %2 %3 %4 %5 %6 %7 %8 %9
goto endofperl
@rem 
';

# The above is pretty ugly, but required for DOS/NT use.  
#
#!/bin/perl
#

# commandline3.cmd

use strict;
use warnings;

my $usage = <<EOU;
usage 
: [-a] [-bnum] [-cstr1] [-dstr2]
    
-== a option
    
-== b option
    
-== c option
    
-== d option
EOU

print $usage;

my $a = 0;
my $b = 0;
my $c = 0;
my $d = 0;

foreach my $arg (@ARGV)
{
    
if ($arg =~/^-a$/i){$a = 1;}
    
elsif ($arg =~/^-b(.*)$/i) {$b = $1;}
    
elsif ($arg =~/^-c(.*)$/i) {$c = $1;}
    
elsif ($arg =~/^-d(.*)$/i) {$d = $1;}
}

if($a) {print"a option is used!\n";}
if($b) {print"b option is used! and the value is $b\n";}
if($c) {print"c option is used! and the value is $c\n";}
if($d) {print"d option is used! and the value is $d\n";}

#calling
#perl commandline3.cmd -a -b12 -cfoor -dbar
#output
#usage : [-a] [-bnum] [-cstr1] [-dstr2]
#       -a == a option
#       -b == b option
#       -c == c option
#       -d == d option
#a option is used!
#b option is used! and the value is 12
#c option is used! and the value is foo
#d option is used! and the value is bar


exit (1);
__END__
: endofperl

 

 

 完!

抱歉!评论已关闭.