note of Perl (二)

##########################################################
#script:alarm.pl
#purpose:using alarm function wake up a timer
#!/usr/bin/perl
print "alarm 5 seconds/n";
alarm 5;
print "sleep 100 seconds/n";
sleep 100;
print "wake up/n";

 

 

#-----------------------------------------------------
#name:array2.pl
#note:anonymity array
#!/usr/bin/perl
$name=["mike","tom","jim"];
print "/$name=$name/n";
print "first element in array is $name->[0]/n";
print "second element in array is $$name[1]/n";
print "all elements :@$name/n";

 

 

#-------------------------------------------
#name:array3.pl
#note:using push function in perl
#!/usr/bin/perl
@name=qw(jim tom peter);
$num=@name;
sub test
{
    my($a,@b)=@_;
    print "total number:$a/n";
    push(@b,"smith","sara");
    print "new array is @b/n";
}

test($num,@name);

 

 

#------------------------------------------------------
#name:AUTOLOAD.pl
#note:AUTOLOAD is the default function. join could join separate
#     strings into a single string
#!/usr/bin/perl
sub AUTOLOAD
{
    @name=@_;
    $result=join("|",@name);
    print "warnning:$AUTOLOAD is not define/n";
    print "the arry is $result/n";
}

$a="mike";
$b="geogre";
$c="tom";
hi($a,$b,$c);
exit 0;

 

sub ave
{
    @element=@_;
    $num2=$#element+1;
    $total=0;
    foreach $i (@element)
{
    $total+=$i;
}
    $e=$total/$num2;
    return $e;
}
return 5;

 

 

#-------------------------
#
#
#!/usr/bin/perl -i.bak
while(<ARGV>)
{
    tr /a-z/A-Z/;
    print;
    close ARGV if eof;
}

 

 

#--------------------------------------------------------------
#name:begin_end.pl
#note:first process is begin block,last is end block
#!/usr/bin/perl
[ -f fun.txt ] |die "fun.txt is not exist/n";
BEGIN {print "program begin/n";}
END {print "program end/n";}

 

 

#-----------------------------------------------------
#
#
#!/usr/bin/perl
package home;
 $name={wh=>"wuhan",
    bj=>"beijing",
    hn=>"hainan"
    };
bless($name,home);
print "/$name:$name/n";
print "ref : ref($name)/n";
print "wh is stand for $name->{wh}/n";

 

 

#------------------------------------------
#name:count.pl
#note:run this script once,the $number add one
#!/usr/bin/perl
open(handle,"+<test.txt");
chomp($number=<handle>);
print "you are the :",$number ," visitor/n";
$number++;
seek(handle,0,0);
print handle $number;
close(handle);

 

 

#------------------------------------------------------------
#name:dog.pl
#note:using perl module
#!/usr/bin/perl
use dog;
$mydog=new dog;
$mydog->set;
$mydog->readmsg;

 

 

#name:dog.pm
package dog;
sub new
{
    $class=shift;
    $attribute={};
    bless($attribute,$class);
}
sub set
{
    $self=shift;
    $self->{name}="xiaoHu";
    $self->{age}="0.5Y";
}
sub readmsg
{
    $self=shift;
    print "name:$self->{name}/n";
    print "age:$self->{age}/n";
}
1;

 

 

#----------------------------------------
#name:eof.pl
#usage:perl eof.pl 1.txt 2.txt
#!/usr/bin/perl
while(<>)
{
    print "$./t$_/n";
    if(eof)
{
    print "-" x 30,"end of file","-" x 30, "/n";
    close(ARGV);
}
}

 

 

##################################################
#srcipt:eval.pl
#purpose:example of eval
#!/usr/bin/perl
eval{chdir "hi"||die "error:$!/n";};
print "this process is still running/n";
print "die mes:$@/n";

 

 

#----------------------------------------
#
#
#!/usr/bin/perl
sub usage
{
    $argc=@ARGV;
    if($argc!=2)
    {
        print "usage:$0 filename find_string/n";
        exit 1;
    }
   
}


usage() ;
$filename=$ARGV[1];
$string=$ARGV[2];

open(handle,"$filename")||die "open error:$!/n";
print "1/n";
while(chomp($line=<handle>))
{
    if($line=~/$string/)
    {
        print "content:$line/n";
    }
}
close(handle);

 

 

#----------------------------------------
#name:flock.pl
#note:example of using flock function
#!/usr/bin/perl
$LOCK_EX=2;
$LOCK_UN=8;
open(handle,">test.txt");
print "what is your name?/n";
chomp($name=<STDIN>);
print "where are you born?/n";
chomp($home=<STDIN>);
flock(handle,$LOCK_EX)||die "you can not edit it now!/n";
sleep 10;
print handle "name:$name /t home:$home/n";
flock(handle,$LOCK_UN)||die "unlock error!/n";
close(handle);

 

 

#-------------------------------------------
#script:fork.pl
#purpose:example of fork
#!/usr/bin/perl
use English;
$result=fork();
if($result==0)
{

    print "this is the child process,its pid=$result/n";
    exec(date)||die "exec error:$!";
}
elsif(defined $result)
{
    print "this is the parent process,return pid=$PID/n";
$pid=    wait;
    print "the child process $pid  is terminate/n ";
}
else
{
    print "fork error:$!/n";
}

 

 

#######################################################
#script:format.pl
#purpose:using format function in perl
#!/usr/bin/per
$name="peter";
$work="coder";
$age=27;
$home="wuhan";
$birthday="1983-01-01";

format STDOUT=
---------------------report-----------------------------------------------
name:@<<<<<<work:@<<<<<<home:@<<<<<<age:@<<<birthday:@<<<<<<<<<<
$name,$work,$home,$age,$birthday
.

write

print "peter message/n";

 

 

#--------------------------------------------------
#name:function.pl
#note:look care @_ and $_[0]
#!/usr/bin/perl

sub hi
{
    print "/@_ is @_/n";
    print "/$_[0] is $_[0] and /$_[1] is $_[1]/n";
}

hi(peter,geogre);

 

 

 

################################################################
#script:gethostent.pl
#purpose:show the host and IP address from /etc/hosts
#!/usr/bin/perl
while( ($name,$aliases,$addrtype,$length,@addrs) = gethostent)
{
    ($a,$b,$c,$d)=unpack("C4",@addrs[0]);
    print "hostname:$name /t addr:$a.$b.$c.$d/n";
}

 

 

#-----------------------------------------
#script:getpwuid.pl
#purpose:usage of getpwuid function
#!/usr/bin/perl
foreach $i (1..10)
{
    if(($login,$passwd,$uid)=getpwuid($i))
{   
    print "$login--$uid/n";
}   
}

 

 

#########################################################
#script:getservbyport.pl
#purpose:input port and protocol,name alias will print out
#!/usr/bin/perl
print "input port/n";
chomp($port=<STDIN>);
print "input protocol/n";
chomp($protocol=<STDIN>);
($name,$alias,$port,$protocol)=getservbyport($port,$protocol);
format STDOUT=
----------------------------result----------------------------
name         alias         port           protocol
@<<<<<<<<    @<<<<<<<<     @<<<<<<<<      @<<<<<<<<
$name,$alias,$port,$protocol
.
write

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值