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

Hello Monks,

I made this version of a daily list for my lazy hippie roommate who complains that he can't find a list. I'm sure he wouldn't miss it if his smokes or his glasses were on it: Initial Output Q1) This validates as html5, but when I view it on windows with chrome, it looks like it's always loading. Is that because of the generated html or is that a browser/OS issue?

Outline for morning preparation indicates where I want go with this. I want to be specific in what I'm going to do to achieve these things before I head out the door. I tried the whole racket today, and it worked as far as me doing all the goodie-two-shoes stuff *and* getting out the door with everything I needed, even though that's always a bit of a guess.

The script I have is bulky. It's set up to do a lot of other things, like having russian captions, so a lot of this won't survive the first edit. Q2) One specific reason I'm making an html page of this is that I want to be able to find it on windows and print it, so as to have a physical copy of it in the morning. What would be a way of using this same data to create a file that could be printed without having to travel to the internet?

#!/usr/bin/perl -w use strict; use 5.010; use lib "template_stuff"; use html2; use nibley1; use utils1; use Cwd; use File::Basename; use Net::FTP; use Path::Class; use File::Slurp; use File::Spec; # initializations that must precede main data structure my $fspecfile = File::Spec->rel2abs(__FILE__); my $ts = "template_stuff"; my $images = "aimages"; my $captions = "captions"; my $ruscaptions = "ruscaptions"; my $bom = "bom"; my $current = cwd; my $rd1 = dir($current); my @a = $rd1->dir_list(); my $srd1 = $rd1->stringify; my $title = $rd1->basename; say "title is $title"; my $rd2 = dir(@a,$ts,$images); my $to_images = $rd2->stringify; my $rd3 = dir(@a,$ts,$captions); my $to_captions = $rd3->stringify; my $rd4 = dir(@a,$ts,$ruscaptions); my $rus_captions = $rd4->stringify; my $rd5 = dir(@a,$ts,$bom); my $bom_dir = $rd5->stringify; # page params my %vars = ( title => $title, headline => undef, place => 'Oakland', css_file => "${title}1.css", header => file($ts,"hc_input2.txt"), footer => file($ts,"footer_center3.txt"), css_local => file($ts,"${title}1.css"), body => file($ts,"rebus4.tmpl"), print_script => "0", code_tmpl=> file(@a,$ts,"code2.tmpl"), oitop=> file($ts,"oitop.txt"), oibottom=> file($ts,"oibottom.txt"), to_images => $to_images, eng_captions => $to_captions, rus_captions => $rus_captions, bottom => file($ts,"bottom2.txt"), words => file($bom_dir, "words1.txt"), subs => file($bom_dir, "substitutions1.txt"), source => file($bom_dir, "jacob1.txt"), book => 'List for Today', chapter => '', path => $to_captions, print_module => 0, script_file => $fspecfile, module_tmpl=> file(@a,$ts,"code3.tmpl"), ); #create html page my $rvars = \%vars; my $rftp = get_ftp_object(); my $html_file = get_html_filename($rftp); my $fh = create_html_file ($html_file); my $remote_dir = $html_file; $remote_dir =~ s/\.html$//; say "remote_dir is $remote_dir"; $vars{remote_dir}= $remote_dir; # create header my $rhdr = write_header($rvars); print $fh $$rhdr; # text_to_captions($rvars); my $refc = get_content($rvars); my @AoA = @$refc; print_aoa($refc); my $body = write_body($rvars, $refc); print $fh $$body; #my $rftr = write_footer($rvars); #print $fh $$rftr; if ($vars{"print_script"}) { my $script = write_script($rvars); print $fh $$script; } if ($vars{"print_module"}) { my $module = write_module($rvars); print $fh $$module; } my $rhbt = write_bottom($rvars); print $fh $$rhbt; close $fh; #load html file to server $rftp->cwd("/pages") or warn "cwd failed $!\n"; $rftp->put($html_file) or die "put failed $!\n"; #load css file to server $rftp->cwd("/css") or warn "cwd failed $@\n"; my $path3 = file(@a, $vars{"css_local"}); my $remote_css = $vars{"css_file"}; $rftp->put("$path3", $remote_css) or warn "put failed $@\n"; # load images $rftp->binary or warn "binary failed$!\n"; $rftp->cwd("/images") or warn "cwd failed $!\n"; $rftp->mkdir($remote_dir) or warn "mkdir failed $!\n"; $rftp->cwd($remote_dir) or warn "Cannot change working directory ", $rftp->message; for my $i ( 0 .. $#AoA ) { my $a = file(@a,$ts,$images,$AoA[$i][0]); my $sa = $a->stringify; my $b = file($AoA[$i][0]); my $sb = $b->stringify; $rftp->put($sa, $sb) or warn "put failed $@\n"; } $rftp->quit(); say "new file is $html_file"; __END__

Q3) What calender tools exist in perl for such a thing? Thanks for your comment.

