Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > formatting a number of elsif statements

Reply
Thread Tools

formatting a number of elsif statements

 
 
ccc31807
Guest
Posts: n/a
 
      07-05-2009
Using 5.8 in a web app, I have about 18 elsif statements which look
similar to this:

elsif ($action eq 'AddAttorney')
{
print qq(<h4>Add Attorney</h4>);
HTML::add_professional($oprid, $role, 'Attorney');
HTML:rint_button('setup.cgi','Return To Setup',$oprid, $role);
}
elsif($action eq 'AddInsurer')
....

I now fact the prospect of adding some more, hopefully not many, but
I'm getting tired of looking at all of this.

Is there any way to mimic the case/switch statement? Ideally, I'd like
to have just the matching term for each block, like this:

'AddAttorney'
print qq(<h4>Add Attorney</h4>);
HTML::add_professional($oprid, $role, 'Attorney');
HTML:rint_button('setup.cgi','Return To Setup',$oprid, $role);
'AddInsurer'
...

It's not critical, but I'm just getting tired of looking at all the
repetition.

CC.

 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      07-05-2009
ccc31807 <(E-Mail Removed)> wrote:
>Using 5.8 in a web app, I have about 18 elsif statements which look
>similar to this:
>
>elsif ($action eq 'AddAttorney')
>{
> print qq(<h4>Add Attorney</h4>);
> HTML::add_professional($oprid, $role, 'Attorney');
> HTML:rint_button('setup.cgi','Return To Setup',$oprid, $role);
>}
>elsif($action eq 'AddInsurer')
>...
>
>I now fact the prospect of adding some more, hopefully not many, but
>I'm getting tired of looking at all of this.

[...]
>It's not critical, but I'm just getting tired of looking at all the
>repetition.


Yeah, repetition is what computers are good at.

Step 1: identify which pieces of your repetitions are constant and which
pieces change. I can only guess but it appears like

