perl读写文件和命令行解析
2013-03-24 19:25
253 查看
一 读写文件
实例:
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 == 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,如下:
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,如下:
@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
完!
实例:
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 == 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,如下:
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,如下:
@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读写文件和命令行解析
- Android-解析/生成xml、文件读写、数据库操作
- 常见布局、文件读写权限、xml解析、SharedPreference
- perl命令行批量修改文件内容
- perl下关于文件读写,hash统计频数并排序的总结
- php的SimpleXML方法读写XML接口文件实例解析
- proc源码解析(六)--proc文件读写函数
- Perl读写文件&字符串操作
- perl 文件读写详细说明
- iMOOC学习笔记:Java的Excel读写操作入门实践-利用POI解析Excel文件(二)
- Caffe命令行解析及log日志文件生成
- Perl 读写 .gz .tgz 等压缩文件
- 使用命令行解析php文件
- [Perl]用XML::Simple解析XML文件
- Python读写txt文本文件的操作方法全解析
- Java nio(文件读写 实例解析)
- Python读写txt文本文件的操作方法全解析
- perl对大量大文件进行读写
- Python读写txt文本文件的操作方法全解析