#!/usr/bin/perl # # Filename: NoteBox.pl # Author: Sven Eisenhauer # Systemprogrammierung in Perl Wintersemester 2006 / 2007 fbi, h_da # Praktikum ################################################# # changelog: # - 2006-10-30 Sven Eisenhauer # Initial coding ################################################# # # Includes # use warnings; use strict; ################################################# # # Defines # ################################################# # # Globals # my @notes; ################################################# # # Subroutines # # print the main menu sub print_menu { my $menu = qq{NoteBox ----------------------------------- e - New Note a - Append text to Note nr d - Delete Note nr l - Show note nr l - Show all notes h - Help s - Search text in all Notes q - Quit ----------------------------------- Your selection: }; #clear console print chr(27)."[2J"; print $menu; return 1; } # new_note sub new_note{ my $act_line; my $note; #clear console print chr(27)."[2J"; print "Please enter your note (single line starting with'.' is end of note):\n"; until ( (($act_line = ) =~/^\./) ) { $note.=$act_line; } push @notes,$note; return 1; } # append_note sub append_note{ if (defined $_[0] && $_[0] < scalar @notes) { my ( $nr ) = @_; my $act_line; my $note; #clear console print chr(27)."[2J"; print "Please enter your note (single line starting with'.' is end of note):\n"; chomp ($act_line = ); while ( ! ($act_line =~/^\./) ) { $note.=$act_line."\n"; chomp ($act_line = ); } $notes[$nr].=$note; } else { print "No Note appended\n"; } print "Press ENTER to continue\n"; my $dummy = ; return 1; } # delete_note sub delete_note{ if (defined $_[0] && $_[0] < scalar @notes) { my ( $nr ) = @_; splice @notes,$nr,1; print "Note $nr deleted\n"; } else { print "No Note deleted\n"; } print "Press ENTER to continue\n"; my $dummy = ; return 1; } # list_note sub list_note{ my $show_break=1; if (defined $_[0] && $_[0] < scalar @notes) { if (defined $_[1]) { $show_break=0; } my ( $nr ) = @_; print "Note $nr:\n"; print $notes[$nr]; print "---------------------------------\n"; } else { my $i=0; print "All Notes\n"; print "===============================\n"; foreach(@notes) { print "Note $i:\n"; print $_; print "---------------------------------\n"; $i++; } $show_break=1; } if ( $show_break != 0) { print "Press ENTER to continue\n"; my $dummy = ; } return 1; } # help_message sub help_message{ print "Help Message\n"; print "Press ENTER to continue\n"; my $dummy = ; } # search sub search{ if (defined $_[0]) { my $i=0; my ( $searchstring ) = @_; foreach(@notes) { if ($_ =~/$searchstring/) { list_note($i,1); } $i++; } } else { print "No search string specified\n"; } print "Press ENTER to continue\n"; my $dummy = ; return 1; } # quit sub quit { exit; } ################################################# # # main routine # ################################################# my $menu_selection; my $continue=1; my %menu_switch=( 'e' =>\&new_note, 'a' => \&append_note, 'd' => \&delete_note, 'l' => \&list_note, 'h' => \&help_message, 's' => \&search, 'q' => \&quit ); do { print_menu; $menu_selection = ; chomp ($menu_selection); my @input = split /\s+/,$menu_selection; if (length $menu_selection > 0) { if (exists $menu_switch{$input[0]}) { if (exists $input[1]) { &{ $menu_switch{$input[0]}} ($input[1]); } else { &{ $menu_switch{$input[0]}}; } } } } while ($continue!=0); exit 0;