print qq(<h4>Add #####</h4>);
HTML::add_professional($oprid, $role, '#####');
HTML:rint_button('setup.cgi','Return To Setup',$oprid, $role);

is the constant part with the variable/changing part blocked out by
#####.

Step 2: Find a way to compute the variable parts from a suitable
parameter. In this case you got $action as the parameter and you want to
map it into a simple string. A hash is an ideal and very simple
datastructure for that.

my %param= {'AddAttorney' -> 'Attorney',
'AddInsurer' -> 'Insurer',
.....
}

Step 3: Replace the placeholder for the variable part ##### with actual
parameterized code that retrieves the variable part at runtime:

print qq(<h4>Add $param{$action}</h4>);
HTML::add_professional($oprid, $role, $param{$action});
HTML:rint_button('setup.cgi','Return To Setup',$oprid, $role);

That should do it.

jue


 
Reply With Quote
 
 
 
 
ccc31807
Guest
Posts: n/a
 
      07-05-2009
Thanks, Ben.

I went ahead and bit the bullet.

I wrote a function that takes, among other arguments, an entity type
and an action type, so now I just call one procedure. It did require a
translation table as you suggested (which I hard coded for now but
will probably put into a database), but that's a lot easier on the
eyes than a couple hundred lines of elsif statements.

As a bonus, I am able to abstract my SQL as well. In my code, I have
procedures to:
- add
- insert
- view all
- view one
- edit
- update
- delete

for a dozen different categories, Insurers, Employers, Attorneys,
Agents, etc. It started out pretty small, but man, scaling becomes an
issue when different people start adding their wants to the list!
Saying, 'Yes, I can do a little app to track the players,' turns into
a Sunday at the keyboard when the office gets a crack at it.

On Jul 5, 4:00*pm, Ben Morrow <(E-Mail Removed)> wrote:
> That's a good suggestion. Another might be to use a hash table of
> subrefs, like
>
> * * my %dispatch = (
> * * * * AddAttorney => sub {
> * * * * * * print qq(<h4>Add Attorney</h4>);
> * * * * * * HTML::add_professional($oprid, $role, 'Attorney');
> * * * * * * HTML:rint_button('setup.cgi','Return To Setup',
> * * * * * * * * $oprid, $role);
> * * * * },
> * * * * AddInsurer => sub { ... },
> * * );
>
> * * $dispatch{$action}();
>
> or even a class with a method for each action.


One of these days, I'm going to learn OO Perl. I've got two books on
my reading list, 'Object Oriented Perl' and 'Higher Order Perl', but
I've never been able timewise to do more than just browse through
them.

Thanks for your suggestion.

CC
 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      07-06-2009
On Sun, 5 Jul 2009 12:07:41 -0700 (PDT), ccc31807 <(E-Mail Removed)> wrote:

>Using 5.8 in a web app, I have about 18 elsif statements which look
>similar to this:
>
>elsif ($action eq 'AddAttorney')
>{
> print qq(<h4>Add Attorney</h4>);
> HTML::add_professional($oprid, $role, 'Attorney');
> HTML:rint_button('setup.cgi','Return To Setup',$oprid, $role);
>}
>elsif($action eq 'AddInsurer')
>...
>
>I now fact the prospect of adding some more, hopefully not many, but
>I'm getting tired of looking at all of this.
>
>Is there any way to mimic the case/switch statement? Ideally, I'd like
>to have just the matching term for each block, like this:
>
>'AddAttorney'
> print qq(<h4>Add Attorney</h4>);
> HTML::add_professional($oprid, $role, 'Attorney');
> HTML:rint_button('setup.cgi','Return To Setup',$oprid, $role);
>'AddInsurer'
> ...
>
>It's not critical, but I'm just getting tired of looking at all the
>repetition.
>
>CC.


I haven't read any other reply's but I would bet there is that
switch/case mimic in Perl 5.10

Since you bring up the C switch/case construct, lets talk about
what that really is and how much you should really worry about it.

Forgetting languages except assembly for the moment. How many branch
instructions do you think there are, and how many language intrinsics
do you think you can make with them?

Lets get at least a little bit real. Case/Switch was added to C for only one
reason, as a pass through when conditions overlapp. It still uses the basic
assembly branches but it is still a compound.

Looks are in the eye of the beholder. You obviously never programmed in assembly.

-sln
 
Reply With Quote
 
Martijn Lievaart
Guest
Posts: n/a
 
      07-06-2009
On Mon, 06 Jul 2009 12:55:43 -0700, sln wrote:

> Lets get at least a little bit real. Case/Switch was added to C for only
> one reason, as a pass through when conditions overlapp. It still uses
> the basic assembly branches but it is still a compound.


Another reason is that it can often be implemented using a branch table.

>
> Looks are in the eye of the beholder. You obviously never programmed in
> assembly.
>


And obviously, you didn't look enough at disassembled C.

M4

 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      07-06-2009
On Mon, 6 Jul 2009 22:27:27 +0200, Martijn Lievaart <(E-Mail Removed)> wrote:

>On Mon, 06 Jul 2009 12:55:43 -0700, sln wrote:
>
>> Lets get at least a little bit real. Case/Switch was added to C for only
>> one reason, as a pass through when conditions overlapp. It still uses
>> the basic assembly branches but it is still a compound.

>
>Another reason is that it can often be implemented using a branch table.
>

[snip]
>
>M4


I'm sorry, I missed that branch table instruction in the op code list (rst?).
Perhaps you should dissasemble your assembler.

-sln
 
Reply With Quote
 
ccc31807
Guest
Posts: n/a
 
      07-06-2009
On Jul 6, 3:55*pm, (E-Mail Removed) wrote:
> I haven't read any other reply's but I would bet there is that
> switch/case mimic in Perl 5.10


Yes.

> Forgetting languages except assembly for the moment. How many branch
> instructions do you think there are, and how many language intrinsics
> do you think you can make with them?


As you say, looks are in the eye of the beholder. In this case, you
can see the logic in two ways. The first way is how I started out, by
switching based on the type (Agent, Attorney, Employer, etc) and task
(insert, update, delete, select). The second way is how I ended up, in
a more OO style, trashing all the branches and replacing them with a
generic function, like this:
$hashref = do_it($type, $task);

> Lets get at least a little bit real. Case/Switch was added to C for only one
> reason, as a pass through when conditions overlapp. It still uses the basic
> assembly branches but it is still a compound.


I don't know about that. I had six months of assembly years ago and
can't remember much about it.

> Looks are in the eye of the beholder. You obviously never programmed in assembly.


Yes, and yes.

CC
 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      07-06-2009
On Mon, 6 Jul 2009 14:03:06 -0700 (PDT), ccc31807 <(E-Mail Removed)> wrote:

>On Jul 6, 3:55*pm, (E-Mail Removed) wrote:

<snip>
>> Lets get at least a little bit real. Case/Switch was added to C for only one
>> reason, as a pass through when conditions overlapp. It still uses the basic
>> assembly branches but it is still a compound.

>
>I don't know about that. I had six months of assembly years ago and
>can't remember much about it.
>
>> Looks are in the eye of the beholder. You obviously never programmed in assembly.

>
>Yes, and yes.
>
>CC


I started out doing assembly on the z80a actually, before assemblers. Did alot of
hand written code (to be machine input later), later looked at all the 8080/286/386/486,
and assemblers (Intel mostly), then reviewed the Motorolla, then some other cpu's, which
were almost identical in basic functionality.

There is only jump relative and its conditional flavors, or jump absolute, and restart (rst)
which is a table absolute jump commonly used to boot (absolute code) systems or in response
to intterrupts (hardware reset), not used by languages.

So there you have it, jump relative (with conditional) or jump absolute.

Remember any of that?

-sln
 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      07-06-2009
On Mon, 06 Jul 2009 14:27:17 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

>On Mon, 6 Jul 2009 14:03:06 -0700 (PDT), ccc31807 <(E-Mail Removed)> wrote:
>
>>On Jul 6, 3:55*pm, (E-Mail Removed) wrote:

>So there you have it, jump relative (with conditional) or jump absolute.

Iiether can be conditional (relative or absolute).
I just never found the TABLE op code, u know. Assemblers have indirection
with locations, but the raw code is jumps, assemblers just embelish and translate
to simple op jumps. There is no construct more complex to the cpu no matter what
you think.

This is a prime example of learning before leaping.

-sln
 
Reply With Quote
 
Martijn Lievaart
Guest
Posts: n/a
 
      07-09-2009
On Mon, 06 Jul 2009 13:32:51 -0700, sln wrote:

>>Another reason is that it can often be implemented using a branch table.
>>

> [snip]
>>
>>M4

>
> I'm sorry, I missed that branch table instruction in the op code list
> (rst?). Perhaps you should dissasemble your assembler.


It's called an indirect jump. Sorry, but you really should take some
education here, these are rather basic concepts.

M4
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
one hot machine without elsif Brad Smallridge VHDL 10 02-19-2009 12:02 AM
nested if-elsif-then Vs case palo VHDL 0 04-25-2007 12:11 PM
if/elsif problem karlwijk VHDL 3 04-18-2007 05:52 AM
Ginerics mixed with if elsif else michaelb VHDL 0 09-30-2006 09:15 PM
Avoiding if..elsif statements unexpected Python 11 08-27-2006 12:58 PM



Advertisments