#------------------------------------------------
#name:pipe.pl
#note:usage of pipe in perl
#!/usr/bin/perl
open(handle,"|tr [a-z] [A-Z]");
print "input a string/n";
$string=<STDIN>;
print "the uppercase is/n ";
print handle "$string /n";
close(handle);
#--------------------------------------
#name:point.pl
#note:using point in perl
#!/usr/bin/perl
$a=123;
$b=/$a;
print "the address assign to /$a is $b/n";
print "the value store in /$a is $$b/n";
#--------------------------------------------------
#name:point2.pl
#note:using point in perl
#!/usr/bin/perl
package employee;
sub msg
{
$class=shift;
$ref={name=>undef,
salary=>undef
};
bless($ref,$class);
return $ref;
}
$a=employee->msg;
$a->{name}="peter";
$a->{salary}=5000;
print "belong class:ref($a)/n";
print "ref->name:$a->{name}/n";
print "ref->salary:$a->{salary}/n";
#-------------------------------------
#script:read.pl
#purpose:list a specify file content with specify byte length
#!/usr/bin/perl
print "input filename/n";
chomp($file=<stdin>);
print "input bytes need to read from file/n";
chomp($length=<stdin>);
open(handle,$file);
$number=read(handle,$msg,$length);
print "msg:$msg/n/
number:$number/n";
########################################################
#script:print_file.pl
#purpose:print message from a file
#!/usr/bin/perl
#cat test
#tom:55:50000:5/19/66
#jack:44:45000:5/6/77
#peter:32:35000:4/12/93
open(handle,"test")||die "open error:$!";
while(<handle>)
{
($name,$age,$salary,$birthday)=split(":");
format STDOUT=
name:@<<<<<<age:@<<<salary:@<<<<<<birthday:@<<<<<<<<<
$name,$age,$salary,$birthday
.
write
}
format STDOUT_TOP=
-------------------employee information-----------------------
.
close handle
#----------------------------
#
#
#!/usr/bin/perl
require "average.pl";
print "input a few number,separate from whitespace/n";
@num=split('',<STDIN>);
$result=ave(@num);
print "the average is ",$result,"/n";
###########################################################
#
#
#!/usr/bin/perl
$keyword=$1;
$filename=data;
$count=0;
print "$0 keyword_to_search filename/n";
open(handle,$filename)||die "open error:$!/n";
seek(handle,0,0);
while(<handle>)
{
$line=$_;
chomp($line);
($name,$id,$age,$salary)=split(//t/,$line);
print "$name/n";
if($keyword eq $name)
{
print "$name,$id,$age,$salary/n";
$count++;
}
}
if($count==0)
{print "the keyword is not found yet!/n";}
else
{print "$count found/n";}
#------------------------------
#name:seek.pl
#note:exaple using seek function
#!/usr/bin/perl
open(DATA,"test.txt");
while(<DATA>)
{
print $_ if $_=~/THMONO/;
}
seek(DATA,7,0);
while(<DATA>)
{
if($_=~/THMONO/)
{print "THMONO found this file/n";}
else
{print "sorry,the word can not find!/n";}
}
close(DATA);
#--------------------------------------
#
#
#!/usr/bin/perl
open(HANDLE,">1.txt");
select(HANDLE);
open(HANDLE,"<test.txt")||die "can not open test.txt/n";
while(HANDLE)
{
print ;
}
select(STDOUT);
print "I am back! guys!/n"
#-------------------------------------------------
#name:split.pl
#note:split a string into a array
#!/usr/bin/perl
print "input 5 numbers,separate by whitespace/n";
@num=split('',<STDIN>);
print "the /@num are @num/n";
print "the firt argument in array is $num[0]/n";
print "the total arguments in this array is ",$#num,"/n";
print "-" x 25,"top number add 2","-" x 25,"/n";
foreach $arg (@num)
{
$arg+=2;
print "the new number is $arg/n";
}
######################################################
#script:signal.pl
#purpose:catch a signal and execute a function
#!/usr/bin/perl
sub handle
{
local($sig)=@_; #why need local here
print "catch signal: $sig,you will be very luck today!/n";
exit 1;
}
$SIG{'INT'}='handle'; #can not understand,why is incorrect this
#$SIG{'INT'}=handle();
print "hi,all! hit ctrl+c ,you will be surprise!/n";
sleep 10;
#----------------------------------------------------------------
#name:split.pl
#note:print $number[0],if $number[1] contains "wuhan
"#!/usr/bin/perl
while(<DATA>)
{
@number=split(":",$_);
print $number[0],"/n" if $number[1] =~/wuhan/;
}
__DATA__
hubei:wuhan
hainan:haikou
zhenzhou:henan
#------------------------------------------------
#name:split2.pl
#note:useful of split function,look care! "^" means prefix with something
#!/usr/bin/perl
while(<DATA>)
{
($name,$job,$salary)=split(":",$_);
print "this person salary is great than 4000:$name/n" if $salary>4000;
print "name prefix with /"tao/" is :$name/n" if $name=~/^tao/;
}
__DATA__
geogre tao: doctor:6000
tao mike:engineer :4500
peter :driver :2500
#-------------------------------------
#script:stat.pl
#purpose:show file inode with stat function
#!/usr/bin/perl
print "input a file /n";
chomp($file=<stdin>);
open(handle,"$file");
@msg=stat(handle);
print "file inode:@msg/n";
#--------------------------------------
#name:subroutine.pl
#note:&hi is equal to hi()
#!/usr/bin/perl
&hi if defined &hi;
bye();
sub hi
{
print "hi everyone!!!/n";
}
sub bye
{
print "goodbye guys!!!/n";
}
#--------------------------------------------
#name:sum.pl
#note:calculate sum for add 1 through 100
#!/usr/bin/perl
sub sum
{
my @arg=@_;
my $sum=0;
foreach $num (@arg)
{
$sum+=$num;
}
return $sum;
}
my @input=(1..100);
my $result=sum(@input);
print "1+2+...+100=",$result,"/n";
#--------------------------------------
#name:swap_cal.pl
#note:$& is equal $salary
#!/usr/bin/perl
$salary=3000;
$salary=~s/$salary/$&*1.8/e;
print "the new salary is /$now:$salary./n"
#---------------------------
#!/usr/bin/perl
print "this file is readable/n" if -r "1.txt";
######################################################################
#
#
#!/usr/bin/perl
socket(client_fd,1,1,0);
do
{
$result=connect(client_fd,"./test");
if($result==1)
{
read(client_fd,$buf,100);
last 0;
}
}while($result!=1);
shutdown(client_fd,2);
############################################################
#
#
#!/usr/bin/perl
socket(server_fd,1,1,0)||die "socket error:$!/n";
print "socket success/n";
$name="./test5";
unlink "test5"||warn "unlink error:$!/n"
bind(server_fd,$name)||die "bind error:$!/n";
print "bind success/n";
listen(server_fd,5)||die "listen error:$!/n";
print "listen success/n";
while (1)
{
accept(newsocket,server_fd)||die "accept error:$!/n";
$pid=fork();
if($pid==0)
{
print newsocket "hi,client!!! the message is send from server!/n";
shutdown(newsocket,2);
}
shutdown(newsocket,2);
}
#-----------------------------
#name:unshift.pl
#note:add a top array element
#!/usr/bin/perl
@a=(1,2,3,4,5);
unshift(@a,0);
print "@a/n";
#-----------------------------------------------------
#script:utime.pl
#purpose:update file's access and modify time,
# creating it if it not exist
#!/usr/bin/perl
$data=time;
print "input your file please!/n";
chomp($file=<STDIN>);
print "data is $data/n";
utime($data,$data,$file)||open(TMP,">$file");