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

Hi. Nowdays I programing a code for iCall application on internet. I wrote code which show table with days of month. You can click on day and show you table with hours and minutes. And here is my little problem. Code is fully functional but is big and gauche, I think. Do you have some idea how can I make my code smaller and profesional??? Here is link where you can see result and try it.

[- use Date::Calc qw(:all); %mesiac=(1=>'January', 2=>'February', 3=>'March', 4=>'April', 5=>'May' +, 6=>'June', 7=>'July', 8=>'August', 9=>'September', 10=>'October', 1 +1=>'November', 12=>'December'); %den=(1=>'Monday', 2=>'Tuesday', 3=>'Wednesday', 4=>'Thursday', 5=>'Fr +iday', 6=>'Saturday', 7=>'Sunday'); $day = $fdat{day}; $month = $fdat{month}; $year = $fdat{year}; $dom = Days_in_Month($year,$month); -]

This part of code testing which days will be next or previous and create anchors for these days. This part is big and I think may be write better, but I dont know how. Please write some idea how can I do it better.

[$ if ($day == $dom) $] [- if ($month == 12) { $mmonth = 1; $myear = $year+1; } else { $mmonth = $month+1; $myear = $year; } $day1 = $day-2; $day2 = $day-1; $day3 = 1; $day4 = 2; $dow1=Day_of_Week($year,$month,$day1); $dow2=Day_of_Week($year,$month,$day2); $dow3=Day_of_Week($myear,$mmonth,$day3); $dow4=Day_of_Week($myear,$mmonth,$day4); $dow=Day_of_Week($year,$month,$day); -] <center> <a href="?mod=day&month=[+ $month +]&year=[+ $year +]&day=[+ $day1 +]" +>[+ $den{$dow1} +]</a>&nbsp;&nbsp;&nbsp; <a href="?mod=day&month=[+ $month +]&year=[+ $year +]&day=[+ $day2 +]" +>[+ $den{$dow2} +]</a>&nbsp;&nbsp;&nbsp; [+ $den{$dow} +]&nbsp;&nbsp;&nbsp; <a href="?mod=day&month=[+ $mmonth +]&year=[+ $myear +]&day=[+ $day3 + +]">[+ $den{$dow3} +]</a>&nbsp;&nbsp;&nbsp; <a href="?mod=day&month=[+ $mmonth +]&year=[+ $myear +]&day=[+ $day4 + +]">[+ $den{$dow4} +]</a> </center></br> [$ elsif ($day == $dom-1) $] [- if ($month == 12) { $mmonth = 1; $myear = $year+1; } else { $mmonth = $month+1; $myear = $year; } $day1 = $day-2; $day2 = $day-1; $day3 = $day+1; $day4 = 1; $dow1=Day_of_Week($year,$month,$day1); $dow2=Day_of_Week($year,$month,$day2); $dow3=Day_of_Week($year,$month,$day3); $dow4=Day_of_Week($myear,$mmonth,$day4); $dow=Day_of_Week($year,$month,$day); -] <center> <a href="?mod=day&month=[+ $month +]&year=[+ $year +]&day=[+ $day1 +]" +>[+ $den{$dow1} +]</a>&nbsp;&nbsp;&nbsp; <a href="?mod=day&month=[+ $month +]&year=[+ $year +]&day=[+ $day2 +]" +>[+ $den{$dow2} +]</a>&nbsp;&nbsp;&nbsp; [+ $den{$dow} +]&nbsp;&nbsp;&nbsp; <a href="?mod=day&month=[+ $month +]&year=[+ $year +]&day=[+ $day3 +]" +>[+ $den{$dow3} +]</a>&nbsp;&nbsp;&nbsp; <a href="?mod=day&month=[+ $mmonth +]&year=[+ $myear +]&day=[+ $day4 + +]">[+ $den{$dow4} +]</a> </center></br> [$ elsif ($day == 2) $] [- if ($month == 1){ $mmonth = 12; $myear = $year-1; } else { $mmonth = $month - 1; $myear = $year; } $mdom = Days_in_Month($myear,$mmonth); $day1 = $mdom; $day2 = 1; $day3 = 3; $day4 = 4; $dow1=Day_of_Week($myear,$mmonth,$day1); $dow2=Day_of_Week($year,$month,$day2); $dow3=Day_of_Week($year,$month,$day3); $dow4=Day_of_Week($year,$month,$day4); $dow=Day_of_Week($year,$month,$day); -] <center> <a href="?mod=day&month=[+ $mmonth +]&year=[+ $myear +]&day=[+ $day1 + +]">[+ $den{$dow1} +]</a>&nbsp;&nbsp;&nbsp; <a href="?mod=day&month=[+ $month +]&year=[+ $year +]&day=[+ $day2 +]" +>[+ $den{$dow2} +]</a>&nbsp;&nbsp;&nbsp; [+ $den{$dow} +]&nbsp;&nbsp;&nbsp; <a href="?mod=day&month=[+ $month +]&year=[+ $year +]&day=[+ $day3 +]" +>[+ $den{$dow3} +]</a>&nbsp;&nbsp;&nbsp; <a href="?mod=day&month=[+ $month +]&year=[+ $year +]&day=[+ $day4 +]" +>[+ $den{$dow4} +]</a> </center></br> [$ elsif ($day == 1) $] [- if ($month == 1) { $mmonth = 12; $myear = $year-1; } else { $mmonth = $month - 1; $myear = $year; } $dom = Days_in_Month($myear,$mmonth); $day1 = $dom-1; $day2 = $dom; $day3 = 2; $day4 = 3; $dow1=Day_of_Week($myear,$mmonth,$day1); $dow2=Day_of_Week($myear,$mmonth,$day2); $dow3=Day_of_Week($year,$month,$day3); $dow4=Day_of_Week($year,$month,$day4); $dow=Day_of_Week($year,$month,$day); -] <center> <a href="?mod=day&month=[+ $mmonth +]&year=[+ $myear +]&day=[+ $day1 + +]">[+ $den{$dow1} +]</a>&nbsp;&nbsp;&nbsp; <a href="?mod=day&month=[+ $mmonth +]&year=[+ $myear +]&day=[+ $day2 + +]">[+ $den{$dow2} +]</a>&nbsp;&nbsp;&nbsp; [+ $den{$dow} +]&nbsp;&nbsp;&nbsp; <a href="?mod=day&month=[+ $month +]&year=[+ $year +]&day=[+ $day3 +]" +>[+ $den{$dow3} +]</a>&nbsp;&nbsp;&nbsp; <a href="?mod=day&month=[+ $month +]&year=[+ $year +]&day=[+ $day4 +]" +>[+ $den{$dow4} +]</a> </center></br> [$ else $] [- $day1 = $day-2; $day2 = $day-1; $day3 = $day+1; $day4 = $day+2; $dow1=Day_of_Week($year,$month,$day1); $dow2=Day_of_Week($year,$month,$day2); $dow3=Day_of_Week($year,$month,$day3); $dow4=Day_of_Week($year,$month,$day4); $dow=Day_of_Week($year,$month,$day); -] <center> <a href="?mod=day&month=[+ $month +]&year=[+ $year +]&day=[+ $day1 +]" +>[+ $den{$dow1} +]</a>&nbsp;&nbsp;&nbsp; <a href="?mod=day&month=[+ $month +]&year=[+ $year +]&day=[+ $day2 +]" +>[+ $den{$dow2} +]</a>&nbsp;&nbsp;&nbsp; [+ $den{$dow} +]&nbsp;&nbsp;&nbsp; <a href="?mod=day&month=[+ $month +]&year=[+ $year +]&day=[+ $day3 +]" +>[+ $den{$dow3} +]</a>&nbsp;&nbsp;&nbsp; <a href="?mod=day&month=[+ $month +]&year=[+ $year +]&day=[+ $day4 +]" +>[+ $den{$dow4} +]</a> </center></br> [$ endif $]
This little code generate table with hours and minutes.
<center>[+ $day +].&nbsp;[+ $mesiac{$month} +]&nbsp;[+ $year +]&nbsp;& +nbsp;&nbsp;</center> <center> <table width="60%" border="1"> [- $hour = 6 -] [$ while $hour<=20 $] [- $min = 00 -] [$ while $min <= 30 $] <tr> [$ if ($min == 0) $] <td width="20%">[+ $hour +]:[+ $min +][+ $min +]</td> [$ else $] <td width="20%">[+ $hour +]:[+ $min +]</td> [$ endif $] <td> &nbsp; </td> </tr> [- $min += 30 -] [$ endwhile $] [- $hour += 1 -] [$ endwhile $] </table> <a href="?mod=calendar&amonth=[+ $month +]&ayear=[+ $year +]">Back on +the week</a></br> </center>

