in reply to Error with User defined data
Having solved the chomp problem, you also may like to implement a dispatch table for different variants of your storage class. Code written this way looks cleaner, has less repetitions and places to make a mistake. Example code:
my %classes = ( D => sub { code_to_handle_dynamic(); ...; }, NV => sub { handle_non_volatile(); ...; }, ); chomp (my $answer = <STDIN>); die "Invalid answer!\n" unless exists $classes{$answer}; $classes{$answer}->();
Going one step further, you can use the IO::Prompter module to handle chomping and validation.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Error with User defined data
by Anonymous Monk on Aug 13, 2014 at 11:40 UTC | |
by roboticus (Chancellor) on Aug 13, 2014 at 12:27 UTC | |
by aitap (Curate) on Aug 13, 2014 at 12:31 UTC |