随笔分类 - perl
摘要:1. 进入debug模式 # perl -d ./perl_debugger.pl it prompts, DB<1> 2. 查看从第10行开始的代码。 查看函数get_pattern的代码 DB<1> l 10 10: my $pattern; DB<2> l get_pattern 11 { 1
阅读全文
摘要:#http://perlmaven.com/open-and-read-from-files#mode operand create truncate#read yes yes #append >> yes Case 1: Throw an exception if yo...
阅读全文
摘要:转自:http://blog.charlee.li/perl-xml-simple/[Perl]用XML::Simple解析XML文件在Perl中解析XML的方法最常见的就是使用XML::DOM和 XML::Simple了。 XML::DOM过于庞大,而且解析结果是一个DOM树,操作也不方便。对于小型且不复杂的XML文件,XML::DOM真是杀鸡用牛刀。 这时就轮到轻便的XML::Simple上场了。XML::Simple如其名,真的很简单。假设XML内容如下: This is a test.那么只需这样写:use XML::Simple;use ...
阅读全文
摘要:来自:http://www.bagualu.net/wordpress/?p=1628使用signal,能让你的程序功能更丰富。要在Linux下列出所有的signal, 利用kill -l即可。 下面是我机器上的输出(后面还有到64的没列出来):xuyang@xuyang-desktop:/$ kill -l1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP6) SIGABRT 7) SIGBUS 8 ) SIGFPE 9) SIGKILL 10) SIGUSR111) SIGSEGV 12) SIGUSR2 13) SIGPI...
阅读全文
摘要:1 去除一个数组中的重复元素:使用grep函数代码片段: 代码:my @array = ( 'a', 'b', 'c', 'a', 'd', 1, 2, 5, 1, 5 ); my %count; my @uniq_times = grep { ++$count{ $_ } $max; }知道的这样搞:use List::Util qw(max);my $max_num = max( 0 .. 1000 );知道的他们还这样搞: use List::Util qw(maxstr);my $max_str = max
阅读全文
摘要:转自:http://perl.apache.org/docs/general/perl_reference/perl_reference.htmluse(), require(), do(), %INC and @INC ExplainedThe @INC array@INCis a special Perl variable which is the equivalent of the shell'sPATHvariable. WhereasPATHcontains a list of directories to search for executables,@INCcontain
阅读全文
摘要:原文地址:http://blogread.cn/it/article/743?f=wb用Perl写了一些监控脚本,放在crontab中调度执行。有时候会发现一个脚本运行时间过长,会同时跑起多个实例,因此有必要为脚本加上控制,只运行一个实例。最简单自然的想法,在脚本中检查并创建一个空的lock文件,脚本结束时再删除。通过判断文件是否存在的方式来判断脚本是否已经运行。不过这样做有个bug,如果脚本运行过程中异常终止,lock文件没有正常删除,就会导致脚本无法再运行。空的lock文件不行,那么考虑在lock文件中加入一点内容,比如进程的PID号,然后通过检查该PID号的进程是否还在运行,就能避免上述
阅读全文
摘要:http://stackoverflow.com/questions/5703705/print-current-directory-using-perl?rq=11)The following get the script's directory, which is not the same as the current directory. It's not clear which one you want.use Cwd qw( abs_path );useFile::Basename qw( dirname );say dirname(abs_path($0));oru
阅读全文
摘要:格式优美的perl代码不但让人赏心悦目,而且可以方便阅读.perltidy的是sourceforge的一个小项目,在我们写完乱七八糟的代码后,他能像变魔术一样把代码整理得漂漂亮亮,快来体验一下吧!!!perltidy 主页: http://perltidy.sourceforge.net/perltidy.html安装方法:进入解压后的目录,然后执行一下命令perl Makefile.PLmakemake testmake install使用方法:配置一下vim,使得我们在写代码的时候,不离开vim就可以美化我们代码。在/etc/vimrc最后一行加入:(意思是快捷键pt来调用perltidy
阅读全文
摘要:来自:http://www.perlfect.com/articles/sorting.shtmlperl 比较操作符列表: NumbersStrings < lt > gt <= le >= ge ...
阅读全文
摘要:安装你自己的perl modules。当没有root权限的时候,需要安装perl modules到自己的home目录下。来自:http://servers.digitaldaze.com/extensions/perl/modules.htmlInstalling Perl5 Modules Loc...
阅读全文
摘要:http://www.garybeene.com/vb/tut-per2.htm完!
阅读全文
摘要:代码: #!/usr/local/bin/perluseCGI':standard';printheader;printstart_html("ExampleCGI.pmForm");print"<h1>ExampleCGI.pmForm</h1>\n";print_prompt();do_work();print_tail();printend_html;subprint_prompt{printstart_form;print"<em>What'syourname?</em&
阅读全文
摘要:1)文件读取的3中方法按行读,存入标量while (<FILE>) { print; }按行读,存入数组@array = <FILE>;读入整个文件 ,存入标量$string = do { local $/; <FILE>; };2)读文件实例open (EP,"/etc/passwd");while (<EP>) {chomp;print "I saw $_ in the password file!\n";}3)读写文件实例open(IN,$a) || die "cannot open $a
阅读全文
摘要:使用正则表达式分两步走:先去掉前面的: $a=~s/^ +//;在去掉后面的: $a=~s/ +$//;一步就可以:s/(^s+|s+$)//g;删除字符串末尾的换行:chomp函数通常会删除变量里包含的字符串尾部的换行符。它是chop函数的一个略微安全些的版本,因为它对没有换行符的字符串没有影响。更准确地说,它根据了解$/的当前值删除字符串终止符,而不只是最后一个字符。和chop不同,chomp返回删除的字符数量。完!
阅读全文
摘要:一 CGI.pm中的方法(routines)调用1. CGI.pm实现了两种使用方法,分别是面向对象的方式和传统的perlmodule方法的方式。面向对象的方式: #!/usr/local/bin/perl -w use CGI; # load CGI routines $q = CGI->new; # create new CGI object print $q->header, # create the HTTP header $q->start_html('hello world'), # start t...
阅读全文
摘要:参考:http://www.willmaster.com/library/manage-forms/using_perl_to_submit_a_form.phphttp://www.oschina.net/code/snippet_12_854有时需要在perl中非交互地调用已有的cgi来完成一定的功能,此时需要模拟一个http请求来调用cgi。get方式调用: 1useHTTP::Request::Common;2useLWP::UserAgent;3$user_agent=LWP::UserAgent->new;4$request=GET'http://clearcase/
阅读全文
摘要:参考:http://docstore.mik.ua/orelly/linux/cgi/ch15_03.htmhttp://stackoverflow.com/questions/2224158/how-can-i-send-post-and-get-data-to-a-perl-cgi-script-via-the-command-linehttp://search.cpan.org/~lds/CGI.pm-3.20/CGI.pm#DEBUGGING一 一般地我们可以使用以下方法来检查cgi脚本的错误:1)使用-cwT来检查cgi脚本的语法,警告。例如perl -wcT your.cgi.2)
阅读全文
摘要:一 此cgi既是提交前的form,也被用来处理form的提交来自:http://www.devdaily.com/perl/perl-cgi-example-scrolling-list-html-form代码: (多选listbox-Multiple-choice SELECTs实例)不带参数时即为form:http://xxxx/cgi/perl-cgi2.cgi当点击form的submit提交时,实际上相当于:http://xxxx/cgi/perl-cgi2.cgi?languages=c&languages=html,此时为对form的处理结果#!/usr/bin/perl-
阅读全文
摘要:代码:http://xxxxx/cgi/perl-cgi.cgi?name=itech&url=itech.cnblogs.com&p1=test1#!/usr/bin/perl-wT#shouldusestrictandwarnningusewarnings;usestrict;useCGI;#todebugerroruseCGI::Carpqw(warningsToBrowserfatalsToBrowser);#ordebugfromcommandlineby:perl-cwTyourcgi.cgi#ordebugby:tail/var/log/apache/error_
阅读全文