Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

(Sorry for English) Hi, currently I'm making an web application using only CGI module
And I'm not using frameworks like Catalyst,Dancer.. because I love DIY.
But programming with MVC pattern I have some trouble.

1. refine data and pass to the function vs pass raw data to the function and function refines the data

for example :

search_by_tags(split ',', $cgi->param('tags'));

versus

search_by_tags($cgi->param('tags')); # split called inside this functi +on

2. I have two controller classes Controller::Admin and Controller::Auth and there is one bootstrap file index.pl
I wonder which is better design :

Each Controller class calls $self->get_view()->print_blabla() as a result and that method prints a piece of HTML

versus

Controller returns a piece of HTML and bootstrapping script index.pl prints it.
for example:

###index.pl use Controller::Admin; Controller::Admin->new()->run(); ###Controller/Admin.pm sub run(){ my $auth = Controller::Auth->new(); $auth->loggedin() or $auth->require_login(); return; # that prints + login page # #Do some tasks if the user has logged in # }
versus
###index.pl use Controller::Admin; print Controller::Admin->new()->run(); ###Controller/Admin.pm sub run(){ my $auth = Controller::Auth->new(); $auth->loggedin() or return $auth->require_login(); # that RETURNS + login page # #Do some tasks if the user has logged in # }

Replies are listed 'Best First'.
Re: Which is better software design
by hippo (Archbishop) on Mar 27, 2015 at 10:37 UTC

    Personally I would try not do this:

    search_by_tags(split ',', $cgi->param('tags'));

    because it does not take account of the possibility that there is no param "tags" and then you would be calling split on an undefined value. This isn't really bad in and of itself but it encourages the use of this pattern for other, potentially more dangerous operations than split which may have worse consequences. I'd pass the string/undef and let the sub check for undef and only then attempt the split. eg:

    search_by_tags ($cgi->param('tags')); #... sub search_by_tags { my $tagstr = shift; return unless defined $tagstr; my @tags = split (/,/, $tagstr); }

    This also gives you the opportunity within the subroutine to raise an exception in the undef case which might be helpful.

      This of course leads to the question of whether it should receive a csv list or whether another component is responsible for this aspect of processing.

      The answer of course depends not on tactics but on strategy. If you want your tag search to be closely tied to this representation then your suggestion is good, but if you want to make it more general and allow that representation to change, then you need to structure it differently.

        Completely agree. The change in structure is a much bigger job but one worth doing (or at least considering).

        Without knowing the detail of the OP's system it is hard to suggest anything specific. That said, generally the best approach IMHO to vanilla CGI is to collect, validate and sanitise the inputs right at the start so that by the time any real processing occurs the script has a known clean data set to work with. This is almost always worth the code refactoring work to achieve it and results in the rest of the code being leaner for having to do less validation at that later stage.

Re: Which is better software design
by einhverfr (Friar) on Mar 27, 2015 at 10:00 UTC

    These strike me as tactial questions when you should be asking strategic questions.

    • How will you structure things library-wise?
    • How will the libraries be used?
    • What abstractions do you want to depend on? (Depend on abstractions, not details)

    I think if you answer those questions then what is good design will become more aparent. Put the strategy first, and then let the tactics match the strategy. Otherwise you will have no strategy and bad design no matter which decisions you make.