#---------------------------------------------
#
#
#!/usr/bin/perl
#use HOME ;
require "home.pm" ;
tie %house,"home","red",6,20000;
print "the house's colour is %house->{color}/n";
%house->{color}="green";
print "the new colour is %house->{color}/n";
#------------------------------------------------
#
#
package home;
sub TIEHASH
{
$class=shift;
$color=shift;
$number=shift;
$price=shift;
$house={color=>$color,
number=>$number,
price=>$price};
bless %house,$class ;
}
sub STORE
{
$self=shift;
$key=shift;
$value=shift;
print "STORE is running/n";
$self->{$key}=$value;
}
sub FETCH
{
$self=shift;
$key=shift;
print "FETCH is running/n";
return $self->{$key};
}
1;
#-------------------------------
#name:local.pl
#note:using local in perl
#!/usr/bin/perl
sub a
{
local @name=($_[0],$_[1]);
print "the local /@name are @name/n";
}
a("peter","mike");
print "@name" if defined @name; #@name in this will no echo
#------------------------------------------------
#name:login.pl
#note:test if peter is login in
#!/usr/bin/perl
open(handle,"who|");
while($name=<handle>)
{
if($name=~/peter/)
{$result=1;last;}
else
{$result=0;}
}
if($result == 1)
{print "peter is login in/n";}
else
{print "peter is login out/n";}
#-----------------------------------------------------------------
#name:match.pl
#name:test of regular expressions
#!/usr/bin/perl
while(<DATA>)
{
print "begin with /"Bl/":$_/n" if $_=~/Bl/ ;
print "begin with /"[3-4][0-9][0-9]/":$_/n" if $_=~/[3-4][0-9][0-9]/;
print "begin with four words before numbers :$_" if $_=~/ /w/w/w/w /d/;
}
__DATA__
Steve Blenheim 101
Betty Boop 201
Lgor Chevsky 301
Norma Cord 401
#---------------------------------------------------
#name:max_min.pl
#note:print the maximum and minimum number
#!/usr/bin/perl
use strict "vars";
sub max
{
my $max=shift @_;
foreach $a (@_)
{
$max=$a if $a>$max;
}
return $max;
}
sub min
{
my $min=shift @_;
foreach $b (@_)
{
$min=$b if $b<$min;
}
return $min;
}
my $max_num=max(34,1,25,100);
my $min_num=min(99,2,41,1);
print "the max number is $max_num and min number is $min_num/n";
#----------------------------------------------
#name:meta_character.pl
#note:useful of dot,one dot is stand for a character
#!/usr/bin/perl
while(<DATA>)
{
print if /t...no/;
print $_ if $_=~/t...no/;
}
__DATA__
hi all
I am back
talent thmono
#-----------------------------------------------
#name:mynumber.pl
#note:take care "use strict "vars""
#!/usr/bin/perl
use strict "vars";
my @a=(1,2,3);
sub mynumber(@$;$)
{
my $b=pop(@_);
my @c=@_;
print "the scalar is $b/n";
print "the array is @c/n";
}
mynumber(@a,200);
#-----------------------------------------
#name:my_package.pl
#note:access package variable
#!/usr/bin/perl
package main;
$name="peter";
my $age="27";
package hi;
print "/$name is $main::name/n";
print "/$age is $main::age/n"; #can not access my variable
#!/usr/bin/perl
#------------------------------------------------
#name:nested_array.pl
#note:nested array in perl
$array=[[1,2,3],
[4,5,6],
[7,8,9]];
for($i=0;$i<3;$i++)
{
for($a=0;$a<3;$a++)
{
print "$array->[$i]->[$a]/n";
}
}
print "-" x 30,"/n";
$c=/$array;
print "the first element is $$c->[0]->[0]/n";
#-------------------------------
#name:open.pl
#note:print a line which contain "apple"
#!/usr/bin/perl
open(FILE,"test.txt")||die "this file is not exist!";
while($line=<FILE>)
{
print "$line" if $line=~/apple/;
}
close(FILE)
#---------------------------------------
#name:open_pipe.pl
#note:read file description should like this <found>
#!/usr/bin/perl
open(found,"find -name test.txt|");
while(<found>)
{print ;}
close(found);
#---------------------------------------
#script:opendir.pl
#purpose:listing a directory files
#!/usr/bin/perl
opendir(dir,"..")||die "opendir error:$!/n" ;
@file=readdir(dir);
foreach $name (@file)
{
print "$name/n";
}
##########################################################
#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";
#---------------------------------------------
#
#
#!/usr/bin/perl
#use HOME ;
require "home.pm" ;
tie %house,"home","red",6,20000;
print "the house's colour is %house->{color}/n";
%house->{color}="green";
print "the new colour is %house->{color}/n";
#------------------------------------------------------
#name:open_write.pl
#usage:if 1.txt is not exist,creating it and write "hi all"
#!/usr/bin/perl
open(handle,">1.txt");
print handle "hi all";
close(handle);
#----------------------------------------------
#name:package.pl
#note:look care the package range
#!/usr/bin/perl
$name=tom; #main package
package hi;
$name=jim;
print "hi,$name/n";
print "package main /$name:$main::name/n "