教程集 www.jiaochengji.com
教程集 >  脚本编程  >  perl  >  正文 perl读写文件与命令行解析的示例代码

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

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

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

一,读写文件
 

复制代码 代码示例:

#!/usr/bin/perl
#site: www.jiaochengji.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 $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 == 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_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。

例子:
 

复制代码 代码示例:

#!/usr/bin/perl
#
#site: www.jiaochengji.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 $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。

例子:
 

复制代码 代码示例:

#!/usr/bin/perl
#site: www.jiaochengji.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 $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

您可能感兴趣的文章:
Perl调用外部命令的方式与区别
《Perl编程24学时教程》笔记第17课 perl的CGI概述
perl读写文件与命令行解析的示例代码
linux安装perl模块的方法
perl实例之http请求的小例子
perl实例之文件读写操作
inux shell初级入门教程
后缀名为php是什么意思
php编程之命名规范
mysqlhotcopy错误Can’t locate DBI.pm的解决方法

[关闭]
~ ~