Guardicchiando per un progetto mi sono imbattuto in HTML::CalendarMonth, un modulo perl interessante per la generazione di calendarietti in html.
Il tutto si basa sui moduli HTML::Element che creano degli oggetti per la gestione dei singoli elementi della pagina html. Segue un esempio particolarmente brutto, ma utile ai fini di capire la logica.
Il punto della questione è definire gli elementi HTML con l’apposito costruttore new(‘tag’, eventuale_attributo => ‘come hash’); gli elementi possono poi essere gestiti con una struttura parent-child attraverso il metodo push_content che ordina in un array gli elementi che lo compongono.
# HTML::Element crea oggetti con nomi/tag e attributi.
my $html = HTML::Element->new(‘html’);
my $head = HTML::Element->new(‘head’);
my $title = HTML::Element->new(‘title’);
$title->push_content(‘Titolo della pagina’);
my $body = HTML::Element->new(‘body’);
my $h1 = HTML::Element->new(‘h1′);
$h1->push_content(‘Header nel body’);
my $hlink = HTML::Element->new(‘a’, href => ‘http://www.simotrone.it’);
$hlink->push_content(‘il mio sito’);
my $p = HTML::Element->new(‘p’);
$p->push_content(‘Questo paragrafo ha un link: ‘, $hlink, ‘.’);
$head->push_content($title);
$body->push_content($h1, $p);
$html->push_content($head, $body);
print $html->as_HTML;
Per ottenere:
Ok, fin qui sembra una pompa, 150 righe per ottenerne 5. Non molto economico.
Comunque il punto della questione era far vedere HTML::CalendarMonth, che sfruttando i moduli suddetti ritorna cose interessanti con poco codice. ![]()
Con queste poche righe…
use HTML::CalendarMonth;
my $cal = HTML::CalendarMonth->new( month => 10, year => 2009 );
$cal->item($cal->year, $cal->month)->attr(style => ‘background-color: wheat; font-size: 150%;’);
$cal->col(0)->attr(style => ‘font-weight: bold’);
print $cal->as_HTML;
possiamo ottenere questo:
| October | 2009 | |||||
| Sun | Mon | Tue | Wed | Thu | Fri | Sat |
| 1 | 2 | 3 | ||||
| 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 25 | 26 | 27 | 28 | 29 | 30 | 31 |
E con la semplice aggiunta di un ciclo for che genera i mesi e di una sub possiamo ottenere un calendario molto flessibile.
my ($mm,$yyyy) = @_;
my $cal = HTML::CalendarMonth->new( month => $mm, year => $yyyy );
$cal->item($cal->year, $cal->month)->attr(style => ‘background-color: wheat; font-size: 150%;’);
$cal->col(0)->attr(style => ‘font-weight: bold’);
print $cal->as_HTML;
print "\n";
}
for (1,2,3) {
mensile($_,1979);
}
Su Debian i pacchetti sono questi qua:
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Cfg-files/Unpacked/Failed-cfg/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name Version Description
+++-=================-=================-=======================
ii libhtml-calendarm 1.19-1 generate and manipulate
calandar months in HTML
ii libhtml-element-e 1.17-3 extended HTML::Element
classes
ii libhtml-tableextr 2.10-3 module for extracting
the content contained
in tab
Write a comment
You need tologin.