[32458] in Perl-Users-Digest
Perl-Users Digest, Issue: 3725 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Jun 26 18:09:30 2012
Date: Tue, 26 Jun 2012 15:09:07 -0700 (PDT)
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, 26 Jun 2012 Volume: 11 Number: 3725
Today's topics:
Re: "use" inside of a subroutine <jwcarlton@gmail.com>
Re: "use" inside of a subroutine <ben@morrow.me.uk>
Re: Error Handling in Net::SSH::Perl <glex_no-spam@qwest-spam-no.invalid>
Re: File::glob pattern matching <marc.girod@gmail.com>
Re: File::glob pattern matching <ben@morrow.me.uk>
Re: File::glob pattern matching <marc.girod@gmail.com>
How to call the standard Exporter::import routine <klaus03@gmail.com>
Re: How to call the standard Exporter::import routine <ben@morrow.me.uk>
Re: How to call the standard Exporter::import routine <klaus03@gmail.com>
Re: Question about a variable in list-context <justin.1203@purestblue.com>
Re: Question about a variable in list-context <jwkrahn@example.com>
Re: Question about a variable in list-context <willem@toad.stack.nl>
Re: question concerning pipes and large strings <glex_no-spam@qwest-spam-no.invalid>
Taking a part a Perl Object. <rodbass63@gmail.com>
Re: Taking a part a Perl Object. <ben@morrow.me.uk>
Re: Taking a part a Perl Object. <marc.girod@gmail.com>
Re: Taking a part a Perl Object. <rweikusat@mssgmbh.com>
Re: Taking a part a Perl Object. <rodbass63@gmail.com>
using HTML::Template effectively <cal@example.invalid>
Re: using HTML::Template effectively <glex_no-spam@qwest-spam-no.invalid>
Re: using HTML::Template effectively <ben@morrow.me.uk>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sat, 23 Jun 2012 14:22:35 -0700 (PDT)
From: Jason C <jwcarlton@gmail.com>
Subject: Re: "use" inside of a subroutine
Message-Id: <02c43e33-4f5d-4993-b308-24b667dfb33a@googlegroups.com>
On Saturday, June 23, 2012 10:58:36 AM UTC-4, Ben Morrow wrote:
> Well, perhaps. If you're worried about load times you would be better
> off switching from CGI (if that's what you're using) to FastCGI or
> something else which uses a persistent Perl interpreter.
I used to use Speedy CGI, but then the persistent interpreter was slowing down the server. Perl speed really isn't all that critical, though; I'm just in the middle of a re-design, so figured that I should make everything as fast and smooth as I can.
> > sub copyFile {
> > require File::Copy;
> > File::Copy->import();
> >
> > copy(...) or die ...;
> > }
>
> That will work, but the brackets are mandatory. If you leave them off,
> perl will not recognise 'copy' as a function, because it was not
> imported at compile time.
Ben mentioned earlier that the brackets are mandatory, but now I realize that I'm not sure which brackets you mean.
Do you mean the round brackets (parentheses)?
copy("whatever", "whatever") or die ...;
Or something else?
------------------------------
Date: Sat, 23 Jun 2012 23:22:57 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: "use" inside of a subroutine
Message-Id: <17shb9-khg.ln1@anubis.morrow.me.uk>
Quoth Jason C <jwcarlton@gmail.com>:
> On Saturday, June 23, 2012 10:58:36 AM UTC-4, Ben Morrow wrote:
> > Well, perhaps. If you're worried about load times you would be better
> > off switching from CGI (if that's what you're using) to FastCGI or
> > something else which uses a persistent Perl interpreter.
>
> I used to use Speedy CGI, but then the persistent interpreter was
> slowing down the server. Perl speed really isn't all that critical,
> though; I'm just in the middle of a re-design, so figured that I should
> make everything as fast and smooth as I can.
The first rule of optimisation is 'DON'T'. Get the system working and
correct the simple and obvious way first, then worry about speeding it
up if it's actually slow enough to be a problem.
> > > sub copyFile {
> > > require File::Copy;
> > > File::Copy->import();
> > >
> > > copy(...) or die ...;
> > > }
> >
> > That will work, but the brackets are mandatory. If you leave them off,
> > perl will not recognise 'copy' as a function, because it was not
> > imported at compile time.
>
> Ben mentioned earlier that the brackets are mandatory, but now I realize
> that I'm not sure which brackets you mean.
>
> Do you mean the round brackets (parentheses)?
Yes. If you omit them, and write
copy "src", "dest" or die "...";
Perl will only recognise this as a function call if the function exists
at compile time. Since the import doesn't happen until runtime, you will
get a 'strict "subs"' error.
Ben
------------------------------
Date: Mon, 25 Jun 2012 11:02:05 -0500
From: "J. Gleixner" <glex_no-spam@qwest-spam-no.invalid>
Subject: Re: Error Handling in Net::SSH::Perl
Message-Id: <4fe88b7d$0$52261$815e3792@news.qwest.net>
On 06/22/12 14:25, dagomakoa wrote:
> my $ssh;
> eval {
> $ssh = Net::SSH::Perl->connect( ... );
> };
> if ($@) {
> warn "Connect failed: $@\n";
> }
>
> It does not work with me, should we remove the option -w on the shebang or remove use warnings / use strict?
>
>
You generally don't call 'connect' when using Net::SSH::Perl.
When you instantiate the class, using new(), it will 'connect' or 'die'
if the socket connection fails.
use Net::SSH::Perl;
eval {
my $ssh = Net::SSH::Perl->new(... );
$ssh->login( ... );
my ( $o, $e, $ev ) = $ssh->cmd( ... );
};
die "SSH Failed: $@" if $@;
perldoc Net::SSH::Perl
------------------------------
Date: Sat, 23 Jun 2012 13:39:14 -0700 (PDT)
From: Marc Girod <marc.girod@gmail.com>
Subject: Re: File::glob pattern matching
Message-Id: <64bbf52a-2017-442c-a087-48c487f9fa2d@q29g2000vby.googlegroups.com>
On Jun 23, 7:37=A0pm, Marc Girod <marc.gi...@gmail.com> wrote:
> Now, it is correct that [[:digit::]] doesn't work (and should?)
tmp> ls aa[[:digit:]][[:digit:]]bb
aa12bb
tmp> ls aa[[:digit:]]{2}bb
ls: cannot access aa[[:digit:]]{2}bb: No such file or directory
I meant that [[:digit:]] works with ls, but {2} doesn't
Marc
------------------------------
Date: Sat, 23 Jun 2012 21:40:11 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: File::glob pattern matching
Message-Id: <b6mhb9-q9f.ln1@anubis.morrow.me.uk>
Quoth Marc Girod <marc.girod@gmail.com>:
> On Jun 22, 10:33 pm, Ben Morrow <b...@morrow.me.uk> wrote:
>
> > > or [0-9]{2}, or [[:digit:]]{2}.
> >
> > The OP is using globs, not regexes.
>
> But {2} is explicitly supported in File::Glob, and also works in
> CORE::glob:
>
> tmp> touch aa12bb
> tmp> perl -le '@f=glob(q(aa[0-9]{2}bb));print for @f'
> aa12bb
It doesn't do what you think it does. Try aa13bb.
> Now, it is correct that [[:digit::]] doesn't work (and should?)
A strict reading of SUSv4 actually says it should, to my surprise, but
traditional shells didn't support it. (On my system the only shell which
does is bash, as might be expected.)
Ben
------------------------------
Date: Sun, 24 Jun 2012 02:14:10 -0700 (PDT)
From: Marc Girod <marc.girod@gmail.com>
Subject: Re: File::glob pattern matching
Message-Id: <8a127130-fac6-4eb6-a2da-81465ab3dfb8@e20g2000vbm.googlegroups.com>
On Jun 23, 9:40=A0pm, Ben Morrow <b...@morrow.me.uk> wrote:
> It doesn't do what you think it does. Try aa13bb.
Indeed:
tmp> perl -le '@f=3Dglob(q(aa[0-9]{2,3}bb));print for @f'
aa12bb
aa13bb
I didn't read with enough attention... and I missed the fact that
the following was ambiguous, and only explained later:
{} Multiple pattern
> A strict reading of SUSv4 actually says it should, to my surprise,
> but traditional shells didn't support it. (On my system the only
> shell which does is bash, as might be expected.)
That's the only one I tried...
Thanks.
Marc
------------------------------
Date: Sun, 24 Jun 2012 10:14:05 -0700 (PDT)
From: Klaus <klaus03@gmail.com>
Subject: How to call the standard Exporter::import routine
Message-Id: <23c00d6d-c06c-4f0f-a7d9-b7f40eaf0923@z19g2000vbe.googlegroups.com>
Hi everybody,
I am trying to write a custom import routine for one of my modules,
but I am having trouble calling the standard Exporter::import routine
from inside my custom import routine.
here is my code:
==================
package XML::Reader;
use strict;
use warnings;
use Carp;
require Exporter;
our @ISA = qw(Exporter);
our %EXPORT_TAGS = ( all => [ qw(slurp_xml) ] );
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
our @EXPORT = qw();
our $VERSION = '0.40';
my $use_module;
sub import {
my $calling_module = shift;
my @plist;
for my $sub (@_) {
if ($sub eq 'XML::Parser' or $sub eq 'XML::Parsepp') {
if (defined $use_module) {
die "Duplicate module ('$use_module' and '$sub')";
}
$use_module = $sub;
}
else {
push @plist, $sub;
}
}
unless (defined $use_module) {
$use_module = 'XML::Parser';
}
if ($use_module eq 'XML::Parser') {
require XML::Parser;
}
elsif ($use_module eq 'XML::Parsepp') {
require XML::Parsepp;
}
else {
die "Can't identify use_module = '$use_module'";
}
no strict 'refs';
my ($package, $file, $line) = caller;
for my $sub (@plist) {
my $found = 0;
for my $ex_ok (@EXPORT_OK) {
if ($sub eq $ex_ok) {
$found = 1;
last;
}
}
if ($found) {
*{$package."::$sub"} = \&$sub;
}
else {
die "Requested invalid subroutine '$sub'";
}
}
}
===============
My program basically works, but I am not happy about the following
code section
===============
no strict 'refs';
my ($package, $file, $line) = caller;
for my $sub (@plist) {
my $found = 0;
for my $ex_ok (@EXPORT_OK) {
if ($sub eq $ex_ok) {
$found = 1;
last;
}
}
if ($found) {
*{$package."::$sub"} = \&$sub;
}
else {
die "Requested invalid subroutine '$sub'";
}
}
===============
That part of the code should normally be handled by
Exoprter::import(), but how do I call it ?
------------------------------
Date: Sun, 24 Jun 2012 20:03:17 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: How to call the standard Exporter::import routine
Message-Id: <ls4kb9-o8j1.ln1@anubis.morrow.me.uk>
Quoth Klaus <klaus03@gmail.com>:
> Hi everybody,
>
> I am trying to write a custom import routine for one of my modules,
> but I am having trouble calling the standard Exporter::import routine
> from inside my custom import routine.
See Exporter->export_to_level.
Ben
------------------------------
Date: Mon, 25 Jun 2012 01:15:16 -0700 (PDT)
From: Klaus <klaus03@gmail.com>
Subject: Re: How to call the standard Exporter::import routine
Message-Id: <4d9fd89a-c0e1-4561-937a-7b0adbfc2ac7@l32g2000yqc.googlegroups.com>
On 24 juin, 21:03, Ben Morrow <b...@morrow.me.uk> wrote:
> Quoth Klaus <klau...@gmail.com>:
>
> > Hi everybody,
>
> > I am trying to write a custom import routine for one of my modules,
> > but I am having trouble calling the standard Exporter::import routine
> > from inside my custom import routine.
>
> See Exporter->export_to_level.
Thanks, works perfectly.
-- Klaus
------------------------------
Date: Mon, 25 Jun 2012 12:22:27 +0100
From: Justin C <justin.1203@purestblue.com>
Subject: Re: Question about a variable in list-context
Message-Id: <j8ulb9-npd.ln1@zem.masonsmusic.co.uk>
On 2012-06-22, Willem <willem@toad.stack.nl> wrote:
>
> Look at:
>
> @id = $input =~ /^(.)(.)(.)/;
Eccentrica Gallumbits!
Justin.
--
Justin C, by the sea.
------------------------------
Date: Mon, 25 Jun 2012 05:29:40 -0700
From: "John W. Krahn" <jwkrahn@example.com>
Subject: Re: Question about a variable in list-context
Message-Id: <VQYFr.36605$l_1.23766@newsfe21.iad>
Willem wrote:
> Markus Hutmacher wrote:
> )
> ) in a recent thread in this group I found the following line of code:
> )
> ) ($id) = $input =~ /^([^\t]+)\t/;
> )
> ) I understand that ($id) means that $id is used in list-context which
> ) means that the part of $input which matches [^\t]+ is assigned to $id.
> ) I understand as well that the same line of code without the list-context
> ) will assign a 0 or 1 to $id depending on "matches" or "matches not".
> ) But I don't understand to which part of the code the list-context refers.
> )
> ) In other words: what is the list in the expression
> ) $input =~ /^([^\t]+)\t/
>
> The list-context is applied to the =~ operator, so the list is the return
> value of the =~ operator.
The binding operator (=~) binds the variable ($input) to the match
operator (/^([^\t]+)\t/). It does not return a value. The value is
returned from the match operator, specifically in this case, the
capturing parentheses in the pattern.
John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein
------------------------------
Date: Mon, 25 Jun 2012 16:57:33 +0000 (UTC)
From: Willem <willem@toad.stack.nl>
Subject: Re: Question about a variable in list-context
Message-Id: <slrnjuh63t.ajs.willem@toad.stack.nl>
John W. Krahn wrote:
) Willem wrote:
)> The list-context is applied to the =~ operator, so the list is the return
)> value of the =~ operator.
)
) The binding operator (=~) binds the variable ($input) to the match
) operator (/^([^\t]+)\t/). It does not return a value. The value is
) returned from the match operator, specifically in this case, the
) capturing parentheses in the pattern.
I stand corrected.
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: Mon, 25 Jun 2012 10:35:33 -0500
From: "J. Gleixner" <glex_no-spam@qwest-spam-no.invalid>
Subject: Re: question concerning pipes and large strings
Message-Id: <4fe88545$0$9080$815e3792@news.qwest.net>
On 06/20/12 09:17, math math wrote:
> On Tuesday, June 19, 2012 5:47:28 PM UTC+1, J. Gleixner wrote:
>> On 06/19/12 10:50, math math wrote:
>>> Hi,
>>>
>>> I have a file with two tab delimited fields. First field is an ID, the second field is a large string (up to hundreds of millions of characters). The file may have many lines.
>>>
>>> I would like to sort the file on the first (ID) field and after this sorting, merge the second fields (i.e. remove the new lines), so that I get a single line with many hundreds of lines that are in the same order appended to each other as their alphabetically sorted IDs.
>>>
>>> Is there a way to do that in PERL without reading the whole file into memory?
>>>
>>> Thanks
>>
>>
>> What have you tried?
>>
>
> Sorry, I should have pasted my trial code first, I did it right after my initial post, but it got here a bit late. Please see my second post for details (there is an ordering mistake in the snipper, "chomp" should be below the line with the "split")
>
>> Probably sorting it first would make it much easier:
>>
>> man sort
>>
> Indeed, I tried sort first, it works, it is more of a scalability question really.
>
>> These should help with the rest:
>>
>> perldoc -f open
>> perldoc -f chomp
>
> Thanks.
Possibly another option is to parse the huge file into many different
files, named by the key. So you'd have files: 001, 002, 003, etc. or
maybe 001.txt, 002.txt, etc. and they'd each contain the contents
of the line of info from the huge file. Sort the file names and
concat the files.
------------------------------
Date: Mon, 25 Jun 2012 16:12:57 -0700 (PDT)
From: Nene <rodbass63@gmail.com>
Subject: Taking a part a Perl Object.
Message-Id: <a60a51e8-82e9-4468-a84e-a20be31c1aa6@t20g2000yqn.googlegroups.com>
Hi,
I've been tasked to learn the behavior of a complicated homegrown Perl
module.
Are there any resources on how to to take a part a perl module, in
particular the 'new' object?
I want to learn the guts of the 'new' object, would the perl debugger
be a good start?
Nene
------------------------------
Date: Tue, 26 Jun 2012 00:49:14 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Taking a part a Perl Object.
Message-Id: <q0anb9-b6o1.ln1@anubis.morrow.me.uk>
Quoth Nene <rodbass63@gmail.com>:
> I've been tasked to learn the behavior of a complicated homegrown Perl
> module.
I suppose it's futile to ask if the module is documented or commented
properly? If there is a test suite, so you can get some idea of how the
author intended it to be used? Do you at least have examples of programs
which successfully use the module?
> Are there any resources on how to to take a part a perl module, in
> particular the 'new' object?
'new' is usually a method.
> I want to learn the guts of the 'new' object, would the perl debugger
> be a good start?
Reading the source, carefully, would be a good start. Once you think you
have an idea how the module works, write little programs that call a
function or two, and see if they do what you expected: forming theories
which you can then test is a much more efficient way of finding things
out than trying things aimlessly. If necessary you can insert 'warn'
statements (or something else appropriate) here and there to print out
important variables, and to see where the control goes.
Write things down as you go, so you don't forget them. If there isn't
any documentation, it would be a good idea to start writing it: the
exercise should help you form a clearer picture of what the code is
trying to accomplish, and how. Add comments to any bit of code that are
particularly obscure.
Personally I don't find the Perl debugger terribly useful. However, it
may be more so when trying to decipher code you don't yet understand.
Ben
------------------------------
Date: Tue, 26 Jun 2012 00:49:41 -0700 (PDT)
From: Marc Girod <marc.girod@gmail.com>
Subject: Re: Taking a part a Perl Object.
Message-Id: <09f8b1dc-60fd-404d-8da8-9e76b90576ee@f30g2000vbz.googlegroups.com>
On Jun 26, 12:49=A0am, Ben Morrow <b...@morrow.me.uk> wrote:
> Personally I don't find the Perl debugger terribly useful. However, it
> may be more so when trying to decipher code you don't yet understand.
On the contrary, I use the debugger a lot. I find it useful to place
breakpoints, inspect the trace and the values of the variables.
Possibly use only the 'action' function (usually print, but sometimes
modify).
Marc
------------------------------
Date: Tue, 26 Jun 2012 10:26:21 +0100
From: Rainer Weikusat <rweikusat@mssgmbh.com>
Subject: Re: Taking a part a Perl Object.
Message-Id: <87zk7qjuf6.fsf@sapphire.mobileactivedefense.com>
Ben Morrow <ben@morrow.me.uk> writes:
> Quoth Nene <rodbass63@gmail.com>:
[...]
>> I want to learn the guts of the 'new' object, would the perl debugger
>> be a good start?
>
> Reading the source, carefully, would be a good start. Once you think you
> have an idea how the module works, write little programs that call a
> function or two, and see if they do what you expected: forming theories
> which you can then test is a much more efficient way of finding things
> out than trying things aimlessly. If necessary you can insert 'warn'
> statements (or something else appropriate) here and there to print out
> important variables, and to see where the control goes.
I suggest the same thing: If you don't understand the actual control
flow, try to catch it with subsequently more specific "does it pass
here?" ad hoc diagnostics, possibly printing other values of interest
while having the source code loaded in a reasonable editor.
In my experience, that's a lot faster and more convenient than trying
to single-step through large bodies of code. You never want to know
everything. It also works in situations where 'stop the world to
inspect its state' isn't possible.
------------------------------
Date: Tue, 26 Jun 2012 14:24:07 -0700 (PDT)
From: Nene <rodbass63@gmail.com>
Subject: Re: Taking a part a Perl Object.
Message-Id: <fd721a33-d816-45f3-b09e-c39aef40c8c6@t20g2000yqn.googlegroups.com>
On Jun 25, 7:49=A0pm, Ben Morrow <b...@morrow.me.uk> wrote:
> Quoth Nene <rodbas...@gmail.com>:
>
> > I've been tasked to learn the behavior of a complicated homegrown Perl
> > module.
>
> I suppose it's futile to ask if the module is documented or commented
> properly?
It is documented but very vaguely.
> If there is a test suite, so you can get some idea of how the
> author intended it to be used?
Yes, the author has test scripts which I made copies of and made them
smaller tests. Once I understand the rudiments
of the module ( a REST API, btw ). I will use his personal tests.
> Do you at least have examples of programs
> which successfully use the module?
>
> > Are there any resources on how to to take a part a perl module, in
> > particular the 'new' object?
>
> 'new' is usually a method.
Thank you clarifying that. What I have learned from the new method,
correct me if I am wrong, that it is the vehicle to obtain
the data required for the purpose of the module and the methods are
there to do whatever you want with the data.
>
> > I want to learn the guts of the 'new' object, would the perl debugger
> > be a good start?
>
> Reading the source, carefully, would be a good start. Once you think you
> have an idea how the module works, write little programs that call a
> function or two, and see if they do what you expected: forming theories
> which you can then test is a much more efficient way of finding things
> out than trying things aimlessly. If necessary you can insert 'warn'
> statements (or something else appropriate) here and there to print out
> important variables, and to see where the control goes.
Thank you so much for this advice. I have inserted print statements
all over the place and what I have learned that
90% of all the data are obtained through hash references.
>
> Write things down as you go, so you don't forget them. If there isn't
> any documentation, it would be a good idea to start writing it:
I made a copy of the pm file with the print statements and added
comments to the file, this file will be my reference file.
Every time I fire up the tests, it will print comments, sort of like a
review each time I use it.
> exercise should help you form a clearer picture of what the code is
> trying to accomplish, and how. Add comments to any bit of code that are
> particularly obscure.
>
> Personally I don't find the Perl debugger terribly useful. However, it
> may be more so when trying to decipher code you don't yet understand.
>
> Ben
------------------------------
Date: Tue, 26 Jun 2012 13:31:01 -0600
From: Cal Dershowitz <cal@example.invalid>
Subject: using HTML::Template effectively
Message-Id: <ptudnb44PN5rkHfSnZ2dnUVZ_rednZ2d@supernews.com>
I've been using perl scripts to mechanize oft-repeated tasks for my
website. I tell my friend what I'm doing and he says, "oh you need
HTML::Template." What I hope to do with this thread is re-write it with
modules, as well as providing a utility to make my choice of filenames
less obvious, but still within the organizational scheme that will be
this site.
First, though, I want to go back just a bit and clear up my last few
difficulties with what has gone before:
#!/usr/bin/perl -w
use strict;
use 5.010;
use Net::FTP;
my $domain = '';
my $username = '';
my $password = '';
my $word = "ceiling";
my $ftp = Net::FTP->new( $domain, Debug => 1, Passive => 1 )
or die "Can't connect: $@\n";
$ftp->login( $username, $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 /ceiling_(\d+)\.html/, @remote_files;
print "matching is @matching\n";
push( @matching, 1 );
@matching = sort { $a <=> $b } @matching;
my $winner = pop @matching;
my $newnum1 = $winner + 1;
my $html_file = "ceiling_$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 "<html>\n";
print $fh "<head>\n";
print $fh "<title>Lutherhaven Renovation</title>\n";
print $fh "</head>\n";
print $fh "<body bgcolor=white>\n";
print $fh "<h1>Basement Ceiling Materials</h1>\n";
# get files from Desktop/images/
my $path = '/home/dan/Desktop/upload_luther/';
my @files = <$path*>;
# 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";
@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;
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";
}
close $fh;
$ftp->cdup() or die "cdup failed $@\n";
$ftp->put($html_file) or die "put failed $@\n";
my @r = $ftp->ls();
print "@r\n";
Q1) I continue to have difficulties combining the syntax of lexical
variables and RE's:
my $word = "ceiling";
my @matching = map /ceiling_(\d+)\.html/, @remote_files;
my $html_file = "ceiling_$newnum1.html";
How would I write those last 2 lines of code with $word instead of
"ceiling?"
Q2) There's a huge amount of reading out there on this topic, which I'm
grateful for. Anyone have a link that they think is particularly
effective at studying up on this?
Q3) This one's a little out there. Right after the unlink statement up
there, or better yet, just before the close statement, what would happen
if you wrote:
open( my $gh, '>', ceiling1.pl) or die("Can't open ceiling1.pl for
writing: $!");
where the name of the script itself is ceiling1.pl,and then attempted to
write from $gh to $fh linewise? I know it's illegal in C, so I don't
want to just try it and see what happens. It's too much work cleaning
up after the nasal demons when they get on your keyboard.
Thanks for your comment,
--
Cal
------------------------------
Date: Tue, 26 Jun 2012 15:25:29 -0500
From: "J. Gleixner" <glex_no-spam@qwest-spam-no.invalid>
Subject: Re: using HTML::Template effectively
Message-Id: <4fea1aba$0$73615$815e3792@news.qwest.net>
On 06/26/12 14:31, Cal Dershowitz wrote:
> I've been using perl scripts to mechanize oft-repeated tasks for my
> website. I tell my friend what I'm doing and he says, "oh you need
> HTML::Template."
Have your friend help you first and do we need to know what your friend
says??..
>What I hope to do with this thread is re-write it with
> modules, as well as providing a utility to make my choice of filenames
> less obvious, but still within the organizational scheme that will be
> this site.
>
> First, though, I want to go back just a bit and clear up my last few
> difficulties with what has gone before:
First though, this has nothing to do with the Subject!!!!!!!! Folks
who know a lot about HTML::Template just wasted their time reading
your post.
>
> Q1) I continue to have difficulties combining the syntax of lexical
> variables and RE's:
> my $word = "ceiling";
> my @matching = map /ceiling_(\d+)\.html/, @remote_files;
> my $html_file = "ceiling_$newnum1.html";
>
> How would I write those last 2 lines of code with $word instead of
> "ceiling?"
This is all you needed to post. Code showing the issue and
your question.
Use ${word} where you want it to occur, instead of $word.
my $html_file = "${word}_$newnum1.html";
Since you could have a variable $word_, you need to show
that you really only want it to use $word.
>
> Q2) There's a huge amount of reading out there on this topic, which I'm
> grateful for. Anyone have a link that they think is particularly
> effective at studying up on this?
>
> Q3) This one's a little out there. Right after the unlink statement up
> there, or better yet, just before the close statement, what would happen
> if you wrote:
>
> open( my $gh, '>', ceiling1.pl) or die("Can't open ceiling1.pl for
> writing: $!");
No need for () there..
die "Can't open ceiling.pl for writing: $!"
>
> where the name of the script itself is ceiling1.pl,and then attempted to
> write from $gh to $fh linewise? I know it's illegal in C, so I don't
> want to just try it and see what happens.
Bye bye ceiling.pl..
Think about it.....
What do you think '>' will do?
Why do you think you want do to that?
Actually, I'm not sure what you're asking.. How do you 'write from $gh
to $fh'?
You can answer this yourself. Be sure to have a copy of ceiling.pl
first though.
> It's too much work cleaning up...
Do you think anyone will continue reading your post if put in
more garbage???..
------------------------------
Date: Tue, 26 Jun 2012 21:42:09 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: using HTML::Template effectively
Message-Id: <1ejpb9-8l32.ln1@anubis.morrow.me.uk>
Quoth Cal Dershowitz <cal@example.invalid>:
>
> Q1) I continue to have difficulties combining the syntax of lexical
> variables and RE's:
> my $word = "ceiling";
> my @matching = map /ceiling_(\d+)\.html/, @remote_files;
> my $html_file = "ceiling_$newnum1.html";
>
> How would I write those last 2 lines of code with $word instead of
> "ceiling?"
my @matching = map /${word}_(\d+)\.html/, @remote_files;
my $html_file = "${ceiling}_${newnum1}.html";
When you are interpolating a variable into a string (a pattern is just a
funny kind of string), and the character following the variable name
could legitimately be part of the name, you need the braces to
disambiguate. If you find it easier, you could get into the habit of
always putting them in: they never do any harm, and sometimes make
things clearer even when they aren't strictly ambiguous.
> Q2) There's a huge amount of reading out there on this topic, which I'm
> grateful for. Anyone have a link that they think is particularly
> effective at studying up on this?
I would say you want to buy and read the book 'Learning Perl'
(1-4493-0358-7; the reference in perlfaq2 is for the previous edition). I
have yet to see a good web tutorial for Perl (which isn't to say they
don't exist, of course).
> Q3) This one's a little out there. Right after the unlink statement up
> there, or better yet, just before the close statement, what would happen
> if you wrote:
>
> open( my $gh, '>', ceiling1.pl) or die("Can't open ceiling1.pl for
> writing: $!");
>
> where the name of the script itself is ceiling1.pl,and then attempted to
> write from $gh to $fh linewise?
Do you mean the other way around (overwriting the script file itself)?
In that case: nothing, except you'd've just overwritten your script
file. Once perl gets to the point of actually running your program, it's
already read and compiled the whole thing; you can't affect the compiled
program by overwriting the source.
'What happens if I overwrite the source from a BEGIN block, before it's
finished compiling?' is a somewhat more interesting question. I'm not
entirely certain, but I believe the answer is just that perl reads
blocks from the file, and whatever happens to be there at that moment is
passed to the compiler.
> I know it's illegal in C, so I don't want to just try it and see what
> happens.
What you're talking about is more akin to writing a C program that
overwrites its own .c source, than one which overwrites its own running
executable.
> It's too much work cleaning up after the nasal demons when they get on
> your keyboard.
In principle there are no nasal demons in Perl. If perl ever does
something undefined, that's a bug in perl (or possibly in an XS module
you've loaded).
(This is not quite as true as it ought to be: that is, there are still
some remaining bugs in perl. They're not easy to fall into by accident,
though.)
Ben
------------------------------
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 3725
***************************************