[23064] in Perl-Users-Digest
Perl-Users Digest, Issue: 5285 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Jul 29 09:05:42 2003
Date: Tue, 29 Jul 2003 06:05:10 -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, 29 Jul 2003 Volume: 10 Number: 5285
Today's topics:
Re: Create electronic newsletters and email to all peop <g4rry_sh0rt@zw4llet.com>
Re: Currying functions <ed@membled.com>
Re: Help with parsing text file (albert vilella)
Re: How do you Call a Perl subroutine with a variable n (Fred)
how to know the week of today? <ggkuo@yahoo.com>
Re: how to know the week of today? <noreply@gunnar.cc>
Re: how to know the week of today? <nobull@mail.com>
Re: How to pick out words from a non-delimited string? <tassilo.parseval@rwth-aachen.de>
Re: I lost my 'perl' file. Where can I get it? <simon.andrews@bbsrc.ac.uk>
Re: Is there a clean way to export all constants from a <bart.lateur@pandora.be>
Re: New to Perl <g4rry_sh0rt@zw4llet.com>
Re: Perl compiler? error (fatted)
Re: Perl variable data "replacement" <usenet@expires082003.tinita.de>
Re: Perl variable data "replacement" <tassilo.parseval@rwth-aachen.de>
Re: Perl variable data "replacement" <tassilo.parseval@rwth-aachen.de>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Tue, 29 Jul 2003 09:43:59 +0000
From: Garry Short <g4rry_sh0rt@zw4llet.com>
Subject: Re: Create electronic newsletters and email to all people in an excel spreadsheet
Message-Id: <bg5ca9$5on$2$8302bc10@news.demon.co.uk>
DB Coordinator for CS wrote:
> Hi Guys
>
> We are using Windows platform and active perl in our organisation. We want
> to create electronic newsletters(which embed images as well) and want to
> mailout all the people whose email addresses are in an excel spreadsheet.
> Can you give some advice about what modules/items we should use in Active
> perl to create/mailout electronic newsletters?. How can we achieve this
> task?. Are there any particular way to do this in active perl?. What
> modules/sections of Active perl we should need?.Can you give some
> reference or help?
>
> Thank you
> Jo
Jo,
one other thing to consider - don't use Excel format if you can help it - it
requires seperate modules, and slows everything down considerably. It's
much easier to do (and quicker to run) if you save the file as a CSV (i.e.
text!).
Regards,
Garry
------------------------------
Date: 29 Jul 2003 13:42:18 +0100
From: Ed Avis <ed@membled.com>
Subject: Re: Currying functions
Message-Id: <l1oezdwllh.fsf@budvar.future-i.net>
In Perl regular expressions are compiled at compile time, except if
they contain variables, in which case the compilation happens at run
time and every time the regexp is used. This is indeed slower than
memoizing previously compiled regexps, as the regexp library of Python
(for example) does.
There is the /o flag which says that once a regexp has been compiled
at run time it can be kept around and not recompiled for each use; you
can use this if you know that the variables you used in the regexp
will not change their values later.
If in Perl you want to use a regexp which is not known until run time,
but on the other hand expect that the same regexp will be used often,
then one way to get the right result is to construct a closure:
my ($f, $g);
{
my $pattern = 'a';
$f = sub { /$pattern/o };
}
{
my $pattern = 'b';
$g = sub { /$pattern/o };
}
foreach (qw(a b)) {
print "testing $_\n";
print "f\n" if $f->();
print "g\n" if $g->();
}
The two different closures $f and $g have different $pattern variables
(which could be read from the user rather than set to string literals)
and the regexps applied will be cached after the first use - the /o
flag on the match says that the value of $pattern will not change.
--
Ed Avis <ed@membled.com>
------------------------------
Date: 29 Jul 2003 04:47:04 -0700
From: vilella@bio.ub.es (albert vilella)
Subject: Re: Help with parsing text file
Message-Id: <b5ecc0be.0307290347.7f713170@posting.google.com>
"Kent" <kstairley@coppernospam.com> wrote in message news:<3f26114c_3@athena.netset.com>...
> All,
>
> I'm trying to parse a text file with logic that essentially goes like this:
> 1) open the file for reading with a handle
> 2) match for a regex (regular expression)
> 3) if found, get or print (to stdout or a file) the next few lines until a
> blank line is found or a different regex is found
>
> I can open the file for reading, no prob. - open (myhandle, <text_file>);
> # or something like that
> I can match the regex, no prob. - while ($myhandle =~ $regex) # or
> something like this, sorry I don't have the code
> in front of me and it's late. (But I can do this with the IO::Handle module)
> But, how do I get the next few lines after I've matched the first regex?
> I've looked at the getline(s) function
> in the IO::Handle module and I couldn't get something like this to work:
> (from the wrox press Perl prog. book)
> $line = $fh->getline();
> while ($fh->getline) {
> print "$_ \n";
> }
>
> Any help would be appreciated.
> TIA, Kent
> (kstairley@copper.net)
Maybe using IO::File instead of IO::Handle,
------------------------------------
use strict;
use IO::File;
my $fh = IO::File->new("<myfile.txt")
or die "Couldnt open file for reading: $!\n";
my $line = undef;
if (defined($line = $fh->getline())) {
#do something
}
while (defined($line = $fh->getline())) {
#do something more
}
1;
------------------------------------
You can find more info in "perldoc IO::File" and "perldoc IO::Handle"
"IO::Handle" is the base class for all other IO handle classes. It is
not intended that objects of "IO::Handle" would be created directly,
but instead "IO::Handle" is inherited from by several other classes in
the IO hierarchy.
Hope it helps,
avilella
------------------------------
Date: 29 Jul 2003 05:12:09 -0700
From: fred.illig@lmco.com (Fred)
Subject: Re: How do you Call a Perl subroutine with a variable name?
Message-Id: <8714513c.0307290412.22273a06@posting.google.com>
To everyone who has responded to this point, thank you.
I have tried the two solutions recommended in the current 8 messages,
but I cannot get either to work. I will be getting my team together
this morning to see if I entered any typos or if any of the formats
provided may need tweeked.
If anyone has any working solutions to my problem, maybe you could gut
the code and send me your structure (how you actually got an array
element, that is a subroutine's name, to be able to be called as a
variable).
Thanks again for all your effort.
Fred
fred.illig@lmco.com (Fred) wrote in message news:<8714513c.0307281029.66147c6a@posting.google.com>...
> Anyone,
>
> I have a Perl module (*.pm).
> I have a subroutine declared - sub x($$$);
> I have the subroutine in the module - sub x($$$){<process the input
> variables and return two variables>};
>
> In another subroutine in this module, I want to call this subroutine
> (x) via an array element - @array = (a,b,c,x,y,z);
> ($outvar1, $outvar2) = &$array[3]($invar1,$invar2,$invar3);
>
> This is not working.
>
> How do I make a call to a subroutine where the name of the subroutine
> to be called is being derived from an element in an array? We have a
> variable as the subroutine name that we want to use.
>
> We have tried many different combination of referencing and
> de-referencing, but we cannot figure it out. A real live example
> would be great.
>
> Thanks in advance for any help you can send me.
>
> Fred
------------------------------
Date: Tue, 29 Jul 2003 03:34:07 GMT
From: "C&J" <ggkuo@yahoo.com>
Subject: how to know the week of today?
Message-Id: <OWlVa.662$Qb7.504188001@newssvr12.news.prodigy.com>
Hi,
Is there any way to know what is the current week?
January 1 is the first week.
Thanks
Chris
------------------------------
Date: Tue, 29 Jul 2003 12:42:15 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: how to know the week of today?
Message-Id: <bg5j5t$l4f74$1@ID-184292.news.uni-berlin.de>
C&J wrote:
> Is there any way to know what is the current week?
You'd better use a module for that. One example:
use Date::Format;
print time2str '%W', time;
> January 1 is the first week.
Is that always true?
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: 29 Jul 2003 11:51:29 +0100
From: Brian McCauley <nobull@mail.com>
Subject: Re: how to know the week of today?
Message-Id: <u9isplip1q.fsf@wcl-l.bham.ac.uk>
"C&J" <ggkuo@yahoo.com> writes:
> Is there any way to know what is the current week?
POSIX::strftime supports at least one of the widely used "week number"
algorithms, more on some platforms. (Note that the ISO and POSIX
standards do not agree).
> January 1 is the first week.
That alone does not uniquely define your week number algorithm.
You have to consider when week 2 starts. Does it always start on
January 8 or does it start on the the first Fooday on or after January
2.
Anyhow, whatever your chosen algorithm (within reason) it can be
expressed as a trivial integer expression in terms of $wday and $yday
as retuned by localtime().
If that feels too low-level for you then see Date::* on CPAN.
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: 29 Jul 2003 11:36:37 GMT
From: "Tassilo v. Parseval" <tassilo.parseval@rwth-aachen.de>
Subject: Re: How to pick out words from a non-delimited string?
Message-Id: <bg5m85$e4v$1@nets3.rz.RWTH-Aachen.DE>
Also sprach Julian Hsiao:
> "Tassilo v. Parseval" <tassilo.parseval@rwth-aachen.de> wrote:
>> Another, more general approach, would try to do the splitting based on
>> morphological rules for a given language.
>
> Can you please elaborate on what you mean? In my case, the string
> will always be in English.
I am not familiar enough with English morphology. However, the idea is
that every language has certain rules how words are built. Certain
combinations of letters never show up in a language (or, if they do,
they must belong to two adjoining syllables; in German for instance
there can't be a 'k' directly following a 'Sch' within a syllable
because the result 'Schk' is no valid German phonem which means it would
be unpronounceable, therefore the word 'Tischkante' has three syllables:
Tisch-kan-te). The word-splitting algorithms in text-processors work
that way.
There are mainly two problems when trying to do it that way: First of
all, you need to have all the rules (they are quite complex for English
as they are for most other languages) and secondly, these rules are only
valid within one word because languages tend to allow any sequence of
words (whereas they do not necessarily allow any sequence of letters
within one word).
So better go for a dictionary-approach. This itself will be hard enough.
I just did a few local tests with a text from the Gutenberg projects and
an English dictionary file. Whichever implementation you choose, the
result will probably be dog-slow and get it wrong the first couple of
attempts. Even when sorting out the brain-dead attempts, the list of
words it spits out is highly suspect.
Tassilo
--
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
------------------------------
Date: Tue, 29 Jul 2003 09:11:10 +0100
From: Simon Andrews <simon.andrews@bbsrc.ac.uk>
Subject: Re: I lost my 'perl' file. Where can I get it?
Message-Id: <3F262C1E.4080107@bbsrc.ac.uk>
David Dorward wrote:
> Francesco Moi wrote:
>
>>Due to an error, I deleted my '/usr/bin/perl' file.
>>It was build with 'perl-5.8.0-88.i386.rpm'.
>>How can I get it?
>
>
> Reinstalling the same RPM file would seem to be the most logical course of
> action.
Since your RPM database still thinks that the original rpm is installed
you may find a normal "rpm -i" won't work (you'll get a message saying
it's already installed). In this case, try something like:
rpm -i --replacepkgs perl-5.8.0-88.i386.rpm
..to force it to reinstall your perl executable.
Cheers
Simon.
------------------------------
Date: Tue, 29 Jul 2003 11:02:33 GMT
From: Bart Lateur <bart.lateur@pandora.be>
Subject: Re: Is there a clean way to export all constants from a module?
Message-Id: <lakcivkhkboqlrs6acvhq42e5cm24jpol0@4ax.com>
Steve Allan wrote:
>I'm creating a module for the sole purpose of defining constants to be
>shared among several Perl scripts. I'm looking for a way to simplify
>getting all the constant names into the @EXPORT_OK array, so I don't
>end up with this:
>
>@EXPORT_OK = qw(CURRENTCHANGE BLDNUM STAGEROOTDIR ...);
>
>because I can envision that list becoming large, and everytime I
>define a new constant I have to remember to add it to the list.
That's not your only problem. Every script using this module will also
have to be changed to include this constant in it's use statement. Or is
that not a problem? If you don't use a constant, you don't need to
import it...
But anyway: there's a way to group these names into a tag, for example
":constants", and all you have to do to import them all, is call
use Foo qw(:constants);
For this to happen, you have to include the tag and it's associated
names in the variable %EXPORT_TAGS, not @EXPORT_OK, like this:
%EXPORT_TAGS = (
constants => [qw(CURRENTCHANGE BLDNUM STAGEROOTDIR)]
);
You can supply multiple tags, even including some of the same names more
than once, allowing for refinement on groups of constants.
See the documentation for Exporter for all the details.
You can use whatever method you used to fill @EXPORT_OK, to populate
this hash.
--
Bart.
------------------------------
Date: Tue, 29 Jul 2003 09:39:02 +0000
From: Garry Short <g4rry_sh0rt@zw4llet.com>
Subject: Re: New to Perl
Message-Id: <bg5c10$5on$1$8302bc10@news.demon.co.uk>
Brett Irving wrote:
> Hi I am new to perl and am trying to do some test functions that will
> let me substitute html from one file into another using markers ie.
>
> <HTML>
>
> <HEAD>
> </HEAD>
>
> <BODY>
> <!-- ***INSERT NEW CONTENT HERE*** -->
> </BODY>
>
> </HTML>
>
> Is there any easy way I can go about doing this??
> any help would be appreciated.
Assuming the above file is in template.html, and the new content is in
content.html, something like this would work:
open TEMPLATE, "template.html" or die "Can't open template.html: $!\n";
open CONTENT, "content.html" or die "Can't open content.html: $!\n";
open OUTFILE, ">outfile.html" or die "Can't write to $outfile: $!\n";
while ($t = <TEMPLATE>) {
if ($t =~ /<!-- ***INSERT NEW CONTENT HERE*** --->/) {
while ($c = <CONTENT>) {
print OUTFILE "$c";
}
} else {
print OUTFILE "$t";
}
}
close TEMPLATE;
close CONTENT;
close OUTFILE;
HTH,
Garry
------------------------------
Date: 29 Jul 2003 00:31:23 -0700
From: fatted@yahoo.com (fatted)
Subject: Re: Perl compiler? error
Message-Id: <4eb7646d.0307282331.49d74e02@posting.google.com>
jkeen@concentric.net (James E Keenan) wrote in message news:<b955da04.0307281016.17c5355d@posting.google.com>...
> fatted@yahoo.com (fatted) wrote in message news:<4eb7646d.0307280451.1acadb3f@posting.google.com>...
> > Lets say a friend of mine :) wrote code which had a bad error in it:
> >
> > #!/usr/bin/perl
> > use warnings;
> > use strict;
> >
> > my $words = ["a", "b", "c"];
> > my $num = 612;
> >
> > if($num == 1)
> > {
> > print "moo\n";
> > }
> > elsif (map($_->[0] =~ /\d{6}/, @$words))
> > {
> > print "Don't give up day job\n";
> > }
> >
> > When run this gives the error:
> > Can't use string ("a") as an ARRAY ref while "strict refs" in use at
> > ./test.pl line 8.
> > After about 1/2 hour of head banging, the problem (which was
> > originally in a much larger program) was finally discovered. The code
> > problem wasn't on line 8 :). I have fixed that and slapped my "friend"
> > around the face for stupidity.
> >
> > However this leads to my real question:
> > Is the error message above, an error in the compiler? For instance if
> > the close bracket of an elsif is forgotton, you get an error on the
> > line where the close bracket was left out. *Not* on the line where the
> > start of the IF THEN ELSE syntax began.
> >
> There's no compiler error here, but I think you may have a conceptual
> error. The 'elsif' clause establishes a Boolean context, i.e., either
> a 'true' or 'false' (in Perl's terms) is the only acceptable answer.
> Boolean context should be thought of as a subset of scalar context.
> 'map', however, generates a list by performing an operation on each
> element of a source list. Hence, 'map' normally establishes a list
> context. However, since that 'map' is itself within the
> Boolean/scalar context established by 'elsif', it will impose a scalar
> context, i.e., it will ask how many items are in the resulting list.
> If there is a nonzero number of items in the source list, the 'elsif'
> will register as true.
>
> Now, are you sure that that's what you were aiming for with this code?
> (In short, I'm puzzled by your use of 'map' inside an 'elsif'
> condition.)
>
> Jim Keenan
I'm comparing each value from the list stored in the array (reference)
to see if it matches the regular expression. If it matches , the
return value of map will be greater than 0, which will be evaluated by
elsif as true and run the code in the elsif block. Seems like a
perfectly good idea to me :)
My coding problem occured, because earlier in my script the value I
wanted to compare against a reg exp, was stored in an array reference
of array references; this doesn't work half as well when trying to
compare against just an array reference:) Unfortunately I did a copy
paste, turned my brain off, and was thrown by the runtime error.
------------------------------
Date: 29 Jul 2003 11:40:04 GMT
From: Tina Mueller <usenet@expires082003.tinita.de>
Subject: Re: Perl variable data "replacement"
Message-Id: <bg5mek$kktnf$1@ID-24002.news.uni-berlin.de>
Tassilo v. Parseval wrote:
> use URI::Find::Schemeless;
> my $u = URI::Find::Schemeless->new (
> sub { my $scheme = "http://" if $_[1] !~ m!^http://!;
> qq!<a href="$scheme$_[1]">$_[1]</a>! }
> );
doesn't URI::Find::Schemeless put the url with "http://" into
$_[0]? so that you can do:
sub { qq{<a href="$_[0]">$_[1]</a>} }
at least version 1.6 does that for me.
regards, tina
--
http://www.tinita.de/ \ enter__| |__the___ _ _ ___
http://Movies.tinita.de/ \ / _` / _ \/ _ \ '_(_-< of
http://www.perlquotes.de/ \ \ _,_\ __/\ __/_| /__/ perception
- my mail address expires end of august 2003 -
------------------------------
Date: 29 Jul 2003 11:51:01 GMT
From: "Tassilo v. Parseval" <tassilo.parseval@rwth-aachen.de>
Subject: Re: Perl variable data "replacement"
Message-Id: <bg5n35$f7e$1@nets3.rz.RWTH-Aachen.DE>
Also sprach "Dandy" Randy:
>> > I've seen Perl driven online forums perform such a task ... I've read
>> > relevant perdoc's but have been unable to locate the solution. Ideas
> and/or
>> > examples welcomed! TIA!
>>
>> You should use URI::Find for this task. It comes with a subclass
>> (URI::Find::Schemeless) that can be used for finding URIs that actually
>> aren't URIs (such as 'www.host.com'):
>>
>> use URI::Find::Schemeless;
>>
>> my $u = URI::Find::Schemeless->new (
>> sub { my $scheme = "http://" if $_[1] !~ m!^http://!;
>> qq!<a href="$scheme$_[1]">$_[1]</a>! }
>> );
>>
>> my $text = "please visit www.mysite.com";
>>
>> # this changes $text in-place
>> $u->find(\$text);
>>
>> print $text;
>> __END__
>> please visit <a href="http://www.mysite.com">www.mysite.com</a>
>>
>> The above also adds a naive heuristic to see whether the URI scheme
>> needs to be prepended to the address.
>
> Tassilo, I wonder if this method would work with what I am trying to do. In
> your code you define $text as a pre-determined value ... I need the code to
> search a flatfile datasource (all lines) and replace all www addresses with
> the needed html tags ... this data could change daily. Would this work:
>
> use URI::Find::Schemeless;
>
> my $u = URI::Find::Schemeless->new (
> sub { my $scheme = "http://" if $_[1] !~ m!^http://!;
> qq!<a href="$scheme$_[1]">$_[1]</a>! }
> );
>
> open (list, "<data.txt") or &error("Unable to open");
> @list=<list>;
> close(list);
>
> my $text = @list;
Close. Instead:
my $text = join '', @list;
What you did was assigning the number of elements in @list to $text
(array in scalar context is the number of its elements).
> # this changes $text in-place
> $u->find(\$text);
>
> print $text;
Now this should work. All you need to do is write back the changed data:
open FILE, ">data.txt" or die $!;
print FILE $text;
close FILE;
and you should be done.
You can also manipulate @list directly:
for my $line (@list) {
$u->find(\$line);
}
...
print FILE @list;
This works because $line is an alias to each element of @list in the
above for-loop.
Tassilo
--
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
------------------------------
Date: 29 Jul 2003 11:54:35 GMT
From: "Tassilo v. Parseval" <tassilo.parseval@rwth-aachen.de>
Subject: Re: Perl variable data "replacement"
Message-Id: <bg5n9r$fcu$1@nets3.rz.RWTH-Aachen.DE>
Also sprach Tina Mueller:
> Tassilo v. Parseval wrote:
>
>> use URI::Find::Schemeless;
>
>> my $u = URI::Find::Schemeless->new (
>> sub { my $scheme = "http://" if $_[1] !~ m!^http://!;
>> qq!<a href="$scheme$_[1]">$_[1]</a>! }
>> );
>
> doesn't URI::Find::Schemeless put the url with "http://" into
> $_[0]? so that you can do:
> sub { qq{<a href="$_[0]">$_[1]</a>} }
Ah, it does indeed. It puts a URI::URL object into $_[0] that has
overloaded stringification.
Tassilo
--
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
------------------------------
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:
The Perl-Users Digest is a retransmission of the USENET newsgroup
comp.lang.perl.misc. For subscription or unsubscription requests, send
the single line:
subscribe perl-users
or:
unsubscribe perl-users
to almanac@ruby.oce.orst.edu.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
To request back copies (available for a week or so), send your request
to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
where x is the volume number and y is the issue number.
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 V10 Issue 5285
***************************************