Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Re: passing arguments in a dispatch table

by CharlesClarkson (Curate)
on Apr 29, 2002 at 02:45 UTC ( #162750=note: print w/ replies, xml ) Need Help??


in reply to Re: passing arguments in a dispatch table
in thread passing arguments in a dispatch table

Frankly, I think I need an object, but I'm reluctant to use one or a closure. I am introducing a dispatch table as a new concept on a perl beginners mailing list (PBML on Yahoo!). The original script used @book as a global variable. I changed it to $friut for the example above.

I haven't added the response yet. I do a ton of editing before replying, so I'm still able to scrap this approach.

    Currently, I'm here:
sub process { my ( $choice, $book ) = @_; my $dispatch = { 1 => \&search_menu, 2 => sub { return \&edit_name($book) }, 3 => sub { return add_entry($book) }, 4 => sub { return delete_entry($book) }, 5 => \&save_default, 6 => \sub{ print "Goodbye!\n"; die; }, e => \sub{ print "Goodbye!\n"; die; }, }; print &{ $dispatch->{ $choice } }; }

It's a direct move from a group of if - elsif blocks that did the same thing. That's why I'd like to call the subs the same way now. I can introduce better concepts in later posts. I consider this an intermediate approach. Later, $dispatch will be passed to process()as well (which is why I use a hash reference instead of a hash). To help with processing the search menu. $book is reference to @book initally taken from a flat file. memoize immediately comes to mind, but too many beginners shun modules and I don't want to lose my audience.

I initially scrapped an idea of using a BEGIN block. Perhaps I could combine the two and grab the array reference in each subroutine as someone else mentioned. Though I find using the dispatch table as a source for the arguments that are passed more powerful.

BEGIN { open FH, 'my_db' or die "Cannot open my_db: $!"; my @book = split /,/, <FH>; sub address_book { return \@book } }

HTH,
Charles K. Clarkson
Clarkson Energy Homes, Inc.


Comment on Re: Re: passing arguments in a dispatch table
Select or Download Code
Re: Re: Re: passing arguments in a dispatch table
by abstracts (Hermit) on Apr 29, 2002 at 03:12 UTC
    You say "Frankly, I think I need an object, but I'm reluctant to use one or a closure.", but you're using closures in your code segment above.

    You might want to consider this approach:

    my $dispatch = { 1 => sub { search_menu() }, 2 => sub { my $book = shift; edit_name($book) }, 3 => sub { my $book = shift; add_entry($book) }, 4 => sub { my $book = shift; delete_entry($book) }, 5 => sub { save_default() }, 6 => sub { print "Goodbye!\n"; exit }, e => sub { print "Goodbye!\n"; exit } }; sub process { my ( $choice, $book ) = @_; print $dispatch->{$choice}->($book); }
    Here, we're removing the closure completely, and passing the args directly to the appropriate sub. Did I completely miss what you're trying to do?

      I have two problems with this aproach

      1. $dispatch is not passed to process, making it a global variable. Just what I'd like to get my readers to avoid. Changing the code to pass $dispatch skips a step. As I said: "Later, $dispatch will be passed to process() as well" [emphasis added]. Your getting ahead of the lesson. (Stop being a teacher's pet.)

      2. You're not passing the args directly to the appropriate sub. You're passing the arguments to every subroutine. Let me illustrate the point with a different example.

        my $dispatch = { 1 => sub { return search_menu($current_user) }, 2 => sub { return edit_name($book) }, 3 => sub { return add_entry($book) }, 4 => sub { return delete_entry($book) }, 5 => sub { return save_default($current_user) }, 6 => \sub{ print "Goodbye!\n"; die; }, e => \sub{ print "Goodbye!\n"; die; }, }; &{ $dispatch->{ $choice } };

        Here, the arguments are defined in the dispatch table. I would like to help my readers think in broad terms instead of solving just this one problem. Your solution works fine, but more than 1400 people view these posts and most don't ask questions. My goal is to reach a wide audience with code examples and explanations that can be applied to other problems as well.

      It would seem I'm better at anwering questions than at asking them. Thank you for your reply.


      HTH,
      Charles K. Clarkson
      Clarkson Energy Homes, Inc.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://162750]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2014-12-20 15:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (96 votes), past polls