Replies are listed 'Best First'.
Re: using perl to print out tomorrow's list
by blindluke (Hermit) on Nov 07, 2014 at 10:42 UTC

    It's great that you are using Perl to improve the everyday experience of others. Still, it's hard to provide some specific suggestions for your script - as you stated, it contains a lot. A concise example is usually much better. I'll try to provide as much help as I can.

    The easiest question to answer would be Q3 - try looking at Calendar::Simple. The documentation contains a great complete example here.

    When it comes to Q2 - HTML is not great when you want to produce documents suitable for printing. If your goal is to create a nice looking document that you can print and carry around with you - try creating a PDF file. It's a bit more complicated, but the documentation for the PDF::API2::Simple provides all you need for a simple list with a header and an image. Russian captions should not pose a problem.

    As to Q1 - it might be a problem on your side - the page does not appear to be loading in my browsers.

    I wish you good luck with your Perl project, and I'll try to update my response with an example using both modules I have suggested.


    UPDATE:

    So, here's the promised update. There is no calendar, and no image, but this should already be enough for you to start. As you will notice, I've changed my initial recommendation - although I have used PDF::API2::Simple for simple tasks before, there were problems with printing utf8 characters, and I had to use the underlying PDF::API2 object. So I switched to PDF::API2 entirely. There is a tutorial in Russian, too, but my limited knowledge of this language does not allow me to check if it's up to date. Seems good.

    Again, best luck with your efforts.

    #!/usr/bin/perl use v5.14; use utf8; use PDF::API2; my $header = 'placeholder header'; my $pdf = PDF::API2->new( width => 595, # A4 dimensions in point height => 842, # 1 point = 1/72 inch ); sub drawline { my ($line, $y) = @_; my $x1 = 50; my $x2 = 550; $line->linewidth(3); $line->move( $x1, $y ); $line->line( $x2, $y ); $line->stroke; } my $page = $pdf->page; my $txt = $page->text; my $font = $pdf->ttfont('DejaVuSans.ttf'); $txt->font($font, 32); $txt->translate(100, 650); $txt->text($header, -encoding => 'utf8'); my @task = ( 'wash the dishes', 'buy some meat', 'run a mile', 'buy more meat', 'plan a barbecue', ); my $line = $page->gfx; $txt->font($font, 20); my $vspace = 70; for my $i (0..$#task) { my $linepos = 500-($i*$vspace); my $msg = '[ ] '.$task[$i]; drawline ($line, $linepos); $txt->translate(70, $linepos+10); $txt->text($msg); } $pdf->saveas("output.pdf");

    You can replace the placeholder header with the string I used to test the script: my $header = 'мой маленький список';. I could not figure out a way to include unicode in the code example.

    - Luke

      Thanks all for replies,

      I combined the examples Luke gave to get a pretty decent list that prints out with a big top banner for the next day. Knowing what day it is right when you wake up is not something I'm equipped for, as I just make too many mistakes. It's a good thing that nothing I take in the morning keeps me alive, because I might forget once a week. I'm convinced that a checklist is the ticket. There are certain things I'm gonna want to have on this list every day. Maybe I should give them their own space.

      #!/usr/bin/perl -w use strict; use v5.14; use lib "template_stuff"; use utils1; use Path::Class; use PDF::API2; my $ts = "template_stuff"; my $images = "aimages"; my $captions = "captions"; my $to_windows = '/media/fred/Windows8_OS/Documents and Settings/Fred/ +Documents'; my @months = qw(January February March April May June July August September October November December); my @days = qw(Sun Mon Tue Wed Thu Fri Sat Sun); my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime() +; print "$mday $months[$mon] $days[$wday]\n"; my $day = $mday + 1; my $yr = $year +1900; my $header = "List for $days[$wday+1], $months[$mon -1] $day, $yr"; my $pdf = PDF::API2->new( width => 595, # A4 dimensions in point height => 842, # 1 point = 1/72 inch ); sub drawline { my ($line, $y) = @_; my $x1 = 50; my $x2 = 550; $line->linewidth(3); $line->move( $x1, $y ); $line->line( $x2, $y ); $line->stroke; } my $page = $pdf->page; my $txt = $page->text; my $font = $pdf->corefont('Times-Roman'); #my $font = $pdf->ttfont('DejaVuSans.ttf'); $txt->font($font, 32); $txt->translate(100, 650); $txt->text($header, -encoding => 'utf8'); my @task = ( 'set alarm for 8:30 --one snooze max', 'take meds', 'stretch', 'get on elliptical/shoot baskets/throw the disc/do yoga', 'eat toast/oatmeal', 'practice Buked', 'choir 10 a.m. --bring music', 'Skype 2:55 p.m.', ); my $line = $page->gfx; $txt->font($font, 20); my $vspace = 70; for my $i (0..$#task) { my $linepos = 500-($i*$vspace); my $msg = '[ ] '.$task[$i]; drawline ($line, $linepos); $txt->translate(70, $linepos+10); $txt->text($msg); } my $new_name = join ('_', $months[$mon -1], $day, $yr); say "new_name is $new_name"; my $save_file = file($to_windows, $new_name); #$pdf->saveas("$save_file.pdf"); $pdf->saveas("$new_name.pdf"); __END__

      Q1) How does one load up on the fonts that are out there like the DejaVu one that I had to comment out and use a corefont instead?

      If I delete the hash-commenter on the first save to my windows partition, I get this error:

      Unable to open /media/fred/Windows8_OS/Documents and Settings/Fred/Doc +uments/October_9_2014.pdf for writing at /usr/local/share/perl/5.18.2 +/PDF/API2/Basic/PDF/File.pm line 393. $

      I suspect that "I" as the perl executable don't have the proper permissions to do what I can do by copying and pasting with nautilus after the script is run. I can hardly express in words how fatigued I am in getting documents to windows so that I can print it, and I don't believe there's linux drivers for my hp 4620 printer (I looked today again). Q2) How do I convince my OS that I'm allowed to write in the directory? Or am I cobbling it together wrong with Path::Class?

      Finally, can someone talk me through what the map is doing in this script:

      $ perl cal1.pl months are January February March April May June July August September + October November December mon is 11 yr is 2014 month are ARRAY(0x1064440) ARRAY(0xff0ee0) ARRAY(0xff1a38) ARRAY(0xff1 +d08) ARRAY(0xf7f2d8) ARRAY(0xf7f380) November 2014 Su Mo Tu We Th Fr Sa 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 $ cat cal1.pl #!/usr/bin/perl -w use strict; use Calendar::Simple; use 5.01; my @months = qw(January February March April May June July August September October November December); say "months are @months"; my $mon = shift || (localtime)[4] + 1; my $yr = shift || (localtime)[5] + 1900; say "mon is $mon"; say "yr is $yr"; my @month = calendar($mon, $yr); say "month are @month"; print "\n$months[$mon -1] $yr\n\n"; print "Su Mo Tu We Th Fr Sa\n"; foreach (@month) { print map { $_ ? sprintf "%2d ", $_ : ' ' } @$_; print "\n"; } $

      One of the items that happy people do every day is read. A person could read perl and check it off....

        It's good that you try to figure out the workings of the code by inserting print statements in it. It's always a good start.

        As to your third question:

        say "month are @month"; month are ARRAY(0x1fa34e0) ARRAY(0x1fa3678) ARRAY(0x1fa41d0) ARRAY(0x1 +edd548) ARRAY(0x1edd5f0) ARRAY(0x1edd698)

        You were expecting the elements of the array, and here they are, still, they look a bit strange. Thing is, @month is an array of arrays (to be more specific, an array of array references). Data structures such as these are best viewed with Data::Dumper.

        use Data::Dumper; say Dumper \@month;

        You will see, that your array looks like this:

        $VAR1 = [ [ undef, undef, undef, undef, undef, undef, 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, undef, undef, undef, undef, undef, undef ] ];

        Now, you could just print it as is, with

        foreach (@month) { say join " ", @$_; }

        The code above means "iterate over the weeks of @month, treat each week as an array ( that's what @$_ does ), and print this array with one space between each element".

        It you try it, it will look like a mess, because each element will occupy zero, one, or two characters, and the days will not align under each other.

        The problem you are facing is this. When you are printing, you have an array that looks like this:

        ( undef, 2, 3, 10 )

        and you want an array like this:

        ( ' ', ' 2 ', ' 3 ', '10 ')

        with every element taking up exactly three spaces.

        It's a good rule: when you want to transform one array into another array, you should be thinking about map. map iterates over the array, and maps the value of the original element ( $_ ) to the value you want in your target array. You can read about map in Map: The Basics, or here.

        map, as used in your example, does exactly this:

        foreach (@month) { say map { if ($_) { # if the element is not undef sprintf "%2d ", $_; # format it so it takes 2 spaces, and add one space to it } else { # if the element is undef ' '; # replace it with 3 spaces } } @$_; }

        It looks different in your example, because it uses the ternary operator as a shorthand for the if ... else expression.

        Now, as to your first question, the line:

        my $font = $pdf->ttfont('DejaVuSans.ttf');

        assumes the font file is in the same directory. The argument for ttfonf is the path to the file. You can of course use the core fonts, but I'm afraid the Russian characters will not work (this was the reason I used ttfont in my example).

        - Luke

        ad Q2: are you sure that the "physical" name of that directory is "Documents and Settings"? Windows newer than Vista have some weird logic to make it appear so in Explorer, but the name on Disk changed to "Users", and trying to access the "logical" name results in "permission denied" rather than "not found"...
Re: using perl to print out tomorrow's list
by CountZero (Bishop) on Nov 07, 2014 at 10:38 UTC
    That is quite a job you have set yourself!

    Personally I would not use HTML as the file-format. PDF seems better suited for something you want to print.

    As it is very difficult to write raw-PDF code, I'd go through LaTeX first.

    I'd make a template which will be the framework and then have a Perl-script fill in the variable data. LaTeX will then compile it into a nice pdf file.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics

      Actually HTML's pretty awesome for print if you write a print stylesheet specifying margins, page break rules, etc. See Paged media in the CSS 2.1 spec.

      You're not going to get pixel-precise positioning of each character, but for most documents you don't need that anyway.