随笔分类 -  Perl

使用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 阅读(1150) 评论(0) 推荐(0) 编辑

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

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

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

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

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 阅读(601) 评论(0) 推荐(0) 编辑

问号在正则表达式里的四种用途
摘要:原文问号,需要转义有无量词非贪婪修饰符不捕获前缀 阅读全文

posted @ 2012-04-20 17:23 perlman 阅读(757) 评论(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 阅读(464) 评论(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 阅读(479) 评论(1) 推荐(0) 编辑

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

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

一个命令行交互脚本
摘要:按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 阅读(632) 评论(0) 推荐(0) 编辑

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 阅读(1027) 评论(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 阅读(3985) 评论(0) 推荐(0) 编辑

perl特殊符号
摘要:Perl的特殊符号 @ 数组 $x{} x名字前面是美元符号($),后面是花括号({}),则其为 hash 元素 % 要引用整个 hash,使用百分号(“ )作为前缀。前面几页中使用的 hash 的名字为%family_name。 $! 系统产生的一些可读的信息,也可能是出错的信息 $_ 子函数参数变量自己本身 @_ 子程序的私有变量 &x 调用子函数x <> 数据输入.如果没有指定文件名,则其从标准输入流中自动打开和关闭一系列文件进行读入 Perl默认的内部变量 $- 当前页可打印的行数,属于Perl格式系统的一部分 $! 根据上下文内容返回错误号或... 阅读全文

posted @ 2012-01-30 10:53 perlman 阅读(5232) 评论(1) 推荐(3) 编辑

perl帮助系统
摘要:总帮助模块帮助函数帮助内置函数帮助特殊变量帮助运算符帮助正则表达式帮助完整列表 perlsyn Perl syntax perldata Perl data structures perlop Perl operators and precedence perlsub Perl subroutines perlfunc Perl built-in functions perlopentut Perl open() ... 阅读全文

posted @ 2012-01-30 09:32 perlman 阅读(489) 评论(0) 推荐(0) 编辑

perl调试
摘要:启动调试perl -d test.pl 阅读全文

posted @ 2012-01-17 09:39 perlman 阅读(363) 评论(0) 推荐(0) 编辑

perl中my和our的区别
摘要:来自CUperl中our的用法require 5.006 当版本号小于 5.006 的时候,会返回失败,从而导致模块加载失败。 所以它的作用就是保证模块调用环境的 Perl 版本。 our 和 my 一样,都是对变量的声明, 不过 our 声明的是包全局变量, 而 my 声明的是词法变量。 不过,经过 our 声明的变量,它会变得像一个词法变量一样, 其实这也是 our 存在的目的:用来欺骗 strict pragma,使 strict 以为它是一个词法变量,其实却不是。 有一个简单的办法可以理解 our: 1,你就把 our 声明的变量和 my 声明的当成一样。 2,记住 our 和 ... 阅读全文

posted @ 2012-01-13 10:37 perlman 阅读(12194) 评论(0) 推荐(0) 编辑

perl文件操作
摘要:打开文件使用三参数的形式打开文件,这样非常便于区分模式和文件名,perl 5.6之后的版本都支持这种方式。#Open the 'txt' file for readingopen FH, '<', "$file_name.txt" or die "Error:$!\n";#Open the 'txt' file for writing. Creates the #file_name if it doesn't already exist #and will delete/overwrite a 阅读全文

posted @ 2012-01-06 09:16 perlman 阅读(4385) 评论(0) 推荐(0) 编辑

or与||
摘要:or比||优先级低,除此之外,两者无区别。下面代码输出什么?my $a = 0; $a = $a or 1;print $a, "\n";$a = $a || 1;print $a, "\n";输出:01为什么呢?因为||, =, or 这三者优先级从左至右逐渐降低。所以,这样写可以。chomp(my $filename = shift( @ARGV ) || <STDIN>);但是这样写就不行chomp(my $filename = shift( @ARGV ) or <STDIN>);会出现如下错误Can't modi 阅读全文

posted @ 2011-12-27 09:41 perlman 阅读(308) 评论(0) 推荐(0) 编辑

perl获取日起和时间
摘要:注意:localtime获取的年份是相对于1900的偏移,需要加上1900,而localtime获取的month范围是0-11,需要加1。my ($sec,$min,$hour,$day,$mon,$year,$wday,$yday,$isdst) = localtime(); $year += 1900; $mon++;my $date = "$year-$mon-$day"; print $date, "\n";== 阅读全文

posted @ 2011-12-26 15:47 perlman 阅读(1159) 评论(0) 推荐(0) 编辑

perl正则表达式杂项
摘要:$也能匹配\n见Perl语言入门,page 132, 注释61 /^.*$/能匹配"\n"么?能!因为$不仅能匹配行尾,也能匹配\n2 /^.*$/能匹配"b\n"么?能!.能b匹配. \n匹配$3 /^.*$/能匹配"\nb"么?不能!为什么?因为默认情况下,.不能匹配\n,把模式改一下变成/^.*$/s就可以了,/s表示.能匹配任意字符,包括\n====多行匹配/m看一个例子,这段代码输出hellomy $text = "hello, world,\nhello zdd,\nhello autumn";whil 阅读全文

posted @ 2011-12-26 13:36 perlman 阅读(1233) 评论(3) 推荐(0) 编辑

perl数据处理
摘要:来自CU的一道题有一组数据 ID 1 1 2 3 4 ID 2 1 2 3 4 ID 3 1 2 3 4 想变成 ID 1 1,2,3,4 ID 2 1,2,3,4 ID 3 1,2,3,4 请问该怎么写代码?谢谢了。我的代码,好复杂!use strict;use warnings;sub test { open SOURCE, '<', "d:/code/abc.txt" or die $!; open DEST, '>', "d:/code/def.txt" or die $!; my $items = q 阅读全文

posted @ 2011-12-26 09:38 perlman 阅读(1075) 评论(0) 推荐(0) 编辑

导航