my函数在perl官方文档中,是这样定义的:
* my EXPR
* my TYPE EXPR
* my EXPR : ATTRS
* my TYPE EXPR : ATTRS
A my declares the listed variables to be local (lexically) to the enclosing block, file, or eval. If more than one value is listed, the list must be placed in parentheses.
也就是说my用在封装块,文件,或者是eval中声明局部变量列表用.如果有多于一个变量值的列表, 必须他们放置在圆括号里面.例如在下面的perl中做了全局变量和局部变量的对比:
#/usr/bin/perl
# use warnings;
$x = 10;
test();
sub test{
my $x = 20;
print "The first \$x is $x.\n";
}
print "The second \$x is $x.\n";
显示结果:
The first $x is 20.
The second $x is 10.
在上面的实例中,看到了全局变量$x和局部变量$x的不同值的有效性.
在看看local:
create a temporary value for a global variable (dynamic scoping)
local为全局变量(动态范围)定义.local赋予变量的值在执行时间才有效,也就是为当前的代码块中的变量赋予一个临时值,其值有效性包括当前代码块中含有其他的程序。可以看看下面的perl程序:
#!/usr/bin/perl
#
use warnings;
use strict;
my $x=2;
$_ = "true";
{
my $x = 10;
local $_ = "false";
somesub();
}
somesub();
sub somesub{
print "\$x is $x\n";
print "\$_ is $_\n";
}
显示结果:
$x is 2
$_ is false
$x is 2
$_ is true
尽管有$x = 10 这条语句,但仅仅对从 "my $x = 10;" 到 "somesub();"这块造成影响. 局部变量会考虑它的实际内容,而不是按照顺序执行.所以当我们调用somesub时,其实$x的值还是2.
不过,一般不推荐使用local定义局部变量.
Jeremy's Blog
work, study, life ..........
Wednesday, April 15, 2009
Monday, April 13, 2009
Linear Algebra and Its Applications
在图书馆不小心翻到这本David C. Lay 写的<<线性代数及其应用>>(<< Linear Algebra and Its Applications>>),便租回家阅读. 其中的内容非常不错,现努力学习中.
Labels:
book
Perl学习之: 模仿grep指令
最近用perl写了一个模仿grep指令的小程序,其功能是可以在多级目录下或文件中查找指定的字符串,并打印出所在对应文件的行数.可以根据需求修改一些小地方,作为删除多级目录指定的文件用.由于自身的水平不够,写的不是很好,欢迎提出意见,共同学习进步!
使用方法,先任意命名,例如 search.plx,贴入下面的代码,授予执行权限.
$ search.plx "字符串" 目录名/文件
可以指定多个目录或者是文件名。
代码如下,经过测试的:
#!/usr/bin/perl
#
##############################
#Author: jeremy
#Email: jiang313hua@gmail.com
###############################
use warnings;
use strict;
die "Usage: $0 \n" unless #@ARGV < 2 ;
my $strings = $ARGV[0];
my $para;
shift @ARGV;
foreach $para(@ARGV){
if ( -d $para){
search_dir($para);
}elsif ( -f $para) {
search_file($para);
}else{
print "Don't search.\n";
}
}
sub search_file {
my $flname = shift;
open FL, "$flname" or die "Can't open the file: $!";
my $line = 1;
while (){
if ($_ =~ /$strings/){
print "FILENAME: $flname contains strings($strings) in the $line lines!\n";
}
$line += 1;
}
close(FL);
}
sub search_dir {
my $dirs=shift;
my $flname;
opendir DH, "$dirs" or die "Couldn't open the directory -- $dirs: $!";
my @arry = readdir(DH);
foreach $flname (@arry){
next if "$flname" eq "." or "$flname" eq "..";
if ( -f "$dirs/$flname" ) {
open FL,"$dirs/$flname";
my $line = 1;
while (){
if ($_ =~ /$strings/){
print "FILENAME: $dirs/$flname contain strings ($strings) in the $line lines!\n";
}
$line += 1;
}
close(FL);
}
if ( -d "$dirs/$flname" ) {
my $dir = "$dirs" . "/$flname";
search_dir($dir);
}
}
closedir(DH);
}
使用方法,先任意命名,例如 search.plx,贴入下面的代码,授予执行权限.
$ search.plx "字符串" 目录名/文件
可以指定多个目录或者是文件名。
代码如下,经过测试的:
#!/usr/bin/perl
#
##############################
#Author: jeremy
#Email: jiang313hua@gmail.com
###############################
use warnings;
use strict;
die "Usage: $0
my $strings = $ARGV[0];
my $para;
shift @ARGV;
foreach $para(@ARGV){
if ( -d $para){
search_dir($para);
}elsif ( -f $para) {
search_file($para);
}else{
print "Don't search.\n";
}
}
sub search_file {
my $flname = shift;
open FL, "$flname" or die "Can't open the file: $!";
my $line = 1;
while (
if ($_ =~ /$strings/){
print "FILENAME: $flname contains strings($strings) in the $line lines!\n";
}
$line += 1;
}
close(FL);
}
sub search_dir {
my $dirs=shift;
my $flname;
opendir DH, "$dirs" or die "Couldn't open the directory -- $dirs: $!";
my @arry = readdir(DH);
foreach $flname (@arry){
next if "$flname" eq "." or "$flname" eq "..";
if ( -f "$dirs/$flname" ) {
open FL,"$dirs/$flname";
my $line = 1;
while (
if ($_ =~ /$strings/){
print "FILENAME: $dirs/$flname contain strings ($strings) in the $line lines!\n";
}
$line += 1;
}
close(FL);
}
if ( -d "$dirs/$flname" ) {
my $dir = "$dirs" . "/$flname";
search_dir($dir);
}
}
closedir(DH);
}
Labels:
shell/perl
Friday, April 3, 2009
perl script中使用use help的遭遇
为了在centos 5.2 上使用 xisofs,其中perl的版本为:
# perl -v
This is perl, v5.8.8 built for x86_64-linux-thread-multi
利用cpan安装help这个模块时,
cpan> install help
提示:
Package contains both files[COPYING about.pm help.pm eltorito.pm install.pl VERSION status.pm files.pm defaults.pm cdwrite.pm INSTALL wcenter.pm dirs.pm xisofs.pl Bubble.pm dlg.pm selector.pm README HISTORY] and directories[large_icons misc_icons help]; not recognized as a perl package, giving up
这让人很郁闷.
于是手工安装吧,
# cd /root/.cpan/build/xisofs-1.3-soLiZe
修改 install.pl文件如下:
$libDir = "/usr/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi
安装完毕后,可以在perl脚本中直接使用use help了。
# perl -v
This is perl, v5.8.8 built for x86_64-linux-thread-multi
利用cpan安装help这个模块时,
cpan> install help
提示:
Package contains both files[COPYING about.pm help.pm eltorito.pm install.pl VERSION status.pm files.pm defaults.pm cdwrite.pm INSTALL wcenter.pm dirs.pm xisofs.pl Bubble.pm dlg.pm selector.pm README HISTORY] and directories[large_icons misc_icons help]; not recognized as a perl package, giving up
这让人很郁闷.
于是手工安装吧,
# cd /root/.cpan/build/xisofs-1.3-soLiZe
修改 install.pl文件如下:
$libDir = "/usr/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi
安装完毕后,可以在perl脚本中直接使用use help了。
Labels:
shell/perl
Tuesday, March 31, 2009
利用openssl加密tar文件
我们在利用tar压缩打包文件,有时候需要进行加密,增强文件的安全性。
例如,需要对某一文件或者目录tar打包加密:
$ tar - zcf - 文件名/或者是目录 | openssl des3 -salt -out 压缩包名.tar.bz2
会提示输入加密密码,然后在检验一次密码。 这样就完成了。
如果直接对加密的压缩包解压,会提示相关错误信息,如对于名为 os200805.tar.bz2加密打包文件直接解压缩时:
bzip2: (stdin) is not a bzip2 file.
tar: Child returned status 2
tar: Error exit delayed from previous errors
解密:
$ openssl des3 -d -salt -in os200805.tar.bz2 | tar jxv
例如,需要对某一文件或者目录tar打包加密:
$ tar - zcf - 文件名/或者是目录 | openssl des3 -salt -out 压缩包名.tar.bz2
会提示输入加密密码,然后在检验一次密码。 这样就完成了。
如果直接对加密的压缩包解压,会提示相关错误信息,如对于名为 os200805.tar.bz2加密打包文件直接解压缩时:
bzip2: (stdin) is not a bzip2 file.
tar: Child returned status 2
tar: Error exit delayed from previous errors
解密:
$ openssl des3 -d -salt -in os200805.tar.bz2 | tar jxv
Labels:
work
Subscribe to:
Posts (Atom)
