perl学习笔记(三)


#!/usr/bin/perl
use strict;
use warnings;
use 5.010;

#say "Hello, World...\n";

for (my $i=1;$i<10 ;${i}++) {
#print "count $i!\n";
}

use File::Basename;
#use File::Basename (); #不引入函数
#use File::Basename qw / /;#不引入函数
#use File::Basename qw /basename/;#只引入basename函数
#use File::Basename;#引入所有函数

my $name = "/usr/bin/perl";
#my $basename = basename $name;
my $dir_name = File::Basename::dirname $name;
say $dir_name;

use File::Spec;
my $myoldname = "高级perl编程.pdf";
my $dirname = File::Basename::dirname $myoldname;
my $basename = File::Basename::basename $myoldname;
my $newname = File::Spec->catfile($dirname,$basename);
say $newname;

#use Path::Class;
#my $dir = dir(qw( Users fred lib ));
#say $dir;
#my $subdir = $dir -> subdir("perls");
#say $subdir;
#my $parent = $dir -> parent;
#say $parent;
#my $windir = $dir -> as_foregin("win32");
#say $windir;

my $filename = "perl_01";
my $filename2 = "perl_02";
my $filesize = (-s $filename) / 1000;
say $filesize;

warn "the file <$filename> already exist.\n"
if -e $filename;#文件是否存在

warn "the file <$filename> have 1 day not change.\n"
if -M $filename2 > 1;#最后一次被修改后至今的天数

warn "thie file <$filename2> size have > 11.\n"
if -s _ > 11; #文件大小,字节表示; -表示虚拟文件句柄,表示用上次查询过的文件来做当前测试

warn "the file <$filename> have 1 day not visit.\n"
if -A $filename > 1; #最后一次被访问后至今的天数

warn "the file <$filename> can read can write.\n"
if -w -r $filename;

my($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime) = stat($filename);
say $size;
say $atime;
my $date = localtime $atime;
say $date;
my($sec,$min,$hour,$day,$mon,$year,$wday,$yday,$isdst) = localtime $atime;
say $year + 1900;#年
say $mon;#0-11 月份
say $wday;#0-6 星期
say $yday;#今天是今年的第几天
my $now = gmtime;
say $now;

chdir '/etc' or warn"cannot chdir to /etc:$!";#改变当前文件目录
chdir;
my @all_files = glob '*'; #文件名通配
say "@all_files";
my @pl_files = glob '*.pl';
say "@pl_files";

use File::Spec::Functions;
my $dir = 'D:\\lucene\\fileDir';
opendir my $files,$dir or warn "cannot open dir $dir $!.\n";
#foreach my $f(readdir $files){
# say "this is $f.";
#}
while(my $name = readdir $files){
next if $name =~ /^\./;
say $name."*****";
$name = catfile($dir,$name);#完整路径
say $name."+++++";
say "the dir $files have file $name.";
}

#unlink glob '*.pl';#删除文件
#unlink qw(file file1 file2);
#重命名文件
rename 'D:\\test\\test\\file\\aaaa.txt' => 'D:\\test\\test\\file\\a1.txt';
my $old_file = 'D:\\test\\test\\file\\a1.new';
my $new_file = $old_file;
$new_file =~ s/\.new$/.old/;#将a1.new 重命名为a1.old
if (-e $new_file) {
say "the file $new_file have exist!";
}elsif(rename $old_file => $new_file){
say "the file $old_file name have change $new_file!";
}else{
warn "the file $old_file rename $new_file fail. $!.";
}

my $tmp_dir = "D:\\test\\test\\file\\aaa_$$";
mkdir $tmp_dir ,0755; #创建目录
my $permissions = "0755";
mkdir $tmp_dir,oct($permissions);#转换成8进制
unlink glob "$tmp_dir/* $tmp_dir/.*";#删除目录中的文件
rmdir $tmp_dir; #删除空目录


$array[0]="hello";
$array[1]="world";
$array[2]="i";
$array[3]="am";
$array[4]="is";
$array[5]="gigg";

print $array[0]."\n";

