[32560] in Perl-Users-Digest
Perl-Users Digest, Issue: 3826 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Nov 27 21:09:21 2012
Date: Tue, 27 Nov 2012 18:09:08 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Tue, 27 Nov 2012 Volume: 11 Number: 3826
Today's topics:
Re: Creating visual graphics <bernie@fantasyfarm.com>
Re: Creating visual graphics <willem@turtle.stack.nl>
Re: Creating visual graphics <ben@morrow.me.uk>
Re: How comes (goes?) Perl 6? <brian.d.foy@gmail.com>
Re: How comes (goes?) Perl 6? (Seymour J.)
Re: New perl books in the last year or two (or three)? <brian.d.foy@gmail.com>
Re: using templates effectively act Two scene 1 <derykus@gmail.com>
Re: using templates effectively act Two scene 1 <cal@example.invalid>
Re: using templates effectively act Two scene 1 <cal@example.invalid>
Re: using templates effectively act Two scene 1 <ben@morrow.me.uk>
Re: using templates effectively act Two scene 1 <cal@example.invalid>
Re: using templates effectively act Two scene 1 <cal@example.invalid>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Tue, 27 Nov 2012 14:43:09 -0500
From: Bernie Cosell <bernie@fantasyfarm.com>
Subject: Re: Creating visual graphics
Message-Id: <jl5ab8leute0tobqq6ifuhsjkn649t3ga0@library.airnews.net>
Eli the Bearded <*@eli.users.panix.com> wrote:
} In comp.lang.perl.misc, Bernie Cosell <bernie@fantasyfarm.com> wrote:
} > reminder from Willem about Tk::Canvas, but I've used tk in the past and
} > didn't like it a lot. Using a scratch file in temp seems easy enough.
} >
} > It'd still be nice if I could open a window somehow and talk to it with
} > GD... :o)
}
} Non Perl response:
}
} On linux, I like feh as an image viewer. You can have feh reload an image
} every (integer) N seconds. Not quite realtime, but good for enough for
} some purposes. I have used it for reloading electoral result maps every
} few minutes.
I was musing about Jim's suggestion about gnuplot and I got to thinking
something similar to that: there must be some image-viewer for Windows that
can be poked to update its image somehow, and then I can use GD to generate
png images and poke whatever-it-is to redisplay them. As mentioned: not
really like a real-time "active" display but it might be fun...
/Bernie\
--
Bernie Cosell Fantasy Farm Fibers
bernie@fantasyfarm.com Pearisburg, VA
--> Too many people, too few sheep <--
------------------------------
Date: Tue, 27 Nov 2012 22:19:36 +0000 (UTC)
From: Willem <willem@turtle.stack.nl>
Subject: Re: Creating visual graphics
Message-Id: <slrnkbaf3o.n67.willem@turtle.stack.nl>
Bernie Cosell wrote:
) Eli the Bearded <*@eli.users.panix.com> wrote:
)
) } In comp.lang.perl.misc, Bernie Cosell <bernie@fantasyfarm.com> wrote:
) } > reminder from Willem about Tk::Canvas, but I've used tk in the past and
) } > didn't like it a lot. Using a scratch file in temp seems easy enough.
) } >
) } > It'd still be nice if I could open a window somehow and talk to it with
) } > GD... :o)
) }
) } Non Perl response:
) }
) } On linux, I like feh as an image viewer. You can have feh reload an image
) } every (integer) N seconds. Not quite realtime, but good for enough for
) } some purposes. I have used it for reloading electoral result maps every
) } few minutes.
)
) I was musing about Jim's suggestion about gnuplot and I got to thinking
) something similar to that: there must be some image-viewer for Windows that
) can be poked to update its image somehow, and then I can use GD to generate
) png images and poke whatever-it-is to redisplay them. As mentioned: not
) really like a real-time "active" display but it might be fun...
How about IE, and a small html page with refresh meta tag and a file:// img?
SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
------------------------------
Date: Tue, 27 Nov 2012 22:46:43 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Creating visual graphics
Message-Id: <jfsfo9-kht.ln1@anubis.morrow.me.uk>
Quoth Bernie Cosell <bernie@fantasyfarm.com>:
> Eli the Bearded <*@eli.users.panix.com> wrote:
>
> } In comp.lang.perl.misc, Bernie Cosell <bernie@fantasyfarm.com> wrote:
> } > reminder from Willem about Tk::Canvas, but I've used tk in the past and
> } > didn't like it a lot. Using a scratch file in temp seems easy enough.
> } >
> } > It'd still be nice if I could open a window somehow and talk to it with
> } > GD... :o)
> }
> } Non Perl response:
> }
> } On linux, I like feh as an image viewer. You can have feh reload an image
> } every (integer) N seconds. Not quite realtime, but good for enough for
> } some purposes. I have used it for reloading electoral result maps every
> } few minutes.
>
> I was musing about Jim's suggestion about gnuplot and I got to thinking
> something similar to that: there must be some image-viewer for Windows that
> can be poked to update its image somehow, and then I can use GD to generate
> png images and poke whatever-it-is to redisplay them. As mentioned: not
> really like a real-time "active" display but it might be fun...
If you've done any GUI programming writing a little Gtk or Tk app which
just displays an image, which you can change as you need to, is not
difficult. (For Windows Wx would be better, but looks a little harder on
first glance; or perhaps Win32::GUI might be a possibility.)
Ben
------------------------------
Date: Tue, 27 Nov 2012 04:11:26 -0600
From: brian d foy <brian.d.foy@gmail.com>
Subject: Re: How comes (goes?) Perl 6?
Message-Id: <271120120411263611%brian.d.foy@gmail.com>
In article <k8s1pt$i8p$1@panix2.panix.com>, David Combs
<dkcombs@panix.com> wrote:
> About Perl 5: are there enough changes since the 3rd ed of
> Programming Perl to be worth a 4th ed? (Is the market big
> enough to make it pay?)
The 3rd Edition covered Perl 5.6, while the new edition covers up to
Perl 5.16. Some things that are important there:
* All the new features and keywords from Perl 5.10 and on
* Much better Unicode support and ways of using it
* Lots (lots!) more regex features and syntax
* CPAN has evolved quite a bit
I think there were more than enough changes and the update was long
overdue.
------------------------------
Date: Tue, 27 Nov 2012 08:40:57 -0500
From: Shmuel (Seymour J.) Metz <spamtrap@library.lspace.org.invalid>
Subject: Re: How comes (goes?) Perl 6?
Message-Id: <50b4c2e9$7$fuzhry+tra$mr2ice@news.patriot.net>
In <271120120411263611%brian.d.foy@gmail.com>, on 11/27/2012
at 04:11 AM, brian d foy <brian.d.foy@gmail.com> said:
>The 3rd Edition covered Perl 5.6, while the new edition covers up to
>Perl 5.16.
The cover says 5.14, not 5.16.
>Some things that are important there:
>* All the new features and keywords from Perl 5.10 and on
>* Much better Unicode support and ways of using it
>* Lots (lots!) more regex features and syntax
>* CPAN has evolved quite a bit
On the flip side, chapters 32 and 33 in the 3rd edition are missing
from the 4th. I understand why the authors removed them, but as a
reader I wish that they hadn't.
--
Shmuel (Seymour J.) Metz, SysProg and JOAT <http://patriot.net/~shmuel>
Unsolicited bulk E-mail subject to legal action. I reserve the
right to publicly post or ridicule any abusive E-mail. Reply to
domain Patriot dot net user shmuel+news to contact me. Do not
reply to spamtrap@library.lspace.org
------------------------------
Date: Tue, 27 Nov 2012 03:58:50 -0600
From: brian d foy <brian.d.foy@gmail.com>
Subject: Re: New perl books in the last year or two (or three)? [Borders, etc, gone]
Message-Id: <271120120358508277%brian.d.foy@gmail.com>
In article <k8s0fa$khp$1@panix2.panix.com>, David Combs
<dkcombs@panix.com> wrote:
> Amazon, unfortunately, doesn't (as far as I know) list books
> by publication date.
Sure it does. It turns out to not be very useful because "perl" has a
lot of hits.
When you search and get results, you're probably getting results from
all departments. Above the results should be something that says
something like "Choose a department to enable sorting". In books, one
of the options is publication date.
However, just pay attention to the new releases from the major
publishers. I have those in my RSS reader. I see the new titles and
move on in less than a minute.
------------------------------
Date: Mon, 26 Nov 2012 22:34:46 -0800 (PST)
From: "C.DeRykus" <derykus@gmail.com>
Subject: Re: using templates effectively act Two scene 1
Message-Id: <144c50e3-328f-4ad8-9787-a0179af23292@googlegroups.com>
On Monday, November 26, 2012 7:11:31 PM UTC-8, Cal Dershowitz wrote:
> On 11/26/2012 12:49 AM, Ben Morrow wrote:
>
> >
>
> > Quoth Cal Dershowitz <cal@example.invalid>:
>
> >>
>
> >> # main control
>
> >> for my $name (@files) {
>
> >> print "name is $name\n";
>
> >> my ($ext) = $name =~ /([^.]*)$/;
>
> >> print "ext is $ext\n";
>
> >>
>
> >> @matching = map /image_(\d+)\.$ext$/, @list;
>
> >> print "matching is @matching\n";
>
> >> push( @matching, 1 );
>
> >> @matching = sort { $a <=> $b } @matching;
>
> >> $winner = pop @matching;
>
> >> my $newnum = $winner + 1;
>
> >
>
> > There is no need to redo the search through the list every time. If you
>
> > move this code to a sub you can just remember the next number for each
>
> > $ext, something like
>
> >
>
> > my %next_for_ext;
>
> > sub next_for_ext {
>
> > my ($ext) = @_;
>
> >
>
> > unless (exists $next_for_ext{$ext}) {
>
> > my @matching = map ... @list;
>
> > ...;
>
> > $next_for_ext{$ext} = pop @matching;
>
> > }
>
> >
>
> > return ++$next_for_ext{$ext};
>
> > }
>
> >
>
> > then in the loop just call the sub
>
> >
>
> > my $newnum = next_for_ext $ext;
>
> >
>
> > (and, of course, you don't then have to keep adding the new names to the
>
> > list).
>
> >
>
>
>
> $ ./ftp4.pl my_ftp
>
> ...
>
> name is target4/26-img_0004.jpg
>
> ext is jpg
>
> Can't locate object method "next_for_ext" via package "jpg" (perhaps you
>
> forgot to load "jpg"?) at ./ftp4.pl line 72.
>
> ...
The perl compiler needs some help if
you don't add the parentheses on that
next_for_ext call.
Easiest correction:
my $newnum = next_for_ext( $ext );
or just add at the top:
use subs q/next_for_ext/
For more info: perldoc subs
--
Charles DeRykus
------------------------------
Date: Tue, 27 Nov 2012 12:54:23 -0700
From: Cal Dershowitz <cal@example.invalid>
Subject: Re: using templates effectively act Two scene 1
Message-Id: <XPOdncPiX7HyhyjNnZ2dnUVZ_gCdnZ2d@supernews.com>
On 11/26/2012 11:34 PM, C.DeRykus wrote:
> The perl compiler needs some help if
> you don't add the parentheses on that
> next_for_ext call.
>
> Easiest correction:
>
> my $newnum = next_for_ext( $ext );
>
> or just add at the top:
>
> use subs q/next_for_ext/
>
>
>
> For more info: perldoc subs
>
Thanks, C , that's got it. I'm invoking it now from a bash script:
$ cat ftp5.pl
#!/usr/bin/perl -w
use strict;
use 5.010;
use Net::FTP;
## usage ./ftp5_pl my_ftp target_dir
#identity and config
my $ident = 'my_ftp.txt';
my ( $config, $domain );
$config = do($ident);
unless ($config) {
die("read error: $!") if $!;
die("parse error: $@") if $@;
}
$domain = $config->{ $ARGV[0] };
die("unknown domain: $ARGV[0]") unless $domain;
#preliminaries at top scope
my $word = "monday";
my %next_for_ext;
# get files
my $path = $ARGV[1];
my @files = <$path*>;
print "@files/n";
#dial up the server
my $ftp = Net::FTP->new( $domain->{domain}, Debug => 1, Passive => 1 )
or die "Can't connect: $@\n";
$ftp->login( $domain->{username}, $domain->{password} )
or die "Couldn't login\n";
$ftp->binary();
# get files from remote root that end in html:
my @remote_files = $ftp->ls();
# print "remote files are: @remote_files\n";
my @matching = map /${word}_(\d+)\.html/, @remote_files;
print "matching is @matching\n";
push( @matching, 0 );
@matching = sort { $a <=> $b } @matching;
my $winner = pop @matching;
my $newnum1 = $winner + 1;
my $html_file = "${word}_$newnum1.html";
print "html file is $html_file\n";
# create file for html stubouts
open( my $fh, '>', $html_file )
or die("Can't open $html_file for writing: $!");
print $fh "<!DOCTYPE html>\n";
print $fh '<html lang="en">' . "\n";
print $fh "<head>\n";
print $fh '<meta charset="utf-8">' . "\n";
print $fh "<title>House Sale</title>\n";
print $fh "</head>\n";
print $fh "<body>\n";
print $fh "<h1>Kitchen Upgrade</h1>\n";
print $fh "<h2>stuff hitting the curb too</h2>\n";
# get ls from remote image directory
$ftp->cwd('/images/') or die "cwd failed $@\n";
my @list = $ftp->ls();
# main control
for my $name (@files) {
print "name is $name\n";
my ($ext) = $name =~ /([^.]*)$/;
print "ext is $ext\n";
my $newnum = next_for_ext ( $ext );
my $new_file2 = "image_$newnum.$ext";
print "newfile is $new_file2\n";
$ftp->put( $name, $new_file2 ) or die "put failed $!\n";
push( @list, $new_file2 );
# unlink($name);
print $fh "<img src=\"/images/$new_file2\"/>\n\n";
print $fh "<p>caption for $new_file2 <\/p>\n";
}
print $fh "</body>\n";
print $fh "</html>\n";
close $fh;
$ftp->cdup() or die "cdup failed $@\n";
$ftp->put($html_file) or die "put failed $@\n";
sub next_for_ext {
my ($ext) = @_;
unless ( exists $next_for_ext{$ext} ) {
my @matching = map /image_(\d+)\.$ext$/, @list;
@matching = sort { $a <=> $b } @matching;
$next_for_ext{$ext} = pop @matching;
}
return ++$next_for_ext{$ext};
}
$ cat bash3.sh
#!/bin/bash
# usage ./bash3.sh dir_from target
set -e
mkdir -p "$2"
find $1 -name "*.JPG" -printf "%p\n$2/%h-%f\n" | xargs -n2 echo cp
find $1 -name "*.JPG" -printf "%p\n$2/%h-%f\n" | xargs -n2 cp
cd $2
ls -l
mogrify -resize 640x480! *
rename 'y/A-Z/a-z/' *.JPG
ls -l
cd ..
./ftp5.pl my_ftp "$2/"
$
http://merrillpjensen.com/monday_4.html
Now I'm pushing to get captions there:
$ cat kitchen1.txt
It takes about 3 minutes to cut up an onion. I take the outside off and
let it sit for ten minutes and then come back to it. Throw them into a
saute pan with olive oil. An unusual start to rosemary potatoes.
Remember that bacon fat you collected during thanksgiving? This is what
you saved it for.
Ground turmeric, rosemary, thyme from debbie's garden, ground coriander
seeds. Crush up the rosemary, or it's a little too much like eating
sagebrush. Rosemary grows everywhere around here. If I'm ever
homeless, I can see myself making a circuit of the houses with a bush
and then cooking it up under the interstate.
Very important for balance: de-glaze with water-wine vinegar mixture.
Where it has browned up due to the Meillard reaction, that's when you
throw it on.
The potatoes have already been boiled, so they don't need a lot of time
in the saute pan. Halve them and place them thusly.
With anything handy, smush them down.
Oops, was programming too long and let it get very brown. What to do?
De-glaze.
Sizzlefitz.
What? I almost forgot to make it new mexican. Jalapenos are great, but
it's like spray painting with a can of mace: if you get any of the oil
from cleaning out the seeds in any of the places that make people scream
when they get maced, you've basically just done it to yourself. Don't
use hot water to try to get it off your eyes! I'm not sure what the
best thing to do is then, but that wasn't the right choice. The other
day I had to turn off the stove and go scream on my lawn with a towel on
my face for a half hour.
When you're done, do the dishes. You want it all clean before
construction for the day begins.
Serve with apricot bbq sauce from Melanie. Size up your work.
$
I think all the parts to do it are on this thread now, and I just have
to put it together.
--
Cal
------------------------------
Date: Tue, 27 Nov 2012 14:16:34 -0700
From: Cal Dershowitz <cal@example.invalid>
Subject: Re: using templates effectively act Two scene 1
Message-Id: <s5Odnc3AN-0vsCjNnZ2dnUVZ_rSdnZ2d@supernews.com>
On 11/26/2012 12:49 AM, Ben Morrow wrote:
>
> Quoth Cal Dershowitz <cal@example.invalid>:
>>
>> # main control
>> for my $name (@files) {
>> print "name is $name\n";
>> my ($ext) = $name =~ /([^.]*)$/;
>> print "ext is $ext\n";
>>
>> @matching = map /image_(\d+)\.$ext$/, @list;
>> print "matching is @matching\n";
>> push( @matching, 1 );
>> @matching = sort { $a <=> $b } @matching;
>> $winner = pop @matching;
>> my $newnum = $winner + 1;
>
> There is no need to redo the search through the list every time. If you
> move this code to a sub you can just remember the next number for each
> $ext, something like
>
> my %next_for_ext;
> sub next_for_ext {
> my ($ext) = @_;
>
> unless (exists $next_for_ext{$ext}) {
> my @matching = map ... @list;
> ...;
> $next_for_ext{$ext} = pop @matching;
> }
>
> return ++$next_for_ext{$ext};
> }
>
> then in the loop just call the sub
>
> my $newnum = next_for_ext $ext;
>
> (and, of course, you don't then have to keep adding the new names to the
> list).
>
>> my $new_file2 = "image_$newnum.$ext";
>
> Give your variables more sensible names. This should be called something
> like $remote_file.
>
>> print "newfile is $new_file2\n";
>> $ftp->put( $name, $new_file2 ) or die "put failed $!\n";
>> push( @list, $new_file2 );
>> # unlink($name);
>>
>> print $fh "<img src=\"/images/$new_file2\"/>\n\n";
>> print $fh "<p>caption for $new_file2 <\/p>\n";
>>
>> }
> <snip>
>
>>
>> So now, how do I write this so that this line:
>>
>> print $fh "<img src=\"/images/$new_file2\"/>\n\n";
>>
>> is instead from a text file, one for each caption, in a template system
>> that mimics what I did here?
>
> The simplest template system is sprintf. It is rather limited, but
> sufficient for what you are doing here. First you create a template
> outside the loop, with printf %-codes in it:
>
> my $template = <<'TEMPLATE';
> <img src="/images/%s"/>
>
> <p>%s</p>
> TEMPLATE
I don't think this is right.
Can't find string terminator "TEMPLATE" anywhere before EOF at ./ftp6.pl
line 65.
$ cat ftp6.pl
#!/usr/bin/perl -w
use strict;
use 5.010;
use Net::FTP;
use HTML::Entities;
## usage ./ftp5_pl my_ftp target_dir
#identity and config
my $ident = 'my_ftp.txt';
my ( $config, $domain );
$config = do($ident);
unless ($config) {
die("read error: $!") if $!;
die("parse error: $@") if $@;
}
$domain = $config->{ $ARGV[0] };
die("unknown domain: $ARGV[0]") unless $domain;
#preliminaries at top scope
my $word = "monday";
my %next_for_ext;
# get files
my $path = $ARGV[1];
my @files = <$path*>;
#dial up the server
my $ftp = Net::FTP->new( $domain->{domain}, Debug => 1, Passive => 1 )
or die "Can't connect: $@\n";
$ftp->login( $domain->{username}, $domain->{password} )
or die "Couldn't login\n";
$ftp->binary();
# get files from remote root that end in html:
my @remote_files = $ftp->ls();
# make unique .html choice
my @matching = map /${word}_(\d+)\.html/, @remote_files;
print "matching is @matching\n";
push( @matching, 0 );
@matching = sort { $a <=> $b } @matching;
my $winner = pop @matching;
my $newnum1 = $winner + 1;
my $html_file = "${word}_$newnum1.html";
print "html file is $html_file\n";
# create file for html stubouts
open( my $fh, '>', $html_file )
or die("Can't open $html_file for writing: $!");
print $fh "<!DOCTYPE html>\n";
print $fh '<html lang="en">' . "\n";
print $fh "<head>\n";
print $fh '<meta charset="utf-8">' . "\n";
print $fh "<title>House Sale</title>\n";
print $fh "</head>\n";
print $fh "<body>\n";
print $fh "<h1>Kitchen Upgrade</h1>\n";
print $fh "<h2>stuff hitting the curb too</h2>\n";
#create template outside loop
my $template = <<'TEMPLATE';
<img src="/images/%s"/>
<p>%s</p>
TEMPLATE
open my $CAPTIONS, "<", "kitchen1.txt" or die "file not there\n";
# get ls from remote image directory
$ftp->cwd('/images/') or die "cwd failed $@\n";
my @list = $ftp->ls();
# main control
for my $name (@files) {
print "name is $name\n";
my ($ext) = $name =~ /([^.]*)$/;
print "ext is $ext\n";
my $newnum = next_for_ext($ext);
my $remote_file = "image_$newnum.$ext";
print "newfile is $remote_file\n";
$ftp->put( $name, $remote_file ) or die "put failed $!\n";
push( @list, $remote_file );
# captions
my $caption = <$CAPTIONS>;
printf $fh $template, $remote_file, $caption;
}
print $fh "</body>\n";
print $fh "</html>\n";
close $fh;
$ftp->cdup() or die "cdup failed $@\n";
$ftp->put($html_file) or die "put failed $@\n";
sub next_for_ext {
my ($ext) = @_;
unless ( exists $next_for_ext{$ext} ) {
my @matching = map /image_(\d+)\.$ext$/, @list;
@matching = sort { $a <=> $b } @matching;
$next_for_ext{$ext} = pop @matching;
}
return ++$next_for_ext{$ext};
}
$
>
> then you open your text file
>
> open my $CAPTIONS, "<", "captions.txt" or die ...;
>
> then in the loop you read a line from the file and fill in the template
>
> my $remote_file = ...;
> my $caption = <$CAPTIONS>;
>
> $ftp->put($name, $remote_file) or ...;
>
> printf $fh $template, $remote_file, $caption;
>
> This assumes, of course, that the captions are listed in the file in the
> order this script will find the files to be uploaded, and that the
> captions in the file are already in HTML. If they might not be in the
> right order you will need a more complicated file format than 'one line
> per file', so you can read it into a hash and look up the caption you
> need. If they aren't in HTML (if they are plain text which might contain
> special characters like <) you need to convert them to HTML using
> something like HTML::Entities.
>
> Ben
>
I guess I don't understand this part, because it doesn't need to worry
about special letters; it needs to wrap the text in tags, which is what
the templates in the badger book do.
--
Cal
------------------------------
Date: Tue, 27 Nov 2012 22:53:36 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: using templates effectively act Two scene 1
Message-Id: <gssfo9-kht.ln1@anubis.morrow.me.uk>
Quoth Cal Dershowitz <cal@example.invalid>:
> On 11/26/2012 12:49 AM, Ben Morrow wrote:
> >
> > my $template = <<'TEMPLATE';
> > <img src="/images/%s"/>
> >
> > <p>%s</p>
> > TEMPLATE
>
> I don't think this is right.
>
> Can't find string terminator "TEMPLATE" anywhere before EOF at ./ftp6.pl
> line 65.
Quoting perl code which contains heredocs is tricky. The terminating
string (TEMPLATE in this case) must be in the first column, so you need
to remove one level of indentation from the whole thing.
> > This assumes, of course, that the captions are listed in the file in the
> > order this script will find the files to be uploaded, and that the
> > captions in the file are already in HTML. If they might not be in the
> > right order you will need a more complicated file format than 'one line
> > per file', so you can read it into a hash and look up the caption you
> > need. If they aren't in HTML (if they are plain text which might contain
> > special characters like <) you need to convert them to HTML using
> > something like HTML::Entities.
>
> I guess I don't understand this part, because it doesn't need to worry
> about special letters; it needs to wrap the text in tags, which is what
> the templates in the badger book do.
If there is any chance of a caption like (say) 'A B&W Photograph' then
you *do* need to worry about 'special letters'.
Ben
------------------------------
Date: Tue, 27 Nov 2012 16:44:55 -0700
From: Cal Dershowitz <cal@example.invalid>
Subject: Re: using templates effectively act Two scene 1
Message-Id: <ptOdnbMIk_vlzSjNnZ2dnUVZ_oidnZ2d@supernews.com>
On 11/27/2012 03:53 PM, Ben Morrow wrote:
>
> Quoth Cal Dershowitz <cal@example.invalid>:
>> On 11/26/2012 12:49 AM, Ben Morrow wrote:
>>>
>>> my $template = <<'TEMPLATE';
>>> <img src="/images/%s"/>
>>>
>>> <p>%s</p>
>>> TEMPLATE
>>
>> I don't think this is right.
>>
>> Can't find string terminator "TEMPLATE" anywhere before EOF at ./ftp6.pl
>> line 65.
Ok. I've made this mistake before.:(
>
> Quoting perl code which contains heredocs is tricky. The terminating
> string (TEMPLATE in this case) must be in the first column, so you need
> to remove one level of indentation from the whole thing.
>
>>> This assumes, of course, that the captions are listed in the file in the
>>> order this script will find the files to be uploaded, and that the
>>> captions in the file are already in HTML. If they might not be in the
>>> right order you will need a more complicated file format than 'one line
>>> per file', so you can read it into a hash and look up the caption you
>>> need. If they aren't in HTML (if they are plain text which might contain
>>> special characters like <) you need to convert them to HTML using
>>> something like HTML::Entities.
>>
>> I guess I don't understand this part, because it doesn't need to worry
>> about special letters; it needs to wrap the text in tags, which is what
>> the templates in the badger book do.
>
> If there is any chance of a caption like (say) 'A B&W Photograph' then
> you *do* need to worry about 'special letters'.
>
> Ben
>
We're really close, and I do think we need to run it through html
entities. Left to my own devices, I'd process the whole file at the
git-go. I don't have enough proficiency with the diamond brackets to
act on it within the loop.
The output shows text under every other image:
http://merrillpjensen.com/monday_5.html
$ cat ftp6.pl
#!/usr/bin/perl -w
use strict;
use 5.010;
use Net::FTP;
use HTML::Entities;
## usage ./ftp5_pl my_ftp target_dir
#identity and config
my $ident = 'my_ftp.txt';
my ( $config, $domain );
$config = do($ident);
unless ($config) {
die("read error: $!") if $!;
die("parse error: $@") if $@;
}
$domain = $config->{ $ARGV[0] };
die("unknown domain: $ARGV[0]") unless $domain;
#preliminaries at top scope
my $word = "monday";
my %next_for_ext;
# get files
my $path = $ARGV[1];
my @files = <$path*>;
#dial up the server
my $ftp = Net::FTP->new( $domain->{domain}, Debug => 1, Passive => 1 )
or die "Can't connect: $@\n";
$ftp->login( $domain->{username}, $domain->{password} )
or die "Couldn't login\n";
$ftp->binary();
# get files from remote root that end in html:
my @remote_files = $ftp->ls();
# make unique .html choice
my @matching = map /${word}_(\d+)\.html/, @remote_files;
print "matching is @matching\n";
push( @matching, 0 );
@matching = sort { $a <=> $b } @matching;
my $winner = pop @matching;
my $newnum1 = $winner + 1;
my $html_file = "${word}_$newnum1.html";
print "html file is $html_file\n";
# create file for html stubouts
open( my $fh, '>', $html_file )
or die("Can't open $html_file for writing: $!");
print $fh "<!DOCTYPE html>\n";
print $fh '<html lang="en">' . "\n";
print $fh "<head>\n";
print $fh '<meta charset="utf-8">' . "\n";
print $fh "<title>House Sale</title>\n";
print $fh "</head>\n";
print $fh "<body>\n";
print $fh "<h1>Kitchen Upgrade</h1>\n";
print $fh "<h2>stuff hitting the curb too</h2>\n";
#create template outside loop
my $template = <<'TEMPLATE';
<img src="/images/%s"/>
<p>%s</p>
TEMPLATE
open my $CAPTIONS, "<", "kitchen1.txt" or die "file not there\n";
# get ls from remote image directory
$ftp->cwd('/images/') or die "cwd failed $@\n";
my @list = $ftp->ls();
# main control
for my $name (@files) {
print "name is $name\n";
my ($ext) = $name =~ /([^.]*)$/;
print "ext is $ext\n";
my $newnum = next_for_ext($ext);
my $remote_file = "image_$newnum.$ext";
print "newfile is $remote_file\n";
$ftp->put( $name, $remote_file ) or die "put failed $!\n";
push( @list, $remote_file );
# captions
my $caption = <$CAPTIONS>;
printf $fh $template, $remote_file, $caption;
}
print $fh "</body>\n";
print $fh "</html>\n";
close $fh;
$ftp->cdup() or die "cdup failed $@\n";
$ftp->put($html_file) or die "put failed $@\n";
sub next_for_ext {
my ($ext) = @_;
unless ( exists $next_for_ext{$ext} ) {
my @matching = map /image_(\d+)\.$ext$/, @list;
@matching = sort { $a <=> $b } @matching;
$next_for_ext{$ext} = pop @matching;
}
return ++$next_for_ext{$ext};
}
$
--
Cal
------------------------------
Date: Tue, 27 Nov 2012 18:12:42 -0700
From: Cal Dershowitz <cal@example.invalid>
Subject: Re: using templates effectively act Two scene 1
Message-Id: <NLudnafR5q2X-CjNnZ2dnUVZ_oadnZ2d@supernews.com>
On 11/27/2012 04:44 PM, Cal Dershowitz wrote:
> On 11/27/2012 03:53 PM, Ben Morrow wrote:
>>
>> Quoth Cal Dershowitz <cal@example.invalid>:
>>> On 11/26/2012 12:49 AM, Ben Morrow wrote:
>>>>
>>>> my $template = <<'TEMPLATE';
>>>> <img src="/images/%s"/>
>>>>
>>>> <p>%s</p>
>>>> TEMPLATE
>>>
>>> I don't think this is right.
>>>
>>> Can't find string terminator "TEMPLATE" anywhere before EOF at ./ftp6.pl
>>> line 65.
>
> Ok. I've made this mistake before.:(
>>
>> Quoting perl code which contains heredocs is tricky. The terminating
>> string (TEMPLATE in this case) must be in the first column, so you need
>> to remove one level of indentation from the whole thing.
>>
>>>> This assumes, of course, that the captions are listed in the file in
>>>> the
>>>> order this script will find the files to be uploaded, and that the
>>>> captions in the file are already in HTML. If they might not be in the
>>>> right order you will need a more complicated file format than 'one line
>>>> per file', so you can read it into a hash and look up the caption you
>>>> need. If they aren't in HTML (if they are plain text which might
>>>> contain
>>>> special characters like <) you need to convert them to HTML using
>>>> something like HTML::Entities.
>>>
>>> I guess I don't understand this part, because it doesn't need to worry
>>> about special letters; it needs to wrap the text in tags, which is what
>>> the templates in the badger book do.
>>
>> If there is any chance of a caption like (say) 'A B&W Photograph' then
>> you *do* need to worry about 'special letters'.
>>
>> Ben
>>
>
> We're really close, and I do think we need to run it through html
> entities. Left to my own devices, I'd process the whole file at the
> git-go. I don't have enough proficiency with the diamond brackets to
> act on it within the loop.
>
> The output shows text under every other image:
>
> http://merrillpjensen.com/monday_5.html
It's skipping captions, and it doesn't seem to be related to special
characters:
Net::FTP=GLOB(0x9e3aff0)<<< 150 Opening BINARY mode data connection for
file list
Net::FTP=GLOB(0x9e3aff0)<<< 226 Transfer complete
name is target12/27-img_0009.jpg
ext is jpg
newfile is image_281.jpg
Net::FTP=GLOB(0x9e3aff0)>>> ALLO 144654
Net::FTP=GLOB(0x9e3aff0)<<< 200 ALLO command successful
Net::FTP=GLOB(0x9e3aff0)>>> PASV
Net::FTP=GLOB(0x9e3aff0)<<< 227 Entering Passive Mode
(74,208,244,112,253,71).
Net::FTP=GLOB(0x9e3aff0)>>> STOR image_281.jpg
Net::FTP=GLOB(0x9e3aff0)<<< 150 Opening BINARY mode data connection for
image_281.jpg
Net::FTP=GLOB(0x9e3aff0)<<< 226 Transfer complete
It takes about 13 minutes to cut up an onion & not much fuss, if you
take your time. My eyes are dryest in the morning, so rushing the
scetioning of the onion is a self-correcting process. I take the
outside off and let it sit for ten minutes and then come back to it.
Throw them into a saute pan with olive oil. An unusual start to
rosemary potatoes.
It takes about 13 minutes to cut up an onion & not much fuss, if you
take your time. My eyes are dryest in the morning, so rushing the
scetioning of the onion is a self-correcting process. I take the
outside off and let it sit for ten minutes and then come back to it.
Throw them into a saute pan with olive oil. An unusual start to
rosemary potatoes.
name is target12/27-img_0010.jpg
ext is jpg
newfile is image_282.jpg
Net::FTP=GLOB(0x9e3aff0)>>> ALLO 134934
Net::FTP=GLOB(0x9e3aff0)<<< 200 ALLO command successful
Net::FTP=GLOB(0x9e3aff0)>>> PASV
Net::FTP=GLOB(0x9e3aff0)<<< 227 Entering Passive Mode
(74,208,244,112,246,23).
Net::FTP=GLOB(0x9e3aff0)>>> STOR image_282.jpg
Net::FTP=GLOB(0x9e3aff0)<<< 150 Opening BINARY mode data connection for
image_282.jpg
Net::FTP=GLOB(0x9e3aff0)<<< 226 Transfer complete
name is target12/27-img_0011.jpg
ext is jpg
newfile is image_283.jpg
> $
I've got HTML::Entities working now, which had to happen before this
script could be considered moderately-robust.
The on-again off again thing reminds me of newline problems, but I can't
sniff it out.
$ cat ftp6.pl
#!/usr/bin/perl -w
use strict;
use 5.010;
use Net::FTP;
use HTML::Entities;
## usage ./ftp5_pl my_ftp target_dir
#identity and config
my $ident = 'my_ftp.txt';
my ( $config, $domain );
$config = do($ident);
unless ($config) {
die("read error: $!") if $!;
die("parse error: $@") if $@;
}
$domain = $config->{ $ARGV[0] };
die("unknown domain: $ARGV[0]") unless $domain;
#preliminaries at top scope
my $word = "monday";
my %next_for_ext;
# get files
my $path = $ARGV[1];
my @files = <$path*>;
#dial up the server
my $ftp = Net::FTP->new( $domain->{domain}, Debug => 1, Passive => 1 )
or die "Can't connect: $@\n";
$ftp->login( $domain->{username}, $domain->{password} )
or die "Couldn't login\n";
$ftp->binary();
# get files from remote root that end in html:
my @remote_files = $ftp->ls();
# make unique .html choice
my @matching = map /${word}_(\d+)\.html/, @remote_files;
print "matching is @matching\n";
push( @matching, 0 );
@matching = sort { $a <=> $b } @matching;
my $winner = pop @matching;
my $newnum1 = $winner + 1;
my $html_file = "${word}_$newnum1.html";
print "html file is $html_file\n";
# create file for html stubouts
open( my $fh, '>', $html_file )
or die("Can't open $html_file for writing: $!");
print $fh "<!DOCTYPE html>\n";
print $fh '<html lang="en">' . "\n";
print $fh "<head>\n";
print $fh '<meta charset="utf-8">' . "\n";
print $fh "<title>House Sale</title>\n";
print $fh "</head>\n";
print $fh "<body>\n";
print $fh "<h1>Kitchen Upgrade</h1>\n";
print $fh "<h2>stuff hitting the curb too</h2>\n";
#create template outside loop
my $template = <<'TEMPLATE';
<img src="/images/%s"/>
<p>%s</p>
TEMPLATE
open my $CAPTIONS, "<", "kitchen1.txt" or die "file not there\n";
# get ls from remote image directory
$ftp->cwd('/images/') or die "cwd failed $@\n";
my @list = $ftp->ls();
# main control
for my $name (@files) {
print "name is $name\n";
my ($ext) = $name =~ /([^.]*)$/;
print "ext is $ext\n";
my $newnum = next_for_ext($ext);
my $remote_file = "image_$newnum.$ext";
print "newfile is $remote_file\n";
$ftp->put( $name, $remote_file ) or die "put failed $!\n";
push( @list, $remote_file );
# captions
my $caption = <$CAPTIONS>;
print "$caption\n";
$caption = encode_entities ( $caption);
print "$caption\n";
printf $fh $template, $remote_file, $caption;
}
print $fh "</body>\n";
print $fh "</html>\n";
close $fh;
$ftp->cdup() or die "cdup failed $@\n";
$ftp->put($html_file) or die "put failed $@\n";
sub next_for_ext {
my ($ext) = @_;
unless ( exists $next_for_ext{$ext} ) {
my @matching = map /image_(\d+)\.$ext$/, @list;
@matching = sort { $a <=> $b } @matching;
$next_for_ext{$ext} = pop @matching;
}
return ++$next_for_ext{$ext};
}
$
I tried putting a newline after the %s in the template, and that didn't
work.
--
Cal
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V11 Issue 3826
***************************************