[25705] in Perl-Users-Digest
Perl-Users Digest, Issue: 7945 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Apr 5 14:05:32 2005
Date: Tue, 5 Apr 2005 11:05:08 -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, 5 Apr 2005 Volume: 10 Number: 7945
Today's topics:
Re: #!perl ?? <ron@savage.net.au>
Re: #!perl ?? <1usa@llenroc.ude.invalid>
Re: auto-deleted explcitily named temp files - possible (Anno Siegel)
Re: auto-deleted explcitily named temp files - possible odigity@gmail.com
Re: Generate random arrays? <No_4@dsl.pipex.com>
Q: // and "magic" <jkrugman345@yahbitoo.com>
Re: Q: // and "magic" <1usa@llenroc.ude.invalid>
Re: Q: // and "magic" xhoster@gmail.com
Re: Q: // and "magic" <1usa@llenroc.ude.invalid>
Re: Q: // and "magic" <matternc@comcast.net>
Re: Q: // and "magic" <1usa@llenroc.ude.invalid>
Re: recursivity (Anno Siegel)
Re: Regular expression question. <tadmc@augustmail.com>
Re: Shell Commands (Getting PID and Timing Them) (Anno Siegel)
Re: Shell Commands (Getting PID and Timing Them) <tadmc@augustmail.com>
Re: Shell Commands (Getting PID and Timing Them) <hal@thresholddigital.com>
Re: speed issues with pattern matching and substitution (Anno Siegel)
Re: time zone offset calc with localtime and gmtime <leendert@wouter.unitedknowledge.net>
Re: time zone offset calc with localtime and gmtime <mothra@nowhereatall.com>
Re: time zone offset calc with localtime and gmtime <leendert@wouter.unitedknowledge.net>
Re: time zone offset calc with localtime and gmtime <mothra@nowhereatall.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Tue, 5 Apr 2005 20:29:03 +1000
From: Ron Savage <ron@savage.net.au>
Subject: Re: #!perl ??
Message-Id: <20054520293.919522@ron>
On Sat, 2 Apr 2005 17:37:30 +1000, J Krugman wrote:
Hi Jill
Search this page
http://savage.net.au/Perl/html/upgrade-perl-apache.html
for /usr/bin/perl and you'll what to do under Windows for the she-bang=
line.
------------------------------
Date: Tue, 05 Apr 2005 10:54:27 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: #!perl ??
Message-Id: <Xns962F4645943C9asu1cornelledu@127.0.0.1>
Ron Savage <ron@savage.net.au> wrote in news:20054520293.919522@ron:
> On Sat, 2 Apr 2005 17:37:30 +1000, J Krugman wrote:
Did Jill make the comments below? No.
Please use a more effective quoting style and avoid misattributions.
> Search this page
>
> http://savage.net.au/Perl/html/upgrade-perl-apache.html
>
> for /usr/bin/perl and you'll what to do under Windows for the she-bang
> line.
1. You completely missed the point of Jill's post.
2. Her question was not about setting up Apache and Perl on Windows.
3. Even if it were, the answer you pointed to is not that great. See
http://httpd.apache.org/docs-2.0/mod/core.html#scriptinterpretersource
for a better way.
Sinan
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)
comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
------------------------------
Date: 5 Apr 2005 11:43:23 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: auto-deleted explcitily named temp files - possible?
Message-Id: <d2ttkr$2du$3@mamenchi.zrz.TU-Berlin.DE>
<xhoster@gmail.com> wrote in comp.lang.perl.misc:
> odigity@gmail.com wrote:
> ...
> > So I came up with the idea that there would be a directory - say
> > /resource - where files would be created indicating that the cpu in
> > question is being used. For example, if an app starts and launches two
> > processes on host01, saturating both CPUs, it would create
> > /resource/host01.1 and /resource/host01.2, and then delete them when
> > finished. That way apps can efficiently use processor resources with
> > tempfiles as a super-simple communication mechanism.
> >
> > One requirement is that the lockfile automatically get deleted no
> > matter what happened to the process. This is imperative, since if it
> > is possible for a process to exit and leave the lockfile behind, other
> > apps will continue to think that resource is in use and not use it.
>
> Each process should not only create the file, but also lock the file
> using flock. Then whatever process is looking for a free CPU, should, if
> it doesn't find one, go through all the files making sure they are
> actually locked (by trying to flock them), and if it finds ones that aren't
> already locked it knows that they have been abandoned by dead process.
I was going to suggest something similar, only I wouldn't bother with
deleting the lock file at all. Just try to create it (using
sysopen with O_EXCL) in case it isn't there, then rely on the existence
of locks alone.
Anno
------------------------------
Date: 5 Apr 2005 10:29:30 -0700
From: odigity@gmail.com
Subject: Re: auto-deleted explcitily named temp files - possible?
Message-Id: <1112722170.551749.63420@z14g2000cwz.googlegroups.com>
Both good ideas. I believe flock does work properly over NFS. I'll
give it a shot!
-ofer
------------------------------
Date: Tue, 05 Apr 2005 11:34:05 +0100
From: Big and Blue <No_4@dsl.pipex.com>
Subject: Re: Generate random arrays?
Message-Id: <2IednTzLM74C9M_fRVnyjQ@pipex.net>
BCC wrote:
>
> sub getCoords {
> my @nums = (1..100);
> my %random = ();
> my $num = 0;
> until ($num == 1000) {
> my $i = int($nums[rand(@nums)]);
> $random{$i}=1;
> $num = keys %random;
> }
> return(keys %random);
> }
This should loop forever. Your range for i is 0 to 99, but your loop
exit condition is to stop when you have 1000 different integers in that
range. It won't happen.
Also, the concept of "random" and "non-repeating" is contradictory.
--
Just because I've written it doesn't mean that
either you or I have to believe it.
------------------------------
Date: Tue, 5 Apr 2005 13:59:02 +0000 (UTC)
From: J Krugman <jkrugman345@yahbitoo.com>
Subject: Q: // and "magic"
Message-Id: <d2u5j6$a4j$1@reader1.panix.com>
In perlre I found these puzzling lines:
@chars = split //, $string; # // is not magic in split
($whitewashed = $string) =~ s/()/ /g; # parens avoid magic s// /
I don't understand the comments. What's all the "magic" about?
In an attempt to understand the first comment, I consulted perldoc
-f split, which made matters worse. I found no mention at all of
"magic", but I came across this:
Using the empty pattern "//" specifically matches
the null string, and is not be confused with the
use of "//" to mean "the last successful pattern
match".
Now I'm hopelessly confused. I understand that "//" matches the
null string, but I have no idea what the last sentence above (about
the "other" use of "//") is talking about. Any help sorting this
out would be greatly appreciated.
TIA!
jill
--
To s&e^n]d me m~a}i]l r%e*m?o\v[e bit from my a|d)d:r{e:s]s.
------------------------------
Date: Tue, 05 Apr 2005 14:13:40 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Q: // and "magic"
Message-Id: <Xns962F680B047BAasu1cornelledu@127.0.0.1>
J Krugman <jkrugman345@yahbitoo.com> wrote in news:d2u5j6$a4j$1
@reader1.panix.com:
> In perlre I found these puzzling lines:
>
> @chars = split //, $string; # // is not magic in split
> ($whitewashed = $string) =~ s/()/ /g; # parens avoid magic s// /
>
> I don't understand the comments. What's all the "magic" about?
>
> In an attempt to understand the first comment, I consulted perldoc
> -f split, which made matters worse. I found no mention at all of
> "magic", but I came across this:
>
> Using the empty pattern "//" specifically matches
> the null string, and is not be confused with the
> use of "//" to mean "the last successful pattern
> match".
>
> Now I'm hopelessly confused. I understand that "//" matches the
> null string, but I have no idea what the last sentence above (about
> the "other" use of "//") is talking about.
In the context of the split function, // matches the empty string.
Elsewhere, // means the last successful pattern match.
IMHO, the passage above is very clear, but here is the relevant section
from perldoc perlop (where m// is being discussed):
If the PATTERN evaluates to the empty string, the last
*successfully* matched regular expression is used instead. In
this case, only the "g" and "c" flags on the empty pattern is
honoured - the other flags are taken from the original pattern.
If no match has previously succeeded, this will (silently) act
instead as a genuine empty pattern (which will always match).
Sinan
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)
comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
------------------------------
Date: 05 Apr 2005 16:56:14 GMT
From: xhoster@gmail.com
Subject: Re: Q: // and "magic"
Message-Id: <20050405125614.353$RT@newsreader.com>
"A. Sinan Unur" <1usa@llenroc.ude.invalid> wrote:
>
> In the context of the split function, // matches the empty string.
>
> Elsewhere, // means the last successful pattern match.
>
> IMHO, the passage above is very clear, but here is the relevant section
> from perldoc perlop (where m// is being discussed):
>
> If the PATTERN evaluates to the empty string, the last
> *successfully* matched regular expression is used instead. In
> this case, only the "g" and "c" flags on the empty pattern is
> honoured - the other flags are taken from the original pattern.
> If no match has previously succeeded, this will (silently) act
> instead as a genuine empty pattern (which will always match).
So, does anyone find this behavior useful? I've never intentionally used
it, and I can't imagine doing so in the future.
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
------------------------------
Date: Tue, 05 Apr 2005 17:24:13 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Q: // and "magic"
Message-Id: <Xns962F885973968asu1cornelledu@127.0.0.1>
xhoster@gmail.com wrote in news:20050405125614.353$RT@newsreader.com:
> "A. Sinan Unur" <1usa@llenroc.ude.invalid> wrote:
>>
>> In the context of the split function, // matches the empty string.
>>
>> Elsewhere, // means the last successful pattern match.
>>
>> IMHO, the passage above is very clear, but here is the relevant
>> section from perldoc perlop (where m// is being discussed):
>>
>> If the PATTERN evaluates to the empty string, the last
>> *successfully* matched regular expression is used instead. In
>> this case, only the "g" and "c" flags on the empty pattern is
>> honoured - the other flags are taken from the original pattern.
>> If no match has previously succeeded, this will (silently) act
>> instead as a genuine empty pattern (which will always match).
>
> So, does anyone find this behavior useful? I've never intentionally
> used it, and I can't imagine doing so in the future.
At the risk of sounding like an AOLer, I am curious as well. I tried
thinking of a way to use this feature. Couldn't think of anything, but
that is probably a reflection of my limitations :)
I have a feeling Abigail might contribute some magic.
Sinan
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)
comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
------------------------------
Date: Tue, 05 Apr 2005 13:49:21 -0400
From: Chris Mattern <matternc@comcast.net>
Subject: Re: Q: // and "magic"
Message-Id: <icWdnVCkIts8Us_fRVn-jQ@comcast.com>
A. Sinan Unur wrote:
> xhoster@gmail.com wrote in news:20050405125614.353$RT@newsreader.com:
>
>> "A. Sinan Unur" <1usa@llenroc.ude.invalid> wrote:
>>>
>>> In the context of the split function, // matches the empty string.
>>>
>>> Elsewhere, // means the last successful pattern match.
>>>
>>> IMHO, the passage above is very clear, but here is the relevant
>>> section from perldoc perlop (where m// is being discussed):
>>>
>>> If the PATTERN evaluates to the empty string, the last
>>> *successfully* matched regular expression is used instead. In
>>> this case, only the "g" and "c" flags on the empty pattern is
>>> honoured - the other flags are taken from the original pattern.
>>> If no match has previously succeeded, this will (silently) act
>>> instead as a genuine empty pattern (which will always match).
>>
>> So, does anyone find this behavior useful? I've never intentionally
>> used it, and I can't imagine doing so in the future.
>
> At the risk of sounding like an AOLer, I am curious as well. I tried
> thinking of a way to use this feature. Couldn't think of anything, but
> that is probably a reflection of my limitations :)
>
> I have a feeling Abigail might contribute some magic.
>
If you have an "untaint this" regexp, you might wind up using several
times in a row on several variables. But mostly, I think this was
Larry getting a little overenthusiastic in "save the programmer
keystrokes" mode. And once it was around for awhile, of course it
couldn't be taken out because it would break stuff.
--
Christopher Mattern
"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
------------------------------
Date: Tue, 05 Apr 2005 17:56:33 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Q: // and "magic"
Message-Id: <Xns962F8DD497435asu1cornelledu@127.0.0.1>
xhoster@gmail.com wrote in news:20050405125614.353$RT@newsreader.com:
> "A. Sinan Unur" <1usa@llenroc.ude.invalid> wrote:
>>
>> In the context of the split function, // matches the empty string.
>>
>> Elsewhere, // means the last successful pattern match.
>>
>> IMHO, the passage above is very clear, but here is the relevant
>> section from perldoc perlop (where m// is being discussed):
>>
>> If the PATTERN evaluates to the empty string, the last
>> *successfully* matched regular expression is used instead. In
>> this case, only the "g" and "c" flags on the empty pattern is
>> honoured - the other flags are taken from the original pattern.
>> If no match has previously succeeded, this will (silently) act
>> instead as a genuine empty pattern (which will always match).
>
> So, does anyone find this behavior useful? I've never intentionally
> used it, and I can't imagine doing so in the future.
I can think of one situation where this feature might be useful.
Consider the following:
#! perl
use strict;
use warnings;
my $s = 'one two three onetwo three one two three four';
my %count;
if( $s =~ /\b(one)\b/ or $s =~ /\b(two)\b/ ) {
++$count{$1} while( $s =~ //g );
}
__END__
Here, I am interested in counting the number of times the word 'one' in
the text. If there are no 'one's, then I want to count the number of
times 'two' occurs.
I think this is the most succint way of expressing the intent above. I
do not know if it would offer any speed advantages over other methods of
doing the same thing.
The construct might allow the programmer to more naturally avoid
alternation in regular expressions in favor of or tests in the
conditional and that might result in a performance benefit as well.
All this is speculation, however.
Sinan
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)
comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
------------------------------
Date: 5 Apr 2005 11:53:45 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: recursivity
Message-Id: <d2tu89$2du$4@mamenchi.zrz.TU-Berlin.DE>
<axel@white-eagle.invalid.uk> wrote in comp.lang.perl.misc:
> John W. Krahn <someone@example.com> wrote:
> > This may work better (untested):
>
> The lack of testing shows.
>
> > opendir DIR, $dir or die "Couldnt open $dir - $!\n";
>
> The variable $var is never declared.
^^^^
$dir
Anno
------------------------------
Date: Tue, 5 Apr 2005 08:51:34 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Regular expression question.
Message-Id: <slrnd555v6.4mj.tadmc@magna.augustmail.com>
MENTAT <chaitanyag@hotmail.com> wrote:
> I have a log file that looks something like this
>
> 2005-03-29 17:17:11.293|DEBUG|Line 1|
>>>>>>>>
> Actual Log output line 1
> Actual Log output line 2
> Actual Log output line 3
> Actual Log output line ...
><<<<<<<
> 2005-03-29 17:17:11.293|DEBUG|Line 9|
>>>>>>>>
> Actual Log output line 1
> Actual Log output line 2
> Actual Log output line 3
> Actual Log output line ...
><<<<<<<
> I am trying to write a regular expression that extracts all the log
> entries for a given value of "Line".
Would a much easier way that makes no use of regular expressions be OK?
> Ofcourse, if i remove the s global modifier, i can easily match it
> using "(^.*\|Line 4.*)", but then I can't get all the (variable) lines
> between <<<<<<< and >>>>>>>. The .* won't match across new line.
There are several ways to write "any character" (which includes
newline) that remain unaffected by the m//s modifier.
[\000-\0377]
[\d\D]
[\w\W]
[\s\S]
> Any idea how this problem could be solved?
Setting
$/ = "<<<<<<<\n";
before reading the input would help a lot.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: 5 Apr 2005 11:06:09 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Shell Commands (Getting PID and Timing Them)
Message-Id: <d2trf1$2du$1@mamenchi.zrz.TU-Berlin.DE>
Hal Vaughan <hal@thresholddigital.com> wrote in comp.lang.perl.misc:
> Joe Smith wrote:
>
> > Hal Vaughan wrote:
> >> In Perl I know how to fork and get a PID, and I know how to use
> >> `command` to get a commands output, but I can't find a way to run a
> >> command from Perl and get the PID so I can kill it when I want.
> >
> > my $child_pid = fork();
> > die unless defined $child_pid;
> > if ($child_pid) { # Parent
> > sleep $sleep_time;
> > kill 2,$child_pid;
> > } else { # Child
> > exec "command </dev/null >/dev/null 2>&1";
> > die "Could not run 'command'";
> > }
> > -Joe
>
> Thanks, but that gives me the PID of the forked process. I want to run a
> BASH command from Perl, and get the PID of the BASH command. The child_pid
> is not the same as the bash command.
Have you tried the code? Have you read "perldoc -f exec"? What is a
BASH-command?
Anno
------------------------------
Date: Tue, 5 Apr 2005 09:20:04 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Shell Commands (Getting PID and Timing Them)
Message-Id: <slrnd557kk.4mj.tadmc@magna.augustmail.com>
Hal Vaughan <hal@thresholddigital.com> wrote:
> Joe Smith wrote:
>> Hal Vaughan wrote:
>>> In Perl I know how to fork and get a PID, and I know how to use
>>> `command` to get a commands output, but I can't find a way to run a
>>> command from Perl and get the PID so I can kill it when I want.
>>
>> my $child_pid = fork();
>> die unless defined $child_pid;
>> if ($child_pid) { # Parent
>> sleep $sleep_time;
>> kill 2,$child_pid;
>> } else { # Child
>> exec "command </dev/null >/dev/null 2>&1";
>> die "Could not run 'command'";
>> }
>> -Joe
>
> Thanks, but that gives me the PID of the forked process.
Which is the process that you want to time-out.
> I want to run a
> BASH command from Perl, and get the PID of the BASH command.
You got it (if /bin/sh is bash on your system. If not then
replace "command" above with "/bin/bash").
> The child_pid
> is not the same as the bash command.
Yes it is.
Why do you think they are different?
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Tue, 05 Apr 2005 12:45:30 -0400
From: Hal Vaughan <hal@thresholddigital.com>
Subject: Re: Shell Commands (Getting PID and Timing Them)
Message-Id: <o7ydnT1jSOU1Xc_fRVn-gg@comcast.com>
Anno Siegel wrote:
> Hal Vaughan <hal@thresholddigital.com> wrote in comp.lang.perl.misc:
>> Joe Smith wrote:
>>
>> > Hal Vaughan wrote:
>> >> In Perl I know how to fork and get a PID, and I know how to use
>> >> `command` to get a commands output, but I can't find a way to run a
>> >> command from Perl and get the PID so I can kill it when I want.
>> >
>> > my $child_pid = fork();
>> > die unless defined $child_pid;
>> > if ($child_pid) { # Parent
>> > sleep $sleep_time;
>> > kill 2,$child_pid;
>> > } else { # Child
>> > exec "command </dev/null >/dev/null 2>&1";
>> > die "Could not run 'command'";
>> > }
>> > -Joe
>>
>> Thanks, but that gives me the PID of the forked process. I want to run a
>> BASH command from Perl, and get the PID of the BASH command. The
>> child_pid is not the same as the bash command.
>
> Have you tried the code? Have you read "perldoc -f exec"? What is a
> BASH-command?
>
> Anno
Tried it with typos. It works now. I saw the other suggested solution and
tried it first. Unfortunately, at later than 3 am, I didn't follow this
example (with the forking) as well as I should have, and misunderstood what
it was doing and what I was looking at.
Funny how things make better sense after a few hours of sleep...
Hal
------------------------------
Date: 5 Apr 2005 10:19:38 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: speed issues with pattern matching and substitution
Message-Id: <d2tonq$p0$1@mamenchi.zrz.TU-Berlin.DE>
Brian McCauley <nobull@mail.com> wrote in comp.lang.perl.misc:
> Arturi wrote:
>
> > I wrote some perl lines that scans a file looking for some words and
> > substitute them for their corresponding pairs, which are previously
> > saved in a HASH.
[...]
> If you are concerned about speed you should take the comoposition and
> compilatiion of the regex outside the loop.
>
> my @patterns = map { qr/(\W)($_)(\W)/ } keys %HASH;
>
> local *_; # Wise not to stomp on someone else's $_
>
> while (<FILE>) {
> my @values = values %HASH;
> for my $pattern ( @patterns ) {
> my $value = shift @values;
> s/$pattern/$1$value$3/g;
> }
> # print or whatever
> }
>
> But my spider-sense is telling me that perhaps the keys of %HASH really
> arbirary regexes but are just words and all you want is word
> substitution. This is a classic bit of Perl.
>
> while (<FILE>) {
> s/(\w+)/$HASH{$1}||$1/eg;
> # print or whatever
> }
At first sight that looks rather inefficient because *every* word
is captured and replaced (often by itself). Out of interest I
benchmarked a few variants, and it turns out that it is just as
fast as more selective solutions.
I'm appending the benchmark script, in case anyone cares. The four
variants tested are (least efficient first, the difference between
the last two is insignificant):
direct -- every regex is compiled from the string at run time
regex -- all alternatives compiled into a big regex
brian -- replace every word, like the second solution above
precomp -- regular expressions precompiled via qr//, like the
first solution above
Anno
--
#!/usr/local/bin/perl
use strict; use warnings; $| = 1;
use Vi::QuickFix;
use Benchmark qw( cmpthese);
our @lines = <DATA>;
my %repla; # string replacements (master)
@repla{
qw( one two three four five six seven eight nine ten eleven twelve)
} = qw( ein zwei drei vier fuenf sechs sieben acht neun zehn elf zwoelf);
my @repla = map [ qr/\b$_\b/ => $repla{ $_}], keys %repla; # precompiled
my $repla = do { # single regex
my $re = join '|', map "\\b$_\\b", keys %repla;
qr/($re)/;
};
goto bench;
check:
print brian( @lines);
print "\n", @lines;
exit;
bench:
@lines = ( @lines) x 10; # vary to check linearity
cmpthese -1, {
direct => 'direct( @lines)',
precomp => 'precomp( @lines)',
brian => 'brian( @lines)',
regex => 'regex( @lines)',
};
#######################################################################
sub direct {
my @new = @_;
for ( @new ) {
my ( $str, $rpl);
s/\b$str\b/$rpl/g while ( $str, $rpl) = each %repla;
}
@new;
}
sub precomp {
my @new = @_;
for ( @new ) {
for my $r ( @repla ) {
s/$r->[ 0]/$r->[ 1]/g;
}
}
@new ;
}
sub brian {
my @new = @_;
s/(\w+)/$repla{ $1} || $1/eg for @new;
@new;
}
sub regex {
my @new = @_;
s/$repla/$repla{ $1}/g for @new;
@new;
}
__DATA__
one spot was detected on each of the two blades
three jolly coachmen, three jolly coachmen
twelve to a dozen
a display of oneupmanship
three o'clock four o'clock five o'clock ROCK
------------------------------
Date: Tue, 05 Apr 2005 14:53:31 +0200
From: "Leendert Bottelberghs" <leendert@wouter.unitedknowledge.net>
Subject: Re: time zone offset calc with localtime and gmtime
Message-Id: <pan.2005.04.05.12.53.31.423426@wouter.unitedknowledge.net>
On Tue, 05 Apr 2005 00:46:45 -0700, Joe Smith wrote:
> Leendert Bottelberghs wrote:
>> I want to calculate the time zone offset (as an integer)
> The international standard for time zone offset is a five-character
> string. A plus or minus sign, two digits for hours, two digits for
> minutes.
Youre right. So I have to use POSIX to be able to calculate the difference
in seconds. I now have the following to calculate and format the time
offset:
<code>
# calculate the time difference in seconds;
my $secoffset = timelocal(localtime()) - timelocal(gmtime());
# translate it to minutes and apply the DLT difference
my $minoffset = ($secoffset / 60) + ((gmtime)[8] - (localtime)[8])*60;
# translate it to "hour-format", so that 90 will be 130,
# and -90 will be -130
my $tzoffset = int($minoffset/60)*100 +
($minoffset/abs($minoffset))*($minoffset%60);
# apply final formatting, including +/- sign and 4 digits.
my $tzstr = sprintf "%+05d", $tzoffset;
</code>
Maybe I'll use Date::Manip after all.
-leendert bottelberhs
------------------------------
Date: Tue, 5 Apr 2005 06:20:15 -0700
From: "Mothra" <mothra@nowhereatall.com>
Subject: Re: time zone offset calc with localtime and gmtime
Message-Id: <42528f8f$1@usenet.ugs.com>
"Leendert Bottelberghs" <leendert@wouter.unitedknowledge.net> wrote in
message news:pan.2005.04.05.12.53.31.423426@wouter.unitedknowledge.net...
> Maybe I'll use Date::Manip after all.
>
> -leendert bottelberhs
Yor are reinventing what we already have done in the DateTime Project
use strict;
use warnings;
use diagnostics;
use DateTime;
my $dt = DateTime->now(time_zone => 'America/Chicago');
print $dt->offset();
printed results
me.pl
-18000
Hope this helps
------------------------------
Date: Tue, 05 Apr 2005 15:57:38 +0200
From: "Leendert Bottelberghs" <leendert@wouter.unitedknowledge.net>
Subject: Re: time zone offset calc with localtime and gmtime
Message-Id: <pan.2005.04.05.13.57.37.593991@wouter.unitedknowledge.net>
On Tue, 05 Apr 2005 06:20:15 -0700, Mothra wrote:
>
> Yor are reinventing what we already have done in the DateTime Project
I know, and I'm not entirely happy with it. But there are good reasons for
it. First of all, I couldn't get the DateTime module installed with CPAN.
Besides the fact that it depends on about a douzen other modules (and it
installs about 60), it just wouldn't compile (running RH8, perl 5.8.0). It
returned:
<snippet>
/usr/bin/make -- NOT OK
Running make test
Can't test without successful make
Running make install
make had returned bad status, install seems impossible
</snippet>
The second reason I already mentioned: it depends on loads of other
modules. Since the module I'm writing is part of a larger project, I don't
want to force other people (on varying OSs) to install this large amount
of third-party modules.
> use strict;
> use warnings;
> use diagnostics;
> use DateTime;
>
>
> my $dt = DateTime->now(time_zone => 'America/Chicago');
>
> print $dt->offset();
Do you have to specify the timezone manually? And can the offset be
formatted in the "standard" timezone-offset way automatically with this
module?
Thanx for you time and response,
-leendert bottelberghs
------------------------------
Date: Tue, 5 Apr 2005 07:39:39 -0700
From: "Mothra" <mothra@nowhereatall.com>
Subject: Re: time zone offset calc with localtime and gmtime
Message-Id: <4252a22b$1@usenet.ugs.com>
"Leendert Bottelberghs" <leendert@wouter.unitedknowledge.net> wrote in
message news:pan.2005.04.05.13.57.37.593991@wouter.unitedknowledge.net...
> On Tue, 05 Apr 2005 06:20:15 -0700, Mothra wrote:
> >
> > Yor are reinventing what we already have done in the DateTime Project
>
> I know, and I'm not entirely happy with it. But there are good reasons for
> it. First of all, I couldn't get the DateTime module installed with CPAN.
> Besides the fact that it depends on about a dozen other modules (and it
> installs about 60), it just wouldn't compile (running RH8, perl 5.8.0). It
> returned:
Please report your failures to the DateTime Mailing list
datetime@perl.org if you are having problems installing DateTime
we need to know about it.
>
> The second reason I already mentioned: it depends on loads of other
> modules. Since the module I'm writing is part of a larger project, I don't
> want to force other people (on varying OSs) to install this large amount
> of third-party modules.
You would be better off installing DateTime that reinventing the wheel. As
far
as I know it is the only suite of modules that incorporate the Olson
Timezone
Database. Timezone conversion is fully supported (along with offsets)
>
> > use strict;
> > use warnings;
> > use diagnostics;
> > use DateTime;
> >
> >
> > my $dt = DateTime->now(time_zone => 'America/Chicago');
> >
> > print $dt->offset();
>
> Do you have to specify the timezone manually? And can the offset be
> formatted in the "standard" timezone-offset way automatically with this
> module?
Yes, you can use the naming convention provided with datetime or
you can specify an offset. I ran into a similar issue when writting the
tests
for the Sunrise module. I could not use the standard naming provided with
DateTime I had to use the offset, something like this
use strict;
use warnings;
use diagnostics;
use DateTime;
use DateTime::TimeZone;
use POSIX qw(floor ceil);
use vars qw($long $lat $offset $dt);
$dt = DateTime->now;
while (<DATA>) {
/(\w+),\s+(\w+)\s+(\d+)\s+(\d+)\s+(\w)\s+(\d+)\s+(\d+)\s+(\w)\s+sunrise:\s+(
\d+:\d+)\s+sunset:\s+(\d+:\d+)/;
$lat = sprintf( "%.3f", ( $3 + ( $4 / 60 ) ) );
$long = sprintf( "%.3f", -( $6 + ( $7 / 60 ) ) );
if ( $long < 0 ) {
$offset =
DateTime::TimeZone::offset_as_string( ceil( ( $long / 15 ) ) * 60
*
60 );
}
elsif ( $long > 0 ) {
$offset =
DateTime::TimeZone::offset_as_string( floor( ( $long / 15 ) ) * 60
*
60 );
}
}
$dt->set_time_zone($offset);
print $dt->offset;
__DATA__
Darwin, Australia 12 28 S 130 51 E sunrise: 05:36 sunset: 17:00
I hope this helps
------------------------------
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.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
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 7945
***************************************