Thanks for all ideas.

Programing in Embperl and all questions are related to Embperl.
Excuse my bad English !!!

Edited by Chady -- moved node out of `pmsig' div

Replies are listed 'Best First'.
Re: Make code smaller and better
by Aragorn (Curate) on Mar 25, 2004 at 11:40 UTC
    I see lots of repeated (or at least, remarkably similar) code in the if statements. I'm sure there's a way to "factor" that out into a function which you then call from the if's.

    Arjen

Re: Make code smaller and better
by dragonchild (Archbishop) on Mar 25, 2004 at 13:33 UTC
    Anytime you have variables called $foo1, $foo2, $foo3, etc ... that's a sign you should be using an array (or possibly a hash). Why are you forcing yourself to stick with scalars? Once you use more complex data structures, you can use a lot of neat features that many would consider "compact and professional".

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

Re: Make code smaller and better
by Juerd (Abbot) on Mar 25, 2004 at 13:28 UTC

    CPAN has several modules that create HTML tables for calendars. Why are you not using any of them?

    Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

      I am a beginner in Perl and I want to learn Perl for now without CPAN modules. I know is a lot of CPAN modules but I use them when I learn pure Perl.

      Programing in Embperl and all questions are related to Embperl.
      Excuse my bad English !!!
        That's like saying you want to learn joinery, but without cheating by using hammers.

        Makeshifts last the longest.

        So far it looks to me that the you have started to learn pure Perl on the basis of your ability to make such a simple program. As dragonchild had pointed out, you can use arrays in place of scalars... however this still is not enough to make your code shorter as you wanted.

        At this stage of your development, you should start using modules to make life easier. Your calendar program should be enough, so start moving on.

        But if you insist, try downloading a CPAN module for a calendar application. Try opening the module and learn from it. Learn how the author made his implementation.