in reply to Best way to handle interactive user input?

hello Ppeoc,

your question is not very clear, at least to me: XML.. menu.. and the code you put at the bottom.
If I understand you vaguely describe a problem like: i have an XML file, i want let the user to navigate some menu based on the content of the XML file.

If so you have some billion of different possibility to do it in Perl. It depends on your skills and your taste. I would do something like:

1-choose a good XML parser: i always suggest XML::Twig but there are others (never fall in the XML::Simple pitfall)

2-parse the XML and build up a nested datastructure (an HashOfHashes) with a leaf for each menu you want displyed, like
#PSEUDO EXAMPLE menu_mustsee_movies => { descr=>'must see movies', previous=>'menu_good_movies', entries=>\@data_from_xml, }
Then 3-choose your user interface, a GUI like Tk or simply Term::Readline

4-build up a loop where you present the top_level_menu, grab the input and display the choosed menu.

If you interested there is a bit aged but working Term::Menu module and also see my own example of Term::Readline in response to my own question about Term::ReadLine and it's imported function

HtH
L*
There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Replies are listed 'Best First'.
Re^2: Best way to handle interactive user input?
by Ppeoc (Beadle) on Nov 26, 2015 at 08:30 UTC
    My XML parser code
    my $twig = XML::Twig->new( twig_handlers => { 'Mughals' => sub { + my ($twig, $el) = @_; + $twig->purge; + }, 'Mughals//*' => sub { + my $a = $_->tag; + if ($a eq "Book") + { + print $fout1 "\n"; + print $fout2 "\n"; + } #print $fout1 $_->tag, ", ", $_->text, unless ($_->has_ +children('#ELT')); print $fout2 $_->tag, ",", unless ($_->has_children('#E +LT')); print $fout1 $_->text, ",", unless ($_->has_children('# +ELT')); } } ); $twig->parsefile('book.xml');
    This is a part of the code for parsing one part of the file 'Mughals'. The path to Mughals is History->Indian History -> Mughals. I am confused about how to accept this input from the user.
Re^2: Best way to handle interactive user input?
by Ppeoc (Beadle) on Nov 26, 2015 at 08:23 UTC
    Hey! Sorry about being so vague. I hope this makes more sense. So I have used XML::Twig to parse the whole file and it took 70 secs for one file. And I have to parse many such files at one go. So instead I thought I could just display the options to parse and parse only that particular section. But like you said, it may be a good idea to display leaf for each menu and then do step 3 and 4.
      if you have big big files you can enjoy the ability of XML::Twig to parse on the fly using twig_handler if you are smart enough you can build an handler that does everything you need as grab data and display the menu (the twig knows his father and his silbings and his children too). That said i was no able to navigate interactively an XML when i tried to do so. It would be simpler to put all the data in minimal database (SQLite)

      L*
      There are no rules, there are no thumbs..
      Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.