[置顶] perl杂项

摘要: 不好分类的暂时都放到这里判断两个数组所有元素是否相同直接比较数组名字即可,不必逐个元素比较。my @a = (1..10) ;my @b = (1..10) ;if (@a == @b) { print "equal\n" ; #equal}但需要注意一点,如果是数组的引用,那么就不能直接比较了。必须先解引用再比较。my $aref1 = [1..10] ;my $aref2 = [1..10] ;if ($aref1 == $aref2) { print "equal\n" # not equal}先解引用再比较my $aref1 = [1..10] 阅读全文

posted @ 2011-10-13 10:20 perlman 阅读(924) 评论(0) 推荐(0) 编辑

2012年11月18日

使用Data::Dumper

摘要: ==use strict;use warnings;use Data::Dumper;my %hash = ( 'name' => 'zdd', 'id' => 1234,);print Dumper(\%hash); # use reference here to get a better output. == 阅读全文

posted @ 2012-11-18 01:30 perlman 阅读(1146) 评论(0) 推荐(0) 编辑

2012年9月19日

perl大小写转换

摘要: 单词首字母大写$str =~ s/(\w+)/\u$1/ 阅读全文

posted @ 2012-09-19 14:57 perlman 阅读(4970) 评论(0) 推荐(0) 编辑

2012年6月20日

@_与@ARGV

摘要: @_是子过程默认的参数列表,在子过程内使用。shift 默认参数。@ARGV是整个程序的默认参数列表,在子过程外使用。 阅读全文

posted @ 2012-06-20 13:57 perlman 阅读(1570) 评论(0) 推荐(0) 编辑

2012年6月15日

eval的两个作用

摘要: 1 捕获错误# Catch exception with eval# This is an exception of divided by zero# if errors happened, perl stores it in $@# you should check $@ immediately after eval {} block# Note, eval is a function, not a flow control clause# you need to add ';' after the blockuse strict;use warnings;eval { re 阅读全文

posted @ 2012-06-15 09:52 perlman 阅读(1735) 评论(0) 推荐(0) 编辑

2012年5月10日

Vim杂项-来自网上

摘要: 1. 全局替换(1) v + G + $ 选定全部,然后输入 :s/原始字符串/目标字符串/(2) :%s/原始字符串/目标字符串/2. 清除页面中所有行尾的空白符::%s/s+$//3. 清除所有空白:%s/(s*)+//4. 去掉所有的//注释:%s!s*//.*!!5. 去掉所有的/* */注释:%s!s*/*\_.{-}*/s*! !6. 做某些内部数据重复替换有些时候我们需要组织一些批量的数据进行命令行的执行,比如我们需要利用前面的数据生成后面的数据,例如这样的数据:/var/database/aaa.txt/var/database/bbb.txt/var/database/ccc 阅读全文

posted @ 2012-05-10 09:55 perlman 阅读(448) 评论(0) 推荐(0) 编辑

括号自动补全

摘要: 在.vimrc中加入如下代码inoremap ( ()<ESC>iinoremap [ []<ESC>iinoremap { {}<ESC>iinoremap < <><ESC>i 阅读全文

posted @ 2012-05-10 09:19 perlman 阅读(448) 评论(0) 推荐(0) 编辑

2012年4月26日

perl-CGI

摘要: 你会写ASP的话,那就试试Mason。apache编译进mason处理器,也跟IIS + ASP一样,放上去就能运行。最简单的是CGI,放在apache的cgi-bin目录,什么都不用配置就可以运行。第一个CGI脚本:#!/usr/bin/perluse strict;use CGI;my $q=CGI->new;print $q->header;print "Hello world";复制代码上述脚本存成a.pl,放在cgi-bin目录,设置可执行chmod +x a.pl,然后从浏览器访问:http://xx.xx.xx.xx/cgi-bin/a.pl输出H 阅读全文

posted @ 2012-04-26 23:24 perlman 阅读(280) 评论(0) 推荐(0) 编辑

tr

摘要: see codes belowmy $str = 'abc';my $count = ( $str =~ tr/a-z/A-Z/ );print $count, "\n"; # How many chars were traslate: 3print $str, "\n"; # The traslated string: ABC== 阅读全文

posted @ 2012-04-26 10:47 perlman 阅读(200) 评论(0) 推荐(0) 编辑

插入文件到代码中

摘要: iab papp <Esc>:r ~/Perl/test.pl<CR><Up>dd<Esc>进入命令模式:r ~/Perl/test.pl<CR> 插入test.pl中的代码到当前光标处<Up>dd,上移光标并删除一行,前一条命令会产生一个空行 阅读全文

posted @ 2012-04-26 09:47 perlman 阅读(340) 评论(0) 推荐(0) 编辑

2012年4月25日

perl常见问题

摘要: 1 local,my及our的区别2 use和require的区别3 -w, use warnings, ^W的区别,看perllexwarn4 ..与...的区别,看range operators5 q, qq, qr, qx 阅读全文

posted @ 2012-04-25 10:11 perlman 阅读(599) 评论(0) 推荐(0) 编辑

2012年4月21日

vim能映射命令么?

摘要: 比如我想将1-10行注释掉,能不能设置这样一个快捷键?1,10 cc 表示comments,这样1-10行就全部加上#号了。 阅读全文

posted @ 2012-04-21 10:43 perlman 阅读(461) 评论(0) 推荐(0) 编辑

2012年4月20日

问号在正则表达式里的四种用途

摘要: 原文问号,需要转义有无量词非贪婪修饰符不捕获前缀 阅读全文

posted @ 2012-04-20 17:23 perlman 阅读(746) 评论(0) 推荐(0) 编辑

use re 'debug' 可以查看正则表达式的匹配过程。

摘要: use strict;use warnings;use re 'debug';sub test { my $str = "123456789"; print join(":", split /(?<=...)/, $str);}test(); 阅读全文

posted @ 2012-04-20 12:56 perlman 阅读(461) 评论(0) 推荐(0) 编辑

用{}修饰变量名

摘要: 用{}修饰变量名,可以防止 _ 被解释为变量名的一部分。sub test { my $head = "abc"; my $tail = "def"; my $full = "${head}_${tail}"; print $full, "\n";}直接写成下面这样,在strict模式下是无法通过的。my $full = "$head_$tail"; 阅读全文

posted @ 2012-04-20 12:44 perlman 阅读(476) 评论(1) 推荐(0) 编辑

2012年4月19日

perl 中的$a和$b

摘要: 这两个变量是为sort函数准备的内置变量,所以声明的时候可以不加 my即使打开了strict和warnings选项也无妨,下面代码并无错误和警告。use strict;use warnings;sub test { $a = 1; $b = 2; print $a, "\n"; print $b, "\n";}test();1;from perldoc perlvarperldoc perlvar $a $b Special package variables when using sort(), see "sort" in ... 阅读全文

posted @ 2012-04-19 10:44 perlman 阅读(1009) 评论(0) 推荐(0) 编辑

2012年4月16日

perl函数原型

摘要: CU上的翻译=head1 prototype Perl 可以通过函数元型在编译期进行有限的参数类型检验。如果你声明 sub mypush (+@) 那么 mypush() 对参数的处理就同内置的 push() 完全一样了。函数声明必须要在编译 相应函数调用之前告知编译器(编译器在编译函数调用时会对相应函数用 prototype 来查询它的元型来进行参数检验,并决定怎样编译此函数调用)。元型只在不用 & 调用 函数的时候起作用。就是说在语法上如果你想像内置函数一样调用,它就表现的像 内置函数一样。如果想用过时的风格通过 & 调用,那么编译器就无视函数声明。另外 元型在函数引用如 阅读全文

posted @ 2012-04-16 12:32 perlman 阅读(2506) 评论(0) 推荐(0) 编辑

2012年4月12日

一个命令行交互脚本

摘要: 按q键退出,否则显示用户输入。#! /usr/local/bin/perl5use strict;use warnings;sub test { while (1) { # display the prompt print "sditest> "; # Get input form STDIN, this is short for my $get = <STDIN> chomp (my $get = <>); # Quit when user pressed 'q', otherwise print what ever... 阅读全文

posted @ 2012-04-12 09:59 perlman 阅读(627) 评论(0) 推荐(0) 编辑

2012年4月9日

建站手册

摘要: Apache+php+mysql配置详解http://tech.163.com/06/0206/11/299AMBLT0009159K.html 阅读全文

posted @ 2012-04-09 09:24 perlman 阅读(189) 评论(0) 推荐(0) 编辑

Vim good pages

摘要: http://yannesposito.com/Scratch/en/blog/Learn-Vim-Progressively/ 阅读全文

posted @ 2012-04-09 09:00 perlman 阅读(224) 评论(0) 推荐(0) 编辑

2012年4月4日

scp的两种方式

摘要: 如果host A 与 host B建立了信任连接(B有A的public key),那么从A向B传送文件,或者从B上传回文件都可以省略密码。但是前提是命令是在A上执行的。从A向B拷贝文件on host Ascp /tmp/file hostB:/home/users/tmp/file从B向A拷贝文件on host Ascp hostB:/home/users/tmp/file /tmp/file总结一下,如果B有A的public key,那么A与B通信皆可以省略密码,但是B与A通信仍需密码,因为A并没有B的public key. 阅读全文

posted @ 2012-04-04 19:00 perlman 阅读(585) 评论(0) 推荐(0) 编辑

2012年3月30日

perl中heredoc

摘要: 在成块打印文本的时候特别有用。格式print <<EOFyou text go hereEOFsub usage{ print <<EOF;Usage: test.pl -c config, -f file -l lines -c config file -f file name -l number of linesEOF}NOTE: the last EOF must start at the beginning of the line!!!you can use other words instead of EOF 阅读全文

posted @ 2012-03-30 10:08 perlman 阅读(1025) 评论(0) 推荐(0) 编辑

在shell帮助文档中搜索内容

摘要: 比如想查看ls的-l参数,可以先 man ls,然后再/-l即可。和vi中的搜索一样。 阅读全文

posted @ 2012-03-30 09:45 perlman 阅读(628) 评论(0) 推荐(0) 编辑

2012年3月26日

perl正则表达式返回多个匹配

摘要: 返回()中的文本。方法一:sub test { my $text = '(zdd)(autumn)(123)(456)'; while ($text =~ /\((.*?)\)/g) { print $1, "\n"; }}方法二:sub test1 { my $text = '(zdd)(autumn)(123)(456)'; my @matches = ($text =~ /\((.*?)\)/g) ; foreach my $match (@matches) { print $match, "\n"; }}== 阅读全文

posted @ 2012-03-26 15:16 perlman 阅读(7221) 评论(0) 推荐(0) 编辑

2012年3月14日

Vim正则表达式

摘要: == 阅读全文

posted @ 2012-03-14 14:38 perlman 阅读(225) 评论(0) 推荐(0) 编辑

2012年2月23日

each也能作用于数组

摘要: 一直以为each只能作用于hash,其实也能用于数组,分别取下标及其对应的值。my @array = (0, 1, 2, 3, 4, 5, 6);while (my ($i, $v) = each @array) { print "$i: $v", "\n";}== 阅读全文

posted @ 2012-02-23 11:13 perlman 阅读(232) 评论(1) 推荐(0) 编辑

2012年2月16日

Sed one line

摘要: 逆序输出文件内容,相当于Linux系统上的tac命令。但是SunOS上好像没有此命令。sed ‘1!G;h;$!d’ file分析:这里== 阅读全文

posted @ 2012-02-16 17:14 perlman 阅读(448) 评论(0) 推荐(0) 编辑

2012年2月15日

在SunOS上使用awk

摘要: Solaris 默认的awk是原汁原味的老awk没有rand.可以使用nawk代替nawk 'BEGIN{srand();k=rand();print k;}'或者使用/usr/xpg4/bin/awk 阅读全文

posted @ 2012-02-15 13:47 perlman 阅读(755) 评论(0) 推荐(0) 编辑

awk里面不要使用单引号

摘要: 因为awk以单引号来界定命令,如下awk '/pattern/{action}' files...所以,在awk中就不要再使用''了,比如下面的代码是无法通过的。awk 'BEGIN { FS=':'; print "name, number, val1, val2, val3\n";}应该写成awk 'BEGIN { FS=":"; print "name, number, val1, val2, val3\n";} 阅读全文

posted @ 2012-02-15 10:17 perlman 阅读(1364) 评论(0) 推荐(0) 编辑

[转]Vim健位映射2

摘要: http://hi.baidu.com/dingguo815/blog/item/2fef0f26d70be7108b82a122.htmlVim 的键位映射真是无所不能,一直以为插入模式中不够普通模式的快捷键多,普通模式中,按“o”能在光标下插入一行行,按“dd”删除一整行……,而在插入模 式中没有这些快捷键,很多快捷命令一直都是按“Esc”返回普通模式使用后再切换来,很麻烦,能看过键位映射后,研究一阵,大呼过瘾!于是把一系列自定义 键位映射写到“_vimrc”中,现在Vim用起来真他妈的爽!!!Alt组合键不映射到菜单上set winaltkeys=no”在 键位映射中,Ctrl加字母是不 阅读全文

posted @ 2012-02-15 10:00 perlman 阅读(3566) 评论(0) 推荐(0) 编辑

2012年2月14日

awk例子(一)

摘要: 处理文件一个文件data.dat内容如下abcxyzaaarstaaaaaa要求对重复出现的项进行计数,处理后如下。abcxyzaaa_001rstaaa_002aaa_003awk代码如下awk '/^aaa/{printf("%s_%03d\n", $0, ++i);next}{printf("%s\n", $0)}' data.dat分析一下/^aaa/{printf("%s_%03d\n", $0, ++i);next}这部分用来打印重复的项,注意不要忘记next.{printf("%s\n" 阅读全文

posted @ 2012-02-14 17:01 perlman 阅读(483) 评论(0) 推荐(0) 编辑

shell中常用的特殊符号

摘要: http://blog.chinaunix.net/space.php?uid=14647215&do=blog&cuid=482234在shell中常用的特殊符号罗列如下:# ; ;; . , / \ 'string'| ! $ ${} $? $$ $* "string"* ** ? : ^ $# $@ `command`{} [] [[]] () (()) || && {xx,yy,zz,...}~ ~+ ~- & \<...\> + - %= == != 输出/输入重导向> >> & 阅读全文

posted @ 2012-02-14 11:32 perlman 阅读(2126) 评论(0) 推荐(0) 编辑

查看当前使用的shell

摘要: 来自 http://rickie622.blog.163.com/blog/static/212388112011213407503/ps |grep $$|awk '{print $4}' (实时)不带参数的ps命令显示和当前终端有关的进程状况$$变量存储当前进程的PIDps第四列是进程所使用的命令,如果是Shell,那么显示shell名,比如sh/ksh等awk '{print $4}'就是只显示第四列的值PS:用echo $SHELL可以查看系统默认的shell查看当前发行版可以使用的shell[jack@localhost ~]$ cat /etc/sh 阅读全文

posted @ 2012-02-14 11:26 perlman 阅读(68860) 评论(0) 推荐(2) 编辑

2012年2月13日

Source命令

摘要: source命令用法:source FileName作用:在当前bash环境下读取并执行FileName中的命令。注:该命令通常用命令“.”来替代。如:source .bash_rc 与 . .bash_rc 是等效的。注意:source命令与shell scripts的区别是,source在当前bash环境下执行命令,而scripts是启动一个子shell来执行命令。这样如果把设置环境变量(或alias等等)的命令写进scripts中,就只会影响子shell,无法改变当前的BASH,所以通过文件(命令列)设置环境变量时,要用source 命令。source命令(从 C Shell 而来)是b 阅读全文

posted @ 2012-02-13 15:52 perlman 阅读(22683) 评论(0) 推荐(5) 编辑

Shell语句可以直接写在命令行上

摘要: 一般我们都是将Shell命令写在脚本里,其实可以直接写在terminal中执行,比如下面这个for循环。for i in `ls`; do echo "$i\n"; done== 阅读全文

posted @ 2012-02-13 11:16 perlman 阅读(4267) 评论(0) 推荐(0) 编辑

Perl使用chdir

摘要: 代码如下use strict;use warnings;# Print all files in a directorysub print_files { my $dir = 'd:/code'; opendir DIR, $dir or die $!; my @files = readdir DIR; chdir $dir; # Use chdir or -f will not work, since -f need absolutely path foreach my $file (@files) { if (-f $file) { ... 阅读全文

posted @ 2012-02-13 11:11 perlman 阅读(3973) 评论(0) 推荐(0) 编辑

2012年2月9日

Shell让切换目录更方便

摘要: 原文地址 http://www.cnblogs.com/zhengyuxin/articles/1933920.html让切换目录更方便: pushd,popd,dirs,cd -一,为何要使用这几个命令?可能大家会有疑问,为何要使用这几个命令,难道用cd不就可以切换目录了吗?没错,使用cd就可以切换到需要访问的目录,但是有时会是一个路径很长,层次很多的目录,进到此目录下后,这时我们不小心运行了 cd命令,理所当然,我们回到了自己的home目录,这时如果想回去怎么办?还有:因为工作的需要,我们需要不停在几个很深层的目录之间切换,不止一个,那么即使有tab键帮忙,我们也会因为一个cd命令花费很多 阅读全文

posted @ 2012-02-09 11:02 perlman 阅读(10762) 评论(1) 推荐(0) 编辑

2012年2月8日

常用文本处理方法

摘要: 取指定行取第一行sed -n '1p' file-n表示禁止默认的输出,p表示print取前N行sed -n '1,np' file或者head -n 10 file取最后一行sed -n '$p' file$表示最后一行或者tail -1 file取最后N行tail -n file取指定行到最后一行比如一个perl脚本,取定义main函数开始的行一直到最后一行。/sub main/表示main函数所在的行,$表示最后一行。sed -n '/sub main/,$p' test.pl取第M至N行head -N file | tai 阅读全文

posted @ 2012-02-08 15:47 perlman 阅读(1105) 评论(0) 推荐(0) 编辑

Shell编程-变量

摘要: 标量定义变量name=valuename是变量名,value是变量的值,比如name=zdd,注意,Shell定义变量的时候不需要加$,而使用变量的时候则需要加$,这点和perl是有区别的,perl定义变量的时候也需要$符号。定义变量的时候要注意以下几点。Shell中的变量只能由字母,数字和下划线组成,且不能以数字开头。如果变量值中含有空格,应该用引号(单引号双引号均可)括起,比如 "Hello world"或'Hello world'。等号左右都没有空格。使用变量访问一个变量的时候只要在其前面加一个$即可,比如echo $name,如果不加$,则显示变量的 阅读全文

posted @ 2012-02-08 14:56 perlman 阅读(15193) 评论(2) 推荐(1) 编辑

2012年2月6日

Shell调试篇

摘要: 检查语法-n选项只做语法检查,而不执行脚本。sh -n script_name.sh启动调试sh -x script_name.sh进入调试模式后,Shell依次执行读入的语句,产生的输出中有的带加号,有的不带,如下。带加号表示该条语句是Shell执行的。不带加号表示该语句是Shell产生的输出。+ array=(1 2 3 4 5)+ for i in '${array[*]}'+ echo 11中断调试在调试过程中可以按Ctrl + Z中断调试,观察结果,然后再按fg键继续调试即可。调试代码块上面的-x选项是调试整个脚本的,如果脚本很大,会很不方便,还有一种方法是调试某一块 阅读全文

posted @ 2012-02-06 10:53 perlman 阅读(26610) 评论(0) 推荐(3) 编辑

2012年2月2日

find命令

摘要: 查找当前目录下用户zdd创建的所有文件find . * -prune -user zdd. 表示当前目录* 表示所有文件-prune 表示只处理当前目录,不处理子目录。这种方法查找到的文件包含file和directory,如果只想显示file,不显示directory,可以使用-type参数。find . * -prune -user zdd -type f删除目录aaa下所有由用户xyz创建的文件方法一find ./aaa -user uuu -type f | xargs rm方法二find ./abc -user xyz -type f -exec rm {} \;方法三ls -l .. 阅读全文

posted @ 2012-02-02 12:38 perlman 阅读(712) 评论(0) 推荐(0) 编辑

导航