Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Appending to variable with a switch statement?

by Spidy (Chaplain)
on Jan 06, 2007 at 21:20 UTC ( #593335=perlquestion: print w/ replies, xml ) Need Help??
Spidy has asked for the wisdom of the Perl Monks concerning the following question:

Greetings, Fellow Monks.

I'm working on something that involves using a switch() statement to call different subroutines based on the values inside a hash. Here is what the code looks like:

use Switch; switch(%A) { case 'sloven' { $responseXML .= sloven('xml') } case 'leggert' { $responseXML .= wiggedy() } ... }

...And so on. This is all well and good, but in every single case, I'm appending to $responseXML. In keeping with the Perlish quality of laziness, is there a way to append to $responseXML in every case, without having to type it every time?

Thanks,
Spidy

Comment on Appending to variable with a switch statement?
Download Code
Re: Appending to variable with a switch statement?
by diotalevi (Canon) on Jan 06, 2007 at 21:26 UTC

    There is no perlish switch statement. If you're using Switch then you're asking for a house to drop onto your head.

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Re: Appending to variable with a switch statement?
by Joost (Canon) on Jan 06, 2007 at 21:26 UTC
Re: Appending to variable with a switch statement?
by jbert (Priest) on Jan 06, 2007 at 21:39 UTC
    As noted elsewhere, there isn't really a switch statement in perl.

    You can get a nice effect though - and also answer your question about removing the repeated bit of code - by using a dispatch table. Perl has the nice feature of anonymous subroutines/closures, which allow you to write a snippet of code in the middle of another function. These snippets are scalars (since they are references) and so can be stored anywhere you can normally store a scalar, in particular as a hash value.

    So you can do things like:

    my %dispatch_table = ( sloven => sub { return sloven('xml'); }, badger => sub { return badger2xml(); }, ); my $case = 'sloven'; my $subref = $dispatch_table{$case}; $responseXML .= $subref->();
    Your code snippets can take arguments (which you'd put in place of the dots in the ->(...) call) and access them in the normal with via @_. They can also do anything code can do (including random sideeffects) rather than just returning a value as above.

    All in all, a cool and useful technique.

Re: Appending to variable with a switch statement?
by jhourcle (Prior) on Jan 06, 2007 at 21:40 UTC
    is there a way to append to $responseXML in every case, without having to type it every time?

    You may wish to use a construct that's composed of '? :', which avoids the use of 'Switch', but I'm not sure if it's more lazy, as it requires some extra typing:

    $responseXML .= ( $_ eq 'sloven' ) ? ( sloven('xml') ) : ( $_ eq ... ) ? ( ... ) : ( ... test ... ) ? ( ... then ... ) : ( final 'else' value )

    Without seeing more of what you're trying to do, I'd think you should probably look at using a lookup table. (see super search for more details on lookup tables, if you're not familiar with them)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2014-09-20 19:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (160 votes), past polls