perl读写文件与命令行解析的示例代码

发布时间:2020-08-21编辑:脚本学堂
本文介绍下,perl编程中读写文件、命令行解析的几个例子,有学习perl语言编程的朋友,不妨作个参考。

本节内容:
perl读写文件的例子,perl命令行解析的例子。

一,读写文件
 

复制代码 代码示例:

#!/usr/bin/perl
#site: www.jb200.com
#
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来判断文件:
perl读写文件

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

二,命令行解析

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 == a option
    -b == b option
    -c == c option
    -d == 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 $bn";}
if(our $c) {print"c option is used! and the value is $cn";}
if(our $d) {print"d option is used! and the value is $dn";}

#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 == a option
    -b == b option
    -c == c option
    -d == 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_bn";}
if(our $opt_c) {print"c option is used! and the value is $opt_cn";}
if(our $opt_d) {print"d option is used! and the value is $opt_dn";}

#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。

例子:
 

复制代码 代码示例:

#!/usr/bin/perl
#
#site: www.jb200.com

use strict;
use warnings;

my $usage = <<EOU;
usage : [-a] [-bnum] [-cstr1] [-dstr2]
    -a == a option
    -b == b option
    -c == c option
    -d == 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 $bn";}
if($c) {print"c option is used! and the value is $cn";}
if($d) {print"d option is used! and the value is $dn";}

#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。

例子:
 

复制代码 代码示例:

#!/usr/bin/perl
#site: www.jb200.com
#
@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 == a option
    -b == b option
    -c == c option
    -d == 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 $bn";}
if($c) {print"c option is used! and the value is $cn";}
if($d) {print"d option is used! and the value is $dn";}

#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