perl读写文件和命令行解析

perl读写文件和命令行解析

 

一 读写文件

实例:  

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
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值