最近在学习perl试着试着写了一个todolist的程序写入一个文本文件,可用于我的conky在桌面显示比较方便写的比较糟和冗余后期会随着学习继续优化脚本。其中遇见了不少问题,也发现很多语法记得不牢靠,幸好以前写过不少其他语言的程序发现问题也只是语法,思维方向大致还是很正确的。

实现功能如下:

1、插入,可同时插入多条。

2、删除单条(

在下一个版本中会加入删除多条可以实现调用方法如下:

输入 1 2 3 是删除指定条数

输入1-3删除范围条数

3、修改,我这里想的是如果是修改那么todo就是需求更改,那么会直接放入todolist的第一条置顶。

用到的知识点

1、列表list

2、hash数组

3、文件的写入与追加

4、sub参数,sub参数的指针调用我用红色标出。

5、脚本调用时的参数判断,个数判断

6、正则

#!/usr/bin/perl

use strict;

my $filename =”$ENV{PWD}/todolist.file”;

sub badarg{

print "badargs";

}

sub dump_arr{

my $line_num = 0;

if(@_ == 0){

    print "You have nothing todo!!!!n";

    exit 0;

}

foreach my $line (@_){

    print "$line_num: $line";

    $line_num++;

}

}

########read only file begin######

sub readfile{

open(IN, "< $filename") or die "Couldn't open $filename for reading: $!";

#close(IN);

return ;

}

########read only file end######

#

########Append line open file begin######

sub appendline{

my @new\_todo = @\_;

print "Below lines are your inputn";

foreach my $s (@new_todo){

    print "$sn";

}

print "Are you sure appending ablow\[yes\]n";

#my $is_append = ;

#chomp($is_append);

my $is_append = "yes";

if($is_append eq "yes"){

    print "Now append to your todolistn";

    open(FO, ">> $filename") or die "Couldn't open $filename for appending: $!";

    foreach my $s (@new_todo){

        print FO "$sn";

    }

    close (FO);

}else{

    print "You are quitn";

}

}

########Append line open file end######

#

########inster file begin######

sub inster{

my $_;

my @new_todo;

my $s;

print "typing \[exit\] for exitn";

while(){;

    chomp;

    push @new\_todo,$\_;

    if($_ eq "exit"){

        pop(@new_todo);

        last;

    }

}

$_ = ;

chomp;

push @new\_todo,$\_;

&appendline(@new_todo);

}

########inster file end######

#

########modify file begin######

sub modify{

my $_;

my @file_lines = &readfile;

my $count\_lines = @file\_lines;

&dump\_arr(@file\_lines);

print "There are $count_lines lines,you can choose one to MODIFY:n";

print "And it will be the FIRST ONE in your todolist: ";

my $n = ;

chomp($n);

print "wrong line is: $file_lines\[$n\]";

chomp($file_lines\[$n\]);

unless (&delete(@file_lines,$n)){

    print "Delete item ERROR!n";

    exit 0;

};

print "You wants change it to:";

my $changeto = ;

chomp($changeto);

push my @new\_file\_lines,$changeto;

print "now newline will be append!==> @new\_file\_linesn";

&appendline(@new\_file\_lines);

}

########modify file end######

#########read only file begin######

sub list{

my @file_lines = reverse(&readfile);

my $line;

my $num=0;

&dump\_arr(@file\_lines);

#foreach $line (@file_lines){

#    print "$num: $line";

#    $num++;

#}

}

########read only file end######

#

########delete file begin######

sub delete{

my ($file\_lines,$n) = @\_;

delete @$file_lines\[$$n\];

#print "this is @file\_lines==>n", @file\_lines;

#print "this is $n ==>$$n<==n";

reverse(@$file_lines);#sort old => new

open(FO, "> $filename") or die "Couldn't open $filename for appending: $!";

foreach my $s (@$file_lines){

   print FO "$s";

}

close (FO);

}

########delete file end######

my $count_arg = $#ARGV + 1;

if ( $count_arg >1 ){

print "your args are: @ARGVn";

exit 1;

}

if ($ARGV[0] =~ s/-|–//){

$_ = $ARGV\[0\];

}

if (/i|inster/i){

&inster;

}elsif(/m|modify/i){

&modify;

}elsif(/l|list/i){

&list;

}elsif(/d|(delete)/i){

my @file_lines = &readfile;

my $count\_lines = @file\_lines;

&dump\_arr(@file\_lines);

print "There are $count_lines lines,you can choose one to DELETE:n";

print "defaut:DELETE oldest onen";

my $n = ;

chomp($n);

&delete(@file_lines,$n);

}else{

print "USAGE:todolist \[-i|--inster\] \[-m|--modify\] \[-l|--list\]n";

}