Pure-Tech

导航

perl中的奇怪符号

#! /usr/bin/perl
use strict;
use utf8;

#1  符号:$&   $`  $'
my $str="my name is  hello world,and  her name  is hello world";
if($str=~m/name/g){
    print $`,"\n";#匹配到的字符串的前面部分
    print $',"\n";#匹配到字符串的后面部分
    print $&,"\n";#匹配到的字符串
}

#result:
my 
 is  hello world,and  her name  is hello world
name

 

#2  符号:  $/ 改变分隔符,默认为换行符


#!/usr/bin/perl

open IN,"/home/hqh/桌面/2";
my @data=<IN>;
print scalar  @data;
#结果为2

open IN,"/home/hqh/桌面/2";
$/="";                                     #这里把分隔符设置为空,所以全部读入到@data中
my @data=<IN>;
print scalar  @data;
#结果为1

 

#3 符号:$@,返回eval块中错误信息
eval{
    my $str=undef;
    print $str+2;
    print 1/0;
};
if($@){
    die "come on  $@";
}
=cut
#4 符号:$_[0],@_
sub test{
    my $name=$_[0];
    my $age=$_[1];
    print $name,"\n";
    print $age,"\n";
}
&test("hello world",20);#$_[0]为"hello world",$_[1]为20

##等价于下面的表达式

sub test1{
    my($name,$age)=@_;
    print $name,"\n";
    print $age,"\n";
}
&test1("hello world",20);


#5 符号:$|=1表示缓冲区有数据就发送出去
local $|=1;
my $str="yellow";
open OF,">/home/hqh/桌面/out";
print OF $str;
$|=0;

 

  

posted on 2015-01-08 17:27  Pure-Tech  阅读(228)  评论(0)    收藏  举报