0: #!/usr/bin/perl -w
1: # Simple notes script.
2:
3: use XML::Simple;
4:
5: print "\nWelcome to jot\n";
6: print "Type 'h' for help.\n";
7: do {
8: print "%> ";
9: my $command = <STDIN>;
10: chomp($command);
11: $command = short($command);
12: &q() if ($command =~ m/^q/);
13: &{$command}();
14: } until $command eq 'q';
15:
16: sub a {
17: # Add to a file
18:
19: while (<*.idea>) {
20: print $_,"\n";
21: }
22: print "Name of file to edit : ";
23: my $cfile = <STDIN>;
24: chomp($cfile);
25: if (! -e $cfile) {
26: warn "jot : $cfile doesn't exist\n";
27: } else {
28: my $simple = XML::Simple->new();
29:
30: $cdata = $simple->XMLin($cfile, searchpath => ".");
31:
32: print "Project name(s) : ", $cdata->{name};
33: my $cname = <STDIN>;
34: chomp($cname);
35:
36: print "Description : ", $cdata->{desc};
37: my $cdesc = <STDIN>;
38: chomp($cdesc);
39:
40: my $oname = $cdata->{name}; # get all the old to concat with new
41: my $odesc = $cdata->{desc};
42:
43: %cdata = (name => "$oname$cname",
44: desc => "$odesc$cdesc");
45:
46: my $xml = $simple->XMLout(\%cdata, noattr => 1, rootname => "idea", outputfile => "$cfile");
47: }
48: }
49:
50: sub b {
51: # Backup all idea files
52:
53: while (my $cfile = <*.idea>) {
54: # read, output to $cfile.idea~
55: my $simple = XML::Simple->new(); # this is slow. better way?
56: my $cdata = $simple->XMLin($cfile, searchpath => '.');
57: my $xml = $simple->XMLout($cdata, noattr => 1, rootname => "idea", outputfile => "$cfile~");
58: print "Backup of $cfile unsuccesfull : $!\n" if $!;
59: }
60: }
61:
62: sub d {
63: # Delete an idea file
64:
65: while (<*.idea>) {
66: print $_,"\n";
67: }
68: print "File to delete : ";
69: my $cfile = <STDIN>;
70: chomp($cfile);
71: unlink($cfile);
72: }
73:
74: sub h {
75: print "a - Add info to a project\n";
76: print "b - Backup all project files\n";
77: print "d - Delete a project file\n";
78: print "h - Display this message\n";
79: print "l - List all projects\n";
80: print "n - New project\n";
81: print "q - Quit\n";
82: print "r - Restore project files\n";
83: }
84:
85: sub l {
86: # List all idea files and their contents in quasi-nicely formatted way
87:
88: while (my $cfile = <*.idea>) {
89: my $simple = XML::Simple->new();
90: print "\n$cfile\n";
91: my $idea = $simple->XMLin($cfile, searchpath => '.');
92: print "\t", $idea->{name}, "\n\t", $idea->{desc}, "\n\t";
93: }
94: }
95:
96: sub n {
97: # New project file
98:
99: my $simple = XML::Simple->new();
100: print "Project name(s) : ";
101: my $cname = <STDIN>;
102: chomp($cname);
103: print "Description : ";
104: my $cdesc = <STDIN>;
105: chomp($cdesc);
106:
107: my $ofile = short($cname);
108:
109: my %pdata = (name => $cname,
110: desc => $cdesc,);
111:
112: my $xml = $simple->XMLout(\%pdata, noattr => 1, rootname => "idea", outputfile => "$ofile.idea");
113: }
114:
115: sub q {
116: print "bye\n";
117: exit;
118: }
119:
120: sub r {
121: # Restore backup files
122:
123: my $simple = XML::Simple->new();
124: while (my $cfile = <*.idea~>) {
125: my $ofile = $cfile;
126: chop($ofile); # remove the ~
127:
128: my $cdata = $simple->XMLin($cfile, searchpath => ".");
129: my $xml = $simple->XMLout($cdata, noattr => 1, rootname => "idea", outputfile => $ofile);
130:
131: print "Restoration of $cfile unsuccesfull : $!\n" if $!;
132: }
133: }
134:
135: sub short {
136: (my $word) = @_;
137: $word =~ s/\W.*//;
138: $word =~ tr/A-Z/a-z/;
139: return $word;
140: }
In reply to jot
by negravulo
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.