#!/usr/bin/perl -w # Simple notes script. use XML::Simple; print "\nWelcome to jot\n"; print "Type 'h' for help.\n"; do { print "%> "; my $command = ; chomp($command); $command = short($command); &q() if ($command =~ m/^q/); &{$command}(); } until $command eq 'q'; sub a { # Add to a file while (<*.idea>) { print $_,"\n"; } print "Name of file to edit : "; my $cfile = ; chomp($cfile); if (! -e $cfile) { warn "jot : $cfile doesn't exist\n"; } else { my $simple = XML::Simple->new(); $cdata = $simple->XMLin($cfile, searchpath => "."); print "Project name(s) : ", $cdata->{name}; my $cname = ; chomp($cname); print "Description : ", $cdata->{desc}; my $cdesc = ; chomp($cdesc); my $oname = $cdata->{name}; # get all the old to concat with new my $odesc = $cdata->{desc}; %cdata = (name => "$oname$cname", desc => "$odesc$cdesc"); my $xml = $simple->XMLout(\%cdata, noattr => 1, rootname => "idea", outputfile => "$cfile"); } } sub b { # Backup all idea files while (my $cfile = <*.idea>) { # read, output to $cfile.idea~ my $simple = XML::Simple->new(); # this is slow. better way? my $cdata = $simple->XMLin($cfile, searchpath => '.'); my $xml = $simple->XMLout($cdata, noattr => 1, rootname => "idea", outputfile => "$cfile~"); print "Backup of $cfile unsuccesfull : $!\n" if $!; } } sub d { # Delete an idea file while (<*.idea>) { print $_,"\n"; } print "File to delete : "; my $cfile = ; chomp($cfile); unlink($cfile); } sub h { print "a - Add info to a project\n"; print "b - Backup all project files\n"; print "d - Delete a project file\n"; print "h - Display this message\n"; print "l - List all projects\n"; print "n - New project\n"; print "q - Quit\n"; print "r - Restore project files\n"; } sub l { # List all idea files and their contents in quasi-nicely formatted way while (my $cfile = <*.idea>) { my $simple = XML::Simple->new(); print "\n$cfile\n"; my $idea = $simple->XMLin($cfile, searchpath => '.'); print "\t", $idea->{name}, "\n\t", $idea->{desc}, "\n\t"; } } sub n { # New project file my $simple = XML::Simple->new(); print "Project name(s) : "; my $cname = ; chomp($cname); print "Description : "; my $cdesc = ; chomp($cdesc); my $ofile = short($cname); my %pdata = (name => $cname, desc => $cdesc,); my $xml = $simple->XMLout(\%pdata, noattr => 1, rootname => "idea", outputfile => "$ofile.idea"); } sub q { print "bye\n"; exit; } sub r { # Restore backup files my $simple = XML::Simple->new(); while (my $cfile = <*.idea~>) { my $ofile = $cfile; chop($ofile); # remove the ~ my $cdata = $simple->XMLin($cfile, searchpath => "."); my $xml = $simple->XMLout($cdata, noattr => 1, rootname => "idea", outputfile => $ofile); print "Restoration of $cfile unsuccesfull : $!\n" if $!; } } sub short { (my $word) = @_; $word =~ s/\W.*//; $word =~ tr/A-Z/a-z/; return $word; }