perl笔记

Perl语言擅长做文本处理,文本其实就是程序中的字符串,一下是关于perl处理字符串的总结.

 

【注意事项】

1、if(<STDIN>)时,在终端输入,数据不会自动存储在$_中;while(<STDIN>)时,在终端输入,数据会自动存储在$_中。

【perl查看字符串ASCII值】

#! /usr/bin/perl -w

#

my $str;

print "$#ARGV,$ARGV[0]\n";

if($#ARGV eq 0){

       while(<>)

       {

                $str .= $_;

       }

       print $str;

}else{

       print "input string\n";

       $str = <STDIN>;

}

print "input char_index\n";

while(<>){

       if($_>0){

               $ch = substr $str, ($_-1),1;

                $num = ord($ch);

                print "OUT:[$ch =>$num]\n";

       }else{

                print "index must>0.\n";

       }

}

【杀死vs进程】

#! /usr/bin/perl -w

#

 

my $str = `ps -e | grep vs.*`;

#print $str;

my @a;

while($str =~ m/([0-9]{4,})\b/g){

       push @a,$1;

}

 

 

 

 

print "all:\n@a\n";

 

foreach(@a){

       system "kill -9 $_";

}

 

【查找指定目录下的所有文件和目录及总大小】

#!/usr/bin/perl -W

#

# File: lsr_s.pl

# Author: 路小佳

# License: GPL-2

 

use strict;

use warnings;

 

sub lsr_s($) {

    my $cwd =shift;

    my @dirs= ($cwd.'/');

 

    my ($dir,$file);

    while($dir = pop(@dirs)) {

        local*DH;

        if(!opendir(DH, $dir)) {

           warn "Cannot opendir $dir: $! $^E";

           next;

        }

        foreach (readdir(DH)) {

           if ($_ eq '.' || $_ eq '..') {

               next;

            }

           $file = $dir.$_;        

           if (!-l $file && -d _) {

               $file .= '/';

               push(@dirs, $file);

            }

           process($file, $dir);

        }

       closedir(DH);

    }

}

 

my ($size, $dircnt, $filecnt) = (0, 0, 0);

 

sub process($$) {

    my $file= shift;

    print$file, "\n";

    if(substr($file, length($file)-1, 1) eq '/') {

       $dircnt++;

    }

    else {

       $filecnt++;

        $size+= -s $file;

    }

}

 

lsr_s('.');

print "$filecnt files, $dircnt directory.$size bytes.\n";

 

【命令行参数关键字】

@ARGV 是你的命令行输入参数数组

$#ARGV 是你的参数个数

$ARGV 是你用<>读取文件时,当前的那个文件名称

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值