$array[0]="hello,";

print $array[0]."\n";

$array["a"]="hello_aa"; #array index of "a" will be rest 0

$array[99]="beckham";

print $array[98]."\n";


print $#array."\n"; #array last one index

print $array[$#array]."\n"; #array end element

print $array[-1]."\n"; #array last one index
print $array[-2]."\n"; #array last two index

print $array[-200]."\n"; #array overstep


print "-------list-----------------\n";

@list1 = (1..10);
@list2 = (1.2..7.9);
@list3 = (5..1);
@list4 = ($m..$n);
@list5 = (1..$#array);
@list6 = (1,2,3..10,11,12);
@list7 = ("hello",4.7);
@list8 = (1,2,3,4,@list1);
@list9 = @list2;

($list[0],$list[1],$list[2],$list[3]) = ("giggs","beckham","kean","scholes");
($list[0],$list[1],$list[2],$list[3]) = qw( giggs bekcham kean scholes);

print $list[0]."\n";

#handler array to tail
$tmp = pop(@list1);
print $tmp."\n";
push(@list1,"tail_11");
print @list1;
print "\n";

#handler array to head
@arrays = qw < one two three four five six >;
$del_ele = shift(@arrays);
print $del_ele ."\n";
unshift(@arrays,"head_11");

print "-------splice------------\n";

@arrays = qw < one two three four five six >;
#splice(@arrays,1);
$del_eles = splice(@arrays,1,2);
print $del_eles."\n";

splice(@arrays,1,0,qw("hello"));
print "\@arrays elements is : (@arrays).\n";
print "\n";

@arrays = qw (one two three four five six);
print "this \@arrays[2] is $arrays[2].\n";
$arr = "ten";
print "this is @arrays[3].\n";
print "this is ${arr}[3]\n";
print "this is $arr"."[3]\n";
print "this is $arr\[3]\n";

print "---foreach-------"."\n";
foreach $item(qw< java c++ c c# python ruby perl >){
print "this is : $item.\n";
}

@rocks = qw / c# f# php /;
foreach $item(@rocks){
#item is @rocks elements
#$item = "\t$item";
$item = "\titem";
$item .="\n";
}
print "this \@rocks element is :\n",@rocks;

$items = qw / apple sanxing xiaomi /;
$item = "songxia";
foreach $item($items){

}
print "the \$item still is $item.\n";

#default variable is "$_"
foreach(1..10){
print "this is $_ .\n";
}

@array = (1..5);
@rev = reverse(@array);
print @rev;
print "\n";

@array = qw( one two three apple);
@sort = sort(@array);
foreach (@sort){
print "sort.this is:$_ .\n";
}
print "\n";

@number = sort(97..102);
print @number;
print "\n";

#use 5.012;
#my @rocks = qw( one two three );
#while(my($index,$value)=each @rocks){
# say "$index:$value";
#}

@rocks = qw ( one two three four five );
foreach $index(0..$#rocks){
print "$index:$rocks[$index]\n";
}

print "---------context----------------\n";

@array = undef; #error clear array
print @array."\n";
@array = (); #clear array
print @array."\n";

@array = qw( one two three four five six );
print "this is have ", @array ," elements!\n";
print "this is have ",scalar @array, " elements!\n";

@array = reverse qw( a1 b2 c3 d4 e5 );
print @array,"\n";
$array = reverse qw (a1 b2 c3 d4 e5);
print $array,"\n";

print "--------------example------------------\n";

#--first example--
#@list = <STDIN>;
#chomp(@list);
#@rev = reverse @list;
#foreach(@rev){
# print "current element is :$_.\n";
#}

#--second example----
#@list = qw(beckham giggs kean scholes);
#@recv = <STDIN>;
#chomp(@recv);
#foreach $index(@recv){
# print "the $index element is @list[$index-1]:\n";
#}

#---three example--------------

@recv = <STDIN>;
chomp(@recv);
print "the result is :\n";
foreach(sort @recv){
#print "the element is $_\n";
print "$_\t";
}
print "\n";
foreach(sort @recv){
print "$_\n";
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值