Chinaunix首页 | 论坛 | 博客
  • 博客访问: 370396
  • 博文数量: 61
  • 博客积分: 2451
  • 博客等级: 上尉
  • 技术积分: 650
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-06 21:24
文章分类

全部博文(61)

文章存档

2012年(1)

2011年(44)

2010年(16)

分类: LINUX

2011-01-26 09:53:26

关于use strict 后全局变量的定义。
可以在文件中用my 定义 虽然用my但是作用域是这个文件
可以用our 定义
还可以用 use var list_of_variables 定义


Data::Dumper 
具体参考:perldoc Data::Dumper
  1. #!/usr/bin/perl
  2. use Data::Dumper;
  3. use warnings;
  4. use strict;
  5. my $a = "good";
  6. my $b = "bad";
  7. my @my_array = qw(one two three);
  8. my %some_hash =(
  9.                         "a" => "one",
  10.                         "b" => "two",
  11.                         "c" => "three",
  12.         );
  13. $Data::Dumper::Indent = 3;
  14. $Data::Dumper::Varname = "zzy";
  15. print Dumper($a);
  16. print Dumper(\@my_array);
  17. print Dumper(\%some_hash);
  18. print Dumper((\%some_hash,\@my_array));
output:
  1. [root@PC_IN_LAN learnperl]#./a
  2. $zzy1 = 'good';
  3. $zzy1 = [
  4.           #0
  5.           'one',
  6.           #1
  7.           'two',
  8.           #2
  9.           'three'
  10.         ];
  11. $zzy1 = {
  12.           'c' => 'three',
  13.           'a' => 'one',
  14.           'b' => 'two'
  15.         };
  16. $zzy1 = {
  17.           'c' => 'three',
  18.           'a' => 'one',
  19.           'b' => 'two'
  20.         };
  21. $zzy2 = [
  22.           #0
  23.           'one',
  24.           #1
  25.           'two',
  26.           #2
  27.           'three'
  28.         ];
continue。。
storable 模块使用的意义:
flw:
意义就是你可以存下来,用 U 盘拷走,然后在别人的机器上再打开。
现在是网络时代,内存里的东西,需要能够发送到另一台机器。
zhlong8:
少掉了把数据结构格式化成 xml 之类的格式,用的时候再解析这两个步骤直接两个函数解决。
Data::Dumper 提供类似的功能,不过生成文件体积稍大主要用来 debug
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Time::Local 模块
$time = timelocal($sec,$min,$hour,$mday,$mon,$year);
They accept a date as a six-element array, and return the corresponding time(2) value in seconds since the system epoch (Midnight, January 1, 1970 GMT on Unix, for example).
The reault are the inverse of built-in perl functions localtime().

查看perl的文档:
man perl 
perlvar 关于perl的内置变量
$\ : 在perl中的作用与awk中的RS一样,行分隔符。

------------------------------------------------------------------------------
perl 调用shell命令
两种方法
1 system(command); system返回的是命令的执行状态
2 `command` 或者 qx(command)
    qx相当于加转义字符吧 

perl与shell命令 通过管道连接
open(OUTPIPE,"| wc -c");
open(INPIPE,"ls -l |");

=====================================================
一个进程一旦调用exec类函数,它本身就“死亡”了,系统把代码段替换成新的程序的代码,废弃原有的数据段和堆栈段,并为新程序分配新的数据段与堆栈段,唯一留下的,就是进程号,也就是说,对系统而言,还是同一个进程,不过已经是另一个程序了。

========================================================
shift : 在函数中如果没提供数组参数的话 默认的操作对象是@_数组。如果放在外面的话,默认的操作对象是@ARGV(即命令行参数组成的数组) 其他可以查看文档。

---------------------------------------------------------
alarm 函数: 启动一个定时器。。。如果超时会产生一个ALARM 信号通过捕获该信号来达到慢系统超时的目的 后面提供一个一秒为单位的参数。 如果不提供参数的话默认以$_ 中的内容来作为参数。通过alarm 0 来关闭定时器。 函数的返回值是前一个定时器(如果有的话)留下的秒数。更详细perldoc -f alarm
alarm 一个小运用:
  1. #!/usr/bin/perl
  2. use strict;
  3. =pod
  4. my $time_out=0;
  5. $SIG{ALRM}=sub { $time_out = 1};
  6. print "Type your password: ";
  7. alarm(5);
  8. my $password = <STDIN>;
  9. alarm(0);
  10. print "you timed out \n" if $time_out;
  11. =cut
  12. #上面的代码是当超时时 不会自动返回
  13. print "Type your password: ";
  14. my $password =
  15. eval{
  16.         local $SIG{ALRM}=sub {die "timeout\n"};
  17.         alarm(5);
  18.         return <STDIN>;
  19. };
  20. # eval 中的代码如果执行成功 eval的返回值是eval代码块
  21. # 中最后一条语句的返回。如果发生错误,将会将错误存在$@中。

  22. alarm(0);
  23. chomp($password);
  24. print "\n You timed out \n" if $@=~/timeout/;
  25. print "Your password is ",$password," \n" if $password;
-------------------------------------
pipe 函数:
pipe READHANDDLE,WRITEHANDLE
作用是用来打开管道, 通过文件句柄READHANDLE 读管道,通过文件句柄WRITEHANDLE写管道
该函数可以和fork结合使用用来实现父进程与子进程之间的通信。
------------------------------------------------------------
内置变量
$. 相当于awk 中的NR 

---------------------------------------
perl 中的state 和 my 的区别
理解:相当于c语言中函数的静态局部变量(用static修饰的)和动态局部变量(auto修饰的)
程序例子:
  1. #!/usr/bin/perl
  2. use warnings;
  3. use strict;
  4. use feature "state";
  5. while(<DATA>){
  6.         state $test1;
  7.         my $test2;
  8.         $test1+=$_;
  9.         $test2+=$_;
  10.         print "state:$test1\n";
  11.         print "my:$test2\n";
  12. }
  13. __DATA__
  14. 1
  15. 2
  16. 3
  17. 4
  18. 5
  19. OUTPUT:
  20. state:1
  21. my:1
  22. state:3
  23. my:2
  24. state:6
  25. my:3
  26. state:10
  27. my:4
  28. state:15
  29. my:5
要加:use feature "state";
或者使用:use 5.010;

============================================================================
perl 中改变字符大小写的四个内置函数:
Perl提供了四个内置的函数类,从而能够轻松地改变字符和字的大小写。函数uc()会用大写返回其所有的参数,而lc()会用小写返回其参数。如果没有指定参数的话,uc()和lc()都会对$_变量进行操作。
要改变字的首字母的大小写,就要使用ucfirst()和lcfirst()。和uc()以及lc()类似,如果没有指定参数,ucfirst()和lcfirst()会对$_变量进行操作。
这四个函数都不会改变其参数;它们只是会返回其参数的副本,而这个副本经过了改变。所以,你必须将结果分派给一个变量或者打印它,或者用其他的方式使用返回的值。
这个是个函数分别相当于\l,\u,\U\E,\L\E 
阅读(2183) | 评论(0) | 转发(0) |
0

上一篇:perl 命令行

下一篇:perl File::Basename

给主人留下些什么吧!~~