#!/usr/bin/perl # # use warnings; use strict; use DateTime; use HTML::Template; use Fcntl; my $dt = DateTime->new( year => 1964, month => 10, day => 16, hour => 16, minute => 12, second => 47, nanosecond => 500000000, time_zone => 'Asia/Taipei', ); $dt = DateTime->now(); # same as ( epoch => time() ) my $year = $dt->year; my $is_leap = $dt->is_leap_year; my $month_abbr = $dt->month_abbr; # Jan, Feb, ... my $mnth = $dt->subtract(months => 1); my $first = $mnth->clone->set_day(1); my $first1 = $first->day . $first->month_abbr . $first->year; my $last = $first->clone->add( months => 1 )->subtract( days => 1 ); my $last1 = $last->day . $last->month_abbr . $last->year; my $lastmonth_period = "$first1 - $last1"; my $cust_file = "customer.txt"; my $billing_file = "20100301.bill"; my $cust; open (CUST_DATA, $cust_file) || die "Can't read '$cust_file': $!\n"; while (defined (my $line = )) { chomp $line; # Remove trailing newline my ($cust_id,$cust_name) = split(/:/,$line); $cust->{$cust_id}->{name} = $cust_name; } close(CUST_DATA); my @bill_fields = qw(cust_id x_cust_id2 Device_Code Port Traf_Dir x_start_date x_end_date x_number Data_Usage); my $bill_line; my $bill_data; open(BILL_DATA, $billing_file) || die "Can't read '$billing_file': $!\n"; while (defined (my $line = )) { chomp $line; # Remove trailing newline @{$bill_line}{@bill_fields} = split(/\s/, $line); $bill_data->{ $bill_line->{cust_id} }->{ $bill_line->{Device_Code} }->{ $bill_line->{Port} }->{ $bill_line->{Traf_Dir} } = $bill_line->{Data_Usage}; } close(BILL_DATA); sub by_name ($$) { return $cust->{$a}->{name} <=> $cust->{$b}->{name} }; my @loop; # the loop data will be put in here my @nameloop; for my $customer ( keys %$cust ) { my $total_usage = 0; my $cust_name = "$cust->{$customer}->{name}"; # Create each customer html file my $template = HTML::Template->new(filename => "billfix.tmpl"); sysopen (HTML, "$cust_name.html", O_RDWR|O_EXCL|O_CREAT, 0755); my %namerow = ( cust_name => $cust_name, ); push(@nameloop, \%namerow); $template->clear_params(custname_loop => \@nameloop); $template->param(custname_loop => \@nameloop); for my $device ( sort by_name keys %{$bill_data->{$customer}} ) { for my $port ( sort keys %{$bill_data->{$customer}->{$device}} ) { (my $short_port = $port) =~ s/^FastEthernet/Fa/g; for my $direction qw(OUT IN) { my $usage = $bill_data ->{$customer}->{$device}->{$port}->{$direction} . $/; my %row = ( lastmonth_period => $lastmonth_period, device => $device, port => $port, direction => $direction, usage => $usage, ); push(@loop, \%row); } $template->param(billfix_loop => \@loop); } } printf HTML $template->output; }