develooper Front page | perl.beginners | Postings from April 2010

AW: How to manage large Conditional Statement

Thread Previous | Thread Next
From:
=?iso-8859-1?Q?Thomas_B=E4tzler?=
Date:
April 20, 2010 23:45
Subject:
AW: How to manage large Conditional Statement
Message ID:
E350C77ABFDBD242BDD51F5DA07905C104B55CE6@SONNE.gw.bringe.net
Mimi Cafe <mimicafe@googlemail.com> asked:
> I need to evaluate a statement in an if, elsif, else block, and the
> statement to evaluate in the elsif is too large as I need to run a SQL
> query and act on the return value.
> What would be a better way to achieve this? Putting the SQL statement
> elsewhere outside the block cannot be right, so I am wondering what to
> do here.

I'm talking for myself here, but the way I usually do this is that I wrap up each SQL query or set of related SQL queries in a subroutine.

If the sub's going to be used as a predicate I make sure it returns true/false and pass all of the relevant data as arguments. By giving it a suitable name, I end up with nicely readable code like

	[...]
	if( isSomeCondition( $foo, $baz, $bar ) ){
	[...]

More or less straight out of Perl Best Practices, Chapter 3.

HTH,
Thomas


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About