#!/usr/bin/perl
use strict;
use Getopt::Long;
use Pod::Usage;
use File::Copy;
use Data::Dumper;
use Cwd;
use DBI;
use Encode;
package Person;
use overload '0+' => \&age, # convert type to integer
'""' => \&name, # convert type to string
fallback => 1; # make ($p - $a) possible
sub new {
my ($this, $name, $age) = @_;
my $class = ref($this) || $this;
my $self = { name => $name, age => $age };
bless $self, $class;
return $self;
}
sub name {
my $self = shift;
return $self->{name};
}
sub age {
my $self = shift;
return $self->{age};
}
sub log10 {
my ($n) = @_;
print "PP10=$n\n";
return log($n)/log(10);
}
sub log100 {
my $n = shift;
print "PP100=$n\n";
return log($n)/log(10);
}
package Clock;
my $conversion={
'seconds' => 1,
'minutes' => 60,
'hours' => 3600,
'days' => 86400,
'years' => 31557600
};
sub new
{
my ($type) = @_;
my $self = {};
$self->{time} = time();
bless $self, $type;
}
sub get
{
my ($self, $type) = @_;
$type ||='seconds';
(print ("Unkown type $type!\n"), return (undef))
if (!defined $conversion->{$type});
return (int($self->{time}/$conversion->{$type}));
}
sub set
{
my ($self, $secs) = @_;
$self->{time} = $secs || time();
return (1);
}
#####################################################
package main;
sub log100 {
my $n = shift;
print "NN100=$n\n";
return log($n)/log(10);
}
sub log1000 {
my($n) = @_;
print "NN1000=$n\n";
return log($n)/log(10);
}
my $p = Person->new("Old Brother", 20);
print $p->name, " is ", $p->age, " years old.\n";
$a = Person->new("Young Sister", 8);
print $a->name, " is ", $a->age, " years old.\n";
print $p, " is ", $p - $a, " year older than ", $a, ".\n";
print $p->log10(1000)."\n";
print $p->log100(1000)."\n";
#return::不是所想要的 参数n=
#1.30102999566398
print log100(1000)."\n";
print log1000(1000)."\n";
my $c = new Clock();
print "now time is $c->get('hours')\n";
#return::
#now time is Clock=HASH(0x250770c)->get('hours')
###########################################
my @a = ("hello", "World");
my @c = ("Jack");
unshift(@c,"hello");
print Dumper(@c)."\n";
unshift(@c,("hello","halloha"));
print Dumper(@c)."\n";
unshift(@c,@a);
print Dumper(@c)."\n";
my $first = shift(@c);
print $first."\n";
my $last = pop(@c);
print $last."\n";
###############################################
my @d=(1..9);
print ";
my @e=("a".."f");
print ";
splice(@d,2,3,@e);
print ";
#del
splice(@d,2,6);
print ";
阅读(1575) | 评论(0) | 转发(0) |