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

HI,

I am a beginner in Perl and I am programing web calendar in Embperl. I use Date::Calc module for this. You can see calendar at site Calendar

All is OK but the shading of today is bad because is shading every day every month and year. Its mean when you change month you will see shade the cell which day is today.

Here is part of code:

if (Today([$year,$month,$day]) eq ($year,$month,$date)) { $bgcolor="#c0c0c0"; } else { $bgcolor="#FFFFFF"; }

Function Today give me today date. And in cycle I change date. But this code compare only day and date and no year and month. Where is problem???

Excuse my bad English I am from Slovak republic and my English is poor.

Thanks

20040211 Edit by Corion: Fixed formatting

Replies are listed 'Best First'.
Re: Compare of array
by ysth (Canon) on Feb 11, 2004 at 23:28 UTC
    I think you need to read the Date::Calc documentation a little better. You are misusing the Today function; it takes a boolean parameter saying whether to return today's date according to your local time zone or according to universal time, not an array reference.

    It also has a RECIPES section, with the very first example being comparing two dates. I think you want:

    if (Delta_Days(Today(0), $year, $month, $day) == 0)
    or, being perhaps a little more efficient, outside your formatting loops, say:
    $today_in_days = Date_to_Days(Today(0));
    and then,
    if ($today_in_days == Date_to_Days($year, $month, $day))
    Use Today(1) instead to use universal time instead of your server's local time to determine today's date.

      Thank you very much. Your solution ysth is simple and work. Perfect.
      Thanks to all for your replies.

      Excuse my bad English !!!
Re: Compare of array
by DrHyde (Prior) on Feb 11, 2004 at 12:14 UTC

      I use Data::Compare module

      @td = Today([$year,$month,$day]); @tod=($year,$month,$date); $c=Compare($td,$tod); if ($c==1) { $bgcolor="#c0c0c0"; } else { $bgcolor="#FFFFFF"; }

      and this error show in Internet Explorer

      [920]ERR: 24: Error in Perl code: Undefined subroutine &Embperl::__5:: +Compare called at d:\sukromne\www\epl\calendar.html line 58.

      What is wrong ???

      Excuse my bad English !!!
        Undefined subroutine &Embperl::__5::Compare

        Without seeing the details of what you're doing, it's hard to be sure (especially since I've never used Embperl), but this looks to me like it might be a namespace issue. When you call the Compare subroutine, try expressly specifying which one you mean:

        @td = Today([$year,$month,$day]); @tod=($year,$month,$date); $c=Data::Compare::Compare($td,$tod); if ($c==1) { $bgcolor="#c0c0c0"; } else { $bgcolor="#FFFFFF"; }

        For comparing dates, though, I would probably be using DateTime or one of the other modules specifically designed to work with dates.

        One last question: did you use the module whose routine you were trying to call? Your code needs to say something like use Data::Compare before you can call anything it defines.


        $;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}} split//,".rekcah lreP rehtona tsuJ";$\=$ ;->();print$/
        Data::Compare is a bit overkill unless you are comparing a lot of arrays. If you are just doing this compare, I would compare the elements.
        @td = Today([$year,$month,$day]); @tod=($year,$month,$date); if ($td[0]==$tod[0] && $td[1]==$tod[1] && $td[2]==$tod[2]) { $bgcolor="#c0c0c0"; } else { $bgcolor="#FFFFFF"; }

        --

        flounder