[29951] in Perl-Users-Digest
Perl-Users Digest, Issue: 1194 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Jan 14 14:09:44 2008
Date: Mon, 14 Jan 2008 11:09:07 -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 Mon, 14 Jan 2008 Volume: 11 Number: 1194
Today's topics:
Re: Converting milliseconds to seconds <jurgenex@hotmail.com>
Cpan problems <jaford@watford53.freeserve.co.uk>
Re: Is script type="text/perl" OK? <joost@zeekat.nl>
Re: Is script type="text/perl" OK? <smallpond@juno.com>
Re: Perl - no longer open source and facing extinction <cwilbur@chromatico.net>
Re: s/A/B/ and s/B/C/ but don't want A -> C <ben@morrow.me.uk>
Shebang line management <SteveSpamTrap@yahoo.com>
Re: Shebang line management <1usa@llenroc.ude.invalid>
Re: Shebang line management <john@castleamber.com>
USEFUL perl SCRIPT: NTP verifier <ignoramus5390@NOSPAM.5390.invalid>
Re: USEFUL perl SCRIPT: NTP verifier <john@castleamber.com>
Re: USEFUL perl SCRIPT: NTP verifier <ignoramus5390@NOSPAM.5390.invalid>
Re: USEFUL perl SCRIPT: NTP verifier <john@castleamber.com>
Re: USEFUL perl SCRIPT: NTP verifier <daneNO@MORE.mk.SPAMtelcordia.com>
Re: USEFUL perl SCRIPT: NTP verifier <ignoramus5390@NOSPAM.5390.invalid>
Re: USEFUL perl SCRIPT: NTP verifier <usenet200712@tobyinkster.co.uk>
Re: USEFUL perl SCRIPT: NTP verifier <ignoramus5390@NOSPAM.5390.invalid>
Re: Wait for background processes to complete <pgodfrin@gmail.com>
Re: Wait for background processes to complete xhoster@gmail.com
Re: Wait for background processes to complete <pgodfrin@gmail.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Mon, 14 Jan 2008 14:38:43 GMT
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: Converting milliseconds to seconds
Message-Id: <jfsmo3l5ri6362h8l3eff45r1mukgji2sc@4ax.com>
Jürgen Exner <jurgenex@hotmail.com> wrote:
>Ilya Zakharevich <nospam-abuse@ilyaz.org> wrote:
>><jurgenex@hotmail.com>], who wrote in article <ik3go3pgkcrol2diigag1hl2prkke0ktt7@4ax.com>:
>>> >This is just a reflection of a sorry state of Perl documentation.
>>> >There is NO OTHER WAY to round-to-0 but to use int().
>>>
>>> Your statement is non sequitur. int() does not an never was supposed to
>>> round a number. If you expect int() to round a number then you are mistaken
>>> about the semantic of this function.
>>
>>I assure you that int() does nothing else but rounding-to-0.
>
>As I said: you are greatly mistaken about the intended semantic of int().
>You may want to read the documentation for clarification.
Ilya, you are right.
It would never have occured to me that the term "to round" could be used to
mean truncation of trailing digits without balancing. But according to
friends and Wikipedia this is quite common.
Please accept the appologies of a non-native English speaker.
jue
------------------------------
Date: Mon, 14 Jan 2008 18:34:22 GMT
From: Jim Ford <jaford@watford53.freeserve.co.uk>
Subject: Cpan problems
Message-Id: <MPG.21f5bb4cc4cd7d64989690@news.cable.ntlworld.com>
I've used the cpan command line to install modules on several Linux
boxes in the past with no problems - cpan has installed 'out of the
box' with little or no configuration.
I've now set-up a Ubuntu 7.10 system (perl 5.8.8) and am having problems
with cpan (1.7602). Initially cpan was unable to connect with a site -
it just hung. After removing the local .cpan directory and re-installing
perl I can't even get that far - I'm now getting problems with
initialising cpan with 'o conf init'. This particular problem is that
when it gets to asking me to input a list of continents (with no options
displayed), it just loops stating that whatever I input is not a valid
option. The only way to break out of the loop is with a ^C.
I'd welcome any suggestions to enable me to get cpan up and running!
Jim Ford
------------------------------
Date: Mon, 14 Jan 2008 15:33:00 +0100
From: Joost Diepenmaat <joost@zeekat.nl>
Subject: Re: Is script type="text/perl" OK?
Message-Id: <874pdgqxyb.fsf@zeekat.nl>
lg <lgt@invalid.com> writes:
> How would I do it with <IMG> tag
<img src="url-to-some-script-that-returns-an-image">
#!perl -w
use strict;
print "Content-type: image/png\n\n";
binmode STDOUT;
print $string_containing_png_data;
--
Joost.
------------------------------
Date: Mon, 14 Jan 2008 08:36:06 -0800 (PST)
From: smallpond <smallpond@juno.com>
Subject: Re: Is script type="text/perl" OK?
Message-Id: <ef848ca3-c64a-4593-869d-35f624962da9@21g2000hsj.googlegroups.com>
On Jan 14, 6:50 am, lg <l...@invalid.com> wrote:
> I've asked this on a html forum but I'll ask it here also.
>
> I have a counter file on my index page which is like below.
> Snippet from index.html:
> <script type="text/perl" SRC="http://www.blahblah.com/counter.pl"> </script>
>
> The script is is a perl file which is processed by the server. Is it legal? Will
> my counter work (be run as usual)?
>
> Below runs OK too, but I get a "done, but with errors on the page"
> <script type="text/javascript" SRC="http://www.blahblah.com/counter.pl">
> </script>
>
> What I am trying to do is get rid of the error message and have the counter.pl
> execute. Since the server (not the client browser) is doing the running of the
> counter.pl I want to know if "<script type="text/perl" SRC="http://.." is
> otherwise valid. I could just put the line there and see if I get hits, but I
> don't want to find out six months later that it while it does work on many
> browsers it does not work on not-so-common-browser and the browser simply quits
> interpreting the html on the file and issues a 404 or similar message.
If you want the perl code to run on the server and include its output
in
the page sent to the browser, then you need to make a server-side
include (SSI).
<!--#include virtual="/cgi-bin/counter.pl" -->
You will also have to enable SSI on the web server and name the file
index.shtml instead of index.html.
--S
------------------------------
Date: 14 Jan 2008 09:11:22 -0500
From: Charlton Wilbur <cwilbur@chromatico.net>
Subject: Re: Perl - no longer open source and facing extinction
Message-Id: <87odboxzsl.fsf@mithril.chromatico.net>
>>>>> "JC" == Jim Cochrane <allergic-to-spam@no-spam-allowed.org> writes:
JC> On 2008-01-02, Charlton Wilbur <cwilbur@chromatico.net> wrote:
>> Trolling, and she's got her hook firmly implanted in you.
>> Plonk her and spare us the noise, please.
JC> How do you know this person is female? Seems like an
JC> assumption to me.
She refers to herself as "gurl" in her nom de net and refers to
herself as a girl from time to time, and I see no reason to doubt it.
It's sufficiently damning to call her what she is; there's no need to
indulge in bizarre gender-related insults to demean her further,
especially as such puerile insults demean the person using them even
more.
Charlton
--
Charlton Wilbur
cwilbur@chromatico.net
------------------------------
Date: Mon, 14 Jan 2008 14:32:12 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: s/A/B/ and s/B/C/ but don't want A -> C
Message-Id: <cc8s55-kt8.ln1@osiris.mauzo.dyndns.org>
Quoth Michele Dondi <bik.mido@tiscalinet.it>:
> On 02 Jan 2008 17:06:56 GMT, Abigail <abigail@abigail.be> wrote:
>
> >[] The only work needed is to populate %reps, but that is more typing with
> >[] any approach :)
> >
> >Yeah, but now you're back to only being able to replace 'fixed strings',
> >while I posted by approach to be able to deal with patterns.
> >
> >[] With yours it's harder to build the match pattern, I think, that's why I
> >[] asked you if I'm misunderstanding it.
> >
> >Sure, it's harder than the "simple approach". But it's more powerful.
>
> I'm trying to follow this discussion and I'm not acquainted yet with
> the new constructs. Anyway the question is: how easy is it to
> construct a sub that given a list of patterns (and substitutions)
> would assemble a pattern satisfying the "condition of the Subject"?
If your strings don't contain ')', then it appears to be as easy as
my %repl = (
'ab?c' => 'ONE',
'xyz+' => 'TWO',
);
my $rx =
map qr/$_/,
'(?{})' . # bug in 5.10.0
join '|',
map "(*:$repl{$_})$_",
keys %repl;
our $REGMARK;
s/$rx/$REGMARK/g;
I don't know whether the fact that the 'NAME' part of the (*MARK:NAME)
constructs can contain anything except ')' is a bug or a feature, but it
works with 5.10.0. For arbitrary strings it would be safer to build a
second hash, giving each replacement a (safe) name and using that in the
(*:NAME) construction.
The way this works is it builds a pattern like (adding whitespace and
ignoring the initial (?{}) that shouldn't be there)
(*:ONE) ab?c | (*:TWO) xyz+
which will set $REGMARK to the (*:) in the branch that actually matched.
Ben
------------------------------
Date: Mon, 14 Jan 2008 12:51:30 -0500
From: Steve <SteveSpamTrap@yahoo.com>
Subject: Shebang line management
Message-Id: <5v1lp2F1jn5dpU1@mid.individual.net>
Are there any clever ways of dealing with portability of the "shebang"
line... when moving Perl scripts across machines (and across operating
systems), where the path to Perl may be different?
For instance, I'm stuck doing initial development of some CGI scripts
on a Windows machine, but then deploying them to a *nix Apache server.
For each script, I'm manually cut-n-pasting to replace the:
#!"c:/perl/bin/perl.exe"
... with:
#!/usr/bin/perl
I was about to put together a script that would parse all my files and
change the shebang line programmatically, but before doing so I wanted
to see if anyone else had a more elegant approach in their own environment.
------------------------------
Date: Mon, 14 Jan 2008 17:58:06 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Shebang line management
Message-Id: <Xns9A2583E9FC5D9asu1cornelledu@127.0.0.1>
Steve <SteveSpamTrap@yahoo.com> wrote in
news:5v1lp2F1jn5dpU1@mid.individual.net:
> Are there any clever ways of dealing with portability of the
> "shebang"
> line... when moving Perl scripts across machines (and across operating
> systems), where the path to Perl may be different?
>
> For instance, I'm stuck doing initial development of some CGI
> scripts
> on a Windows machine, but then deploying them to a *nix Apache server.
> For each script, I'm manually cut-n-pasting to replace the:
>
> #!"c:/perl/bin/perl.exe"
>
> ... with:
>
> #!/usr/bin/perl
>
> I was about to put together a script that would parse all my
> files and
> change the shebang line programmatically, but before doing so I wanted
> to see if anyone else had a more elegant approach in their own
> environment.
This has been asked and answered on this group many times. The question
is somewhat off-topic.
Anyway, myy recommendation is to always use #!/usr/bin/perl with
whatever options you need on the shebang line and read
http://httpd.apache.org/docs/2.2/mod/core.html#scriptinterpretersource
Sinan
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)
clpmisc guidelines: <URL:http://www.augustmail.com/~tadmc/clpmisc.shtml>
------------------------------
Date: 14 Jan 2008 18:18:47 GMT
From: John Bokma <john@castleamber.com>
Subject: Re: Shebang line management
Message-Id: <Xns9A257D40F6376castleamber@130.133.1.4>
Steve <SteveSpamTrap@yahoo.com> wrote:
> Are there any clever ways of dealing with portability of the
> "shebang"
> line... when moving Perl scripts across machines (and across operating
> systems), where the path to Perl may be different?
>
> For instance, I'm stuck doing initial development of some CGI
> scripts
> on a Windows machine, but then deploying them to a *nix Apache server.
> For each script, I'm manually cut-n-pasting to replace the:
>
> #!"c:/perl/bin/perl.exe"
>
> ... with:
>
> #!/usr/bin/perl
>
> I was about to put together a script that would parse all my
> files and
> change the shebang line programmatically, but before doing so I wanted
> to see if anyone else had a more elegant approach in their own
> environment.
I use Apache Ant [1]. It also can do stuff like packing all your files
together in a zip, etc. In my experience, the she-bang line might not
be the only thing you want to replace, nor that replacing is limited to
Perl (related) files
example:
<project name="YourProject" basedir=".">
<target name="dist">
<delete dir="build"/>
<mkdir dir="build"/>
<copy file="cgi-bin/script.cgi" todir="build/cgi-bin"/>
<replace
file="build/cgi-bin/script.cgi"
token="#!perl"
value="#!/usr/bin/perl"
/>
<tstamp>
<format property="time.stamp"
pattern="yyyyMMdd-kkmmss"
locale="en"/>
</tstamp>
<delete dir="dist"/>
<mkdir dir="dist"/>
<zip basedir="build"
destfile="dist/${ant.project.name}-${time.stamp}.zip"/>
<delete dir="build"/>
</target>
</project>
ant dist
[1] http://ant.apache.org/
--
John
------------------------------
Date: Mon, 14 Jan 2008 10:44:08 -0600
From: Ignoramus5390 <ignoramus5390@NOSPAM.5390.invalid>
Subject: USEFUL perl SCRIPT: NTP verifier
Message-Id: <RPCdnRhaNsLFDBbanZ2dnUVZ_vmlnZ2d@giganews.com>
Over the years, I have had many problems with NTPd stopping to work
without any notice. That always upset me. I finally wrote a perl
script that checks times among various servers and computes a time
offset for each of them. It can either report that, or it can also set
exit code based on whether all these offsets are below a user
specified limit.
To run it, say query-ntp host1 host2 etc
A good practice is to specify localhost as host1, pool.ntp.org as
host2, and whatever other machines you are checking, would follow
after that.
i
######################################################################
#!/usr/bin/perl
# See Also:
# http://www.webreference.com/programming/perl/ntp/
# http://www.webreference.com/programming/perl/ntp/2.html
#
# Copyright(C) Igor Chudov, 2008.
# Released under GNU Public License version 3.
#
use strict; use warnings;
use Data::Dumper;
use Getopt::Long;
use Net::NTP;
use Time::HiRes qw( time );
my $detail = undef;
my $limit = undef;
GetOptions(
"detail!" => \$detail,
"limit=f" => \$limit,
);
my @servers = @ARGV;
my $bad = undef;
foreach my $server (@servers) {
my $t0 = time;
my %h = get_ntp_response( $server );
#Timestamp Name ID When Generated
#------------------------------------------------------------
#Originate Timestamp T1 time request sent by client
#Receive Timestamp T2 time request received by server
#Transmit Timestamp T3 time reply sent by server
#Destination Timestamp T4 time reply received by client
#
#The roundtrip delay d and local clock offset t are defined as
#
#d = (T4 - T1) - (T2 - T3) t = ((T2 - T1) + (T3 - T4)) / 2
my $T1 = $t0; # $h{'Originate Timestamp'};
my $T2 = $h{'Receive Timestamp'};
my $T3 = $h{'Transmit Timestamp'};
my $T4 = time; # From Time::HiRes! Accurate to usec!
#print "T4=$T4\n";
my $d = ($T4 - $T1) - ($T2 - $T3);
my $t = (($T2 - $T1) + ($T3 - $T4)) / 2;
my $duration = $T4-$t0;
my $delta = $T2-$T1 - $duration/2;
if( $limit ) {
if( $delta > $limit || -$delta < -$limit) {
print "Server $server OFF by $delta seconds!!!\n";
$bad = 1;
} else {
print "Server $server OK, delta is $delta seconds!!!\n";
}
} else {
if( $detail ) {
print Dumper( \%h ) . "\nt0=$t0, T4=$T4\ndelay=$d, offset=$t\nelapsed=$duration, delta=$delta\n\n";
} else {
#print "$server delay=$d, offset=$t, delta=$delta, duration=$duration\n";
print sprintf( "%-30s %.5f\n", $server, $t );
}
}
}
exit 1 if $bad;
------------------------------
Date: 14 Jan 2008 17:21:40 GMT
From: John Bokma <john@castleamber.com>
Subject: Re: USEFUL perl SCRIPT: NTP verifier
Message-Id: <Xns9A25739231DF9castleamber@130.133.1.4>
Ignoramus5390 <ignoramus5390@NOSPAM.5390.invalid> wrote:
> use strict; use warnings;
You might want each on a line of its own.
>
> use Data::Dumper;
> use Getopt::Long;
> use Net::NTP;
> use Time::HiRes qw( time );
>
> my $detail = undef;
> my $limit = undef;
undef is the default value, I wouldn't assign it explicitly.
> GetOptions(
> "detail!" => \$detail,
> "limit=f" => \$limit,
> );
>
> my @servers = @ARGV;
you might want to do some checking here, and report a usage message.
Also, I recommend to support at least -h / --help.
> foreach my $server (@servers) {
> my $t0 = time;
> my %h = get_ntp_response( $server );
>
>
> #Timestamp Name ID When Generated
> #------------------------------------------------------------
> #Originate Timestamp T1 time request sent by client
> #Receive Timestamp T2 time request received by server
> #Transmit Timestamp T3 time reply sent by server
> #Destination Timestamp T4 time reply received by client
If you need to explain what IDs mean it clearly shows that the names of
your variables are not chosen well.
> #
> #The roundtrip delay d and local clock offset t are defined as
> #
> #d = (T4 - T1) - (T2 - T3) t = ((T2 - T1) + (T3 - T4)) / 2
>
> my $T1 = $t0; # $h{'Originate Timestamp'};
^^^^ remove commented out cruft
> my $T2 = $h{'Receive Timestamp'};
> my $T3 = $h{'Transmit Timestamp'};
>
> my $T4 = time; # From Time::HiRes! Accurate to usec!
>
> #print "T4=$T4\n";
don't leave cruft in like that. If you need debugging, use a --debug
option, and use something like:
$debug and print ....
> my $d = ($T4 - $T1) - ($T2 - $T3);
my $roundtrip_delay =
> my $t = (($T2 - $T1) + ($T3 - $T4)) / 2;
my $local_clock_offset = ...
> my $duration = $T4-$t0;
> my $delta = $T2-$T1 - $duration/2;
>
> if( $limit ) {
> if( $delta > $limit || -$delta < -$limit) {
> print "Server $server OFF by $delta seconds!!!\n";
> $bad = 1;
>
> } else {
> print "Server $server OK, delta is $delta seconds!!!\n";
> }
>
> } else {
Note that the else here makes that $detail and $limit are mutual
exclusive. Sounds odd to me. But if that's what you want, instead of
setting $bad to 1, and do a test at the end, you could just have used:
die "Server .... OFF ... \n";
note that die also sets conveniently the exit value...
> if( $detail ) {
> print Dumper( \%h ) . "\nt0=$t0, T4=$T4\ndelay=$d,
> offset=$t\nelapsed=$duration, delta=$delta\n\n";
> } else {
> #print "$server delay=$d, offset=$t, delta=$delta,
> duration=$duration\n"; print sprintf( "%-30s %.5f\n", $server,
> $t );
> }
> }
> }
>
> exit 1 if $bad;
--
John Bokma http://johnbokma.com/
------------------------------
Date: Mon, 14 Jan 2008 11:45:06 -0600
From: Ignoramus5390 <ignoramus5390@NOSPAM.5390.invalid>
Subject: Re: USEFUL perl SCRIPT: NTP verifier
Message-Id: <DJydnYv5Jek_AhbanZ2dnUVZ_ozinZ2d@giganews.com>
On 2008-01-14, John Bokma <john@castleamber.com> wrote:
> Ignoramus5390 <ignoramus5390@NOSPAM.5390.invalid> wrote:
>
>
>> use strict; use warnings;
>
> You might want each on a line of its own.
ok
>>
>> use Data::Dumper;
>> use Getopt::Long;
>> use Net::NTP;
>> use Time::HiRes qw( time );
>>
>> my $detail = undef;
>> my $limit = undef;
>
> undef is the default value, I wouldn't assign it explicitly.
I like this style. Makes it very explicit.
>> GetOptions(
>> "detail!" => \$detail,
>> "limit=f" => \$limit,
>> );
>>
>> my @servers = @ARGV;
>
> you might want to do some checking here, and report a usage message.
done
> Also, I recommend to support at least -h / --help.
Is there some standard way of doing it?
>> foreach my $server (@servers) {
>> my $t0 = time;
>> my %h = get_ntp_response( $server );
>>
>>
>> #Timestamp Name ID When Generated
>> #------------------------------------------------------------
>> #Originate Timestamp T1 time request sent by client
>> #Receive Timestamp T2 time request received by server
>> #Transmit Timestamp T3 time reply sent by server
>> #Destination Timestamp T4 time reply received by client
>
> If you need to explain what IDs mean it clearly shows that the names of
> your variables are not chosen well.
They come from NTP specification.
>
>> #
>> #The roundtrip delay d and local clock offset t are defined as
>> #
>> #d = (T4 - T1) - (T2 - T3) t = ((T2 - T1) + (T3 - T4)) / 2
>>
>> my $T1 = $t0; # $h{'Originate Timestamp'};
>
> ^^^^ remove commented out cruft
>
>
>> my $T2 = $h{'Receive Timestamp'};
>> my $T3 = $h{'Transmit Timestamp'};
>>
>> my $T4 = time; # From Time::HiRes! Accurate to usec!
>>
>> #print "T4=$T4\n";
>
> don't leave cruft in like that. If you need debugging, use a --debug
> option, and use something like:
>
> $debug and print ....
ok
>> my $d = ($T4 - $T1) - ($T2 - $T3);
>
> my $roundtrip_delay =
>
>> my $t = (($T2 - $T1) + ($T3 - $T4)) / 2;
>
> my $local_clock_offset = ...
changed
>> my $duration = $T4-$t0;
>> my $delta = $T2-$T1 - $duration/2;
>>
>> if( $limit ) {
>> if( $delta > $limit || -$delta < -$limit) {
>> print "Server $server OFF by $delta seconds!!!\n";
>> $bad = 1;
>>
>> } else {
>> print "Server $server OK, delta is $delta seconds!!!\n";
>> }
>>
>> } else {
>
> Note that the else here makes that $detail and $limit are mutual
> exclusive. Sounds odd to me. But if that's what you want, instead of
> setting $bad to 1, and do a test at the end, you could just have used:
>
> die "Server .... OFF ... \n";
I want all bad servers reported, hence delayed dying.
> note that die also sets conveniently the exit value...
Yes, but I want to be a little clear since I want to go to the end.
######################################################################
#!/usr/bin/perl
# See Also:
# http://www.webreference.com/programming/perl/ntp/
# http://www.webreference.com/programming/perl/ntp/2.html
#
# Copyright(C) Igor Chudov, 2008.
# Released under GNU Public License version 3.
#
use strict;
use warnings;
use Data::Dumper;
use Getopt::Long;
use Net::NTP;
use Time::HiRes qw( time );
my $detail = undef;
my $limit = undef;
GetOptions(
"detail!" => \$detail,
"limit=f" => \$limit,
);
die "detail and limit are mutually exclusive" if $detail and $limit;
my @servers = @ARGV;
my $bad = undef;
foreach my $server (@servers) {
my $t0 = time;
my %h = get_ntp_response( $server );
#Timestamp Name ID When Generated
#------------------------------------------------------------
#Originate Timestamp T1 time request sent by client
#Receive Timestamp T2 time request received by server
#Transmit Timestamp T3 time reply sent by server
#Destination Timestamp T4 time reply received by client
#
#The roundtrip delay d and local clock offset t are defined as
#
#d = (T4 - T1) - (T2 - T3) t = ((T2 - T1) + (T3 - T4)) / 2
my $T1 = $t0;
my $T2 = $h{'Receive Timestamp'};
my $T3 = $h{'Transmit Timestamp'};
my $T4 = time; # From Time::HiRes! Accurate to usec!
my $roundtrip_delay = ($T4 - $T1) - ($T2 - $T3);
my $local_clock_offset = (($T2 - $T1) + ($T3 - $T4)) / 2;
my $duration = $T4-$t0;
my $delta = $T2-$T1 - $duration/2;
if( $limit ) {
if( $delta > $limit || -$delta < -$limit) {
print "Server $server OFF by $delta seconds!!!\n";
$bad = 1;
} else {
print "Server $server OK, delta is $delta seconds!!!\n";
}
} else {
if( $detail ) {
print Dumper( \%h ) . "\nt0=$t0, T4=$T4\ndelay=$roundtrip_delay, offset=$local_clock_offset\nelapsed=$duration, delta=$delta\n\n";
} else {
#print "$server delay=$roundtrip_delay, offset=$local_clock_offset, delta=$delta, duration=$duration\n";
print sprintf( "%-30s %.5f\n", $server, $local_clock_offset );
}
}
}
exit 1 if $bad;
------------------------------
Date: 14 Jan 2008 18:10:25 GMT
From: John Bokma <john@castleamber.com>
Subject: Re: USEFUL perl SCRIPT: NTP verifier
Message-Id: <Xns9A257BD54E636castleamber@130.133.1.4>
Ignoramus5390 <ignoramus5390@NOSPAM.5390.invalid> wrote:
> On 2008-01-14, John Bokma <john@castleamber.com> wrote:
[..]
>> undef is the default value, I wouldn't assign it explicitly.
>
> I like this style. Makes it very explicit.
IMO Perl programmers should know that the default is undef, and hence no
need to assign undef. To confuse matters a bit more (IMO), you set $bad
to undef, yet you assign later on 1 to it (in case of an error), and at
the end of the script you test if $bad .... I try to avoid testing for
undef that way, and I try to avoid to use 1/undef as a true/false. In
short, I would've used something like:
my $server_limit_out_of_range = 0;
:
:
if ....
$server_limit_out_of_range = 1;
:
:
$server_limit_out_of_range and exit 1;
ditto for $detail (which is a flag).
As for limit, I probably would pick a sensible default instead of undef.
>> Also, I recommend to support at least -h / --help.
>
> Is there some standard way of doing it?
Perl Best Practices (recommended) recommends:
--usage - one line
--help - --usage line followed by one-line
summary of each option (in my experience
the one-line is to strict)
--version
--man - complete documentation of the program
>> If you need to explain what IDs mean it clearly shows that the names
>> of your variables are not chosen well.
>
> They come from NTP specification.
OK, clear. That's something you've read to write this, but someone who
reads your program does not always have to be familiar with this
documentation, nor has to read it first IMO.
my $roundtrip_delay = ($T4 - $T1) - ($T2 - $T3);
v.s.
my $roundtrip_delay = ( $destination_ts - $originate_ts )
- ( $receive_ts - $transmit_ts );
which is more readable?
>> Note that the else here makes that $detail and $limit are mutual
>> exclusive. Sounds odd to me. But if that's what you want, instead of
>> setting $bad to 1, and do a test at the end, you could just have
>> used:
>>
>> die "Server .... OFF ... \n";
>
> I want all bad servers reported, hence delayed dying.
My bad, I missed the additional }
In that case I probably would have used (warnings go to STDERR):
warn "Server ... \n";
next;
}
to keep it functionally the same. OTOH, I can't see why $limit and
$detail have to be mutually exclusive.
> if( $limit ) {
> if( $delta > $limit || -$delta < -$limit) {
if ( abs( $delta ) > $limit ) {
^ note the extra space, /if/ is not a function
--
John
http://johnbokma.com/mexit/
------------------------------
Date: Mon, 14 Jan 2008 18:52:19 GMT
From: Dan Espen <daneNO@MORE.mk.SPAMtelcordia.com>
Subject: Re: USEFUL perl SCRIPT: NTP verifier
Message-Id: <ic8x2sjl3z.fsf@mk.telcordia.com>
Ignoramus5390 <ignoramus5390@NOSPAM.5390.invalid> writes:
> Over the years, I have had many problems with NTPd stopping to work
> without any notice. That always upset me.
Me too, but I took a different approach.
In root's crontab:
35 * * * * /root/scripts/ntp-check.sh
Content of script:
x=`/etc/rc.d/init.d/ntpd status`
case $x in
*stopped*|*dead*)
status=`/etc/rc.d/init.d/ntpd start 2>&1`
printf "had to start ntpd on `uname -n`, state <$x>\
\nrestart got status\n $status" | mail -s cron root
;;
esac
------------------------------
Date: Mon, 14 Jan 2008 12:57:24 -0600
From: Ignoramus5390 <ignoramus5390@NOSPAM.5390.invalid>
Subject: Re: USEFUL perl SCRIPT: NTP verifier
Message-Id: <DoqdnRT-c5EJLRbanZ2dnUVZ_sTinZ2d@giganews.com>
On 2008-01-14, Dan Espen <daneNO@MORE.mk.SPAMtelcordia.com> wrote:
> Ignoramus5390 <ignoramus5390@NOSPAM.5390.invalid> writes:
>
>> Over the years, I have had many problems with NTPd stopping to work
>> without any notice. That always upset me.
>
> Me too, but I took a different approach.
> In root's crontab:
>
> 35 * * * * /root/scripts/ntp-check.sh
>
> Content of script:
>
> x=`/etc/rc.d/init.d/ntpd status`
> case $x in
> *stopped*|*dead*)
> status=`/etc/rc.d/init.d/ntpd start 2>&1`
> printf "had to start ntpd on `uname -n`, state <$x>\
> \nrestart got status\n $status" | mail -s cron root
> ;;
> esac
Yeah, and what happens when it is alive, but does not talk to any
timeservers?
i
------------------------------
Date: Mon, 14 Jan 2008 18:36:37 +0000
From: Toby A Inkster <usenet200712@tobyinkster.co.uk>
Subject: Re: USEFUL perl SCRIPT: NTP verifier
Message-Id: <lmms55-i0o.ln1@ophelia.g5n.co.uk>
Ignoramus5390 wrote:
> my @servers = @ARGV;
die "Usage: query-ntp [--detail] [--limit=N] host1 host2 [host3 ...]\n"
unless ($#servers > 1);
--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.17.14-mm-desktop-9mdvsmp, up 15 days, 5:47.]
NetSol Cybersquatting
http://tobyinkster.co.uk/blog/2008/01/10/netsol-cybersquatting/
------------------------------
Date: Mon, 14 Jan 2008 13:03:23 -0600
From: Ignoramus5390 <ignoramus5390@NOSPAM.5390.invalid>
Subject: Re: USEFUL perl SCRIPT: NTP verifier
Message-Id: <C5ydnR9QyZNmLBbanZ2dnUVZ_oWdnZ2d@giganews.com>
On 2008-01-14, Toby A Inkster <usenet200712@tobyinkster.co.uk> wrote:
> Ignoramus5390 wrote:
>
>> my @servers = @ARGV;
>
> die "Usage: query-ntp [--detail] [--limit=N] host1 host2 [host3 ...]\n"
> unless ($#servers > 1);
>
Thanks. I changed it to:
die "Usage: query-ntp [--detail] [--limit=N] host1 host2 [host3 ...]\n"
unless (@servers);
Thank you
i
------------------------------
Date: Mon, 14 Jan 2008 07:37:35 -0800 (PST)
From: pgodfrin <pgodfrin@gmail.com>
Subject: Re: Wait for background processes to complete
Message-Id: <3cb89e50-a6ab-433a-b3ca-9d8d6f15cfcc@d70g2000hsb.googlegroups.com>
On Jan 13, 9:43 pm, xhos...@gmail.com wrote:
> pgodfrin <pgodf...@gmail.com> wrote:
> > Greetings,
> > Well - I've spent a bunch of time trying to figure this out - to no
> > avail.
>
> > Here's what I want to do - run several commands in the background and
> > have the perl program wait for the commands to complete. Fork doesn't
> > do it, nor does wait nor waitpid.
>
> None of them individually do it, no. You have to use them together.
>
>
>
> > Any thoughts?
>
> > Here's a sample program which starts the processes:
>
> > while (<*.txt>)
> > {
> > print "Copying $_ \n";
> > system("cp $_ $_.old &") ;
>
> This starts a shell, which then starts cp in the background. As soon as
> the cp is *started*, the shell exits. So Perl has nothing to wait for, as
> the shell is already done (and waited for) before system returns. You need
> to use fork and system or fork and exec. Or you could use
> Parallel::ForkManager, which will wrap this stuff up nicely for you and
> also prevent you from fork-bombing your computer if there are thousands of
> *.txt
>
> > }
> > print "End of excercise\n";
> > exit;
>
> 1 until -1==wait(); # on my system, yours may differ
>
> Xho
>
> --
> --------------------http://NewsReader.Com/--------------------
> The costs of publication of this article were defrayed in part by the
> payment of page charges. This article must therefore be hereby marked
> advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
> this fact.
Hi Xho,
Well - It seems you're on to something. Thanks. I have tested this
using system and fork (also exec) and it appears to be exactly what
you're saying - the pid returned by fork is not the pid of the cp
command.
Frankly I'm amazed that something as basic as this, handled easily by
a plain old shell, is not an easy thing in Perl. (no I don't want to
use a shell script for this - I'm a perl bigot :) ).
I'll take a look at Parallel::ForkManager and any other modules that
might be useful...
cheers,
pg
------------------------------
Date: 14 Jan 2008 17:11:53 GMT
From: xhoster@gmail.com
Subject: Re: Wait for background processes to complete
Message-Id: <20080114121154.841$BK@newsreader.com>
pgodfrin <pgodfrin@gmail.com> wrote:
> On Jan 13, 9:43 pm, xhos...@gmail.com wrote:
> > pgodfrin <pgodf...@gmail.com> wrote:
> > > Greetings,
> > > Well - I've spent a bunch of time trying to figure this out - to no
> > > avail.
> >
> > > Here's what I want to do - run several commands in the background and
> > > have the perl program wait for the commands to complete. Fork doesn't
> > > do it, nor does wait nor waitpid.
> >
> > None of them individually do it, no. You have to use them together.
> >
> >
> >
> > > Any thoughts?
> >
> > > Here's a sample program which starts the processes:
> >
> > > while (<*.txt>)
> > > {
> > > print "Copying $_ \n";
> > > system("cp $_ $_.old &") ;
> >
> > This starts a shell, which then starts cp in the background. As soon
> > as the cp is *started*, the shell exits. So Perl has nothing to wait
> > for, as the shell is already done (and waited for) before system
> > returns. You need to use fork and system or fork and exec. Or you
> > could use Parallel::ForkManager, which will wrap this stuff up nicely
> > for you and also prevent you from fork-bombing your computer if there
> > are thousands of *.txt
> >
> > > }
> > > print "End of excercise\n";
> > > exit;
> >
> > 1 until -1==wait(); # on my system, yours may differ
> >
>
> Well - that's beginning to make a little sense - the shell completes
> and perl has nothing to wait for. No wonder I'm pulling out what
> little of my hair is left! :) I guess the fork process returns the pid
> of the process, but - if it's the pid of the shell process, then we're
> back to square one.
The fork returns (to the parent) the pid of the process forked off.
(but you don't actually need to know the pid if you merely want to wait,
rather than waitpid.) If that forked-off process then itself starts the cp
in the background, of course you are no better off. But if the forked-off
process either becomes cp (using exec) or it starts up cp in the foreground
(using system without a "&"), then you now have something to wait for. In
the first case, you wait for cp itself. In the second case, you wait for
the forked-off perl process which is itself waiting for the cp.
$ perl -wle 'use strict; fork or exec "sleep " . $_*3 foreach 1..3 ; \
my $x; do {$x=wait; print $x} until $x==-1'
438
439
440
-1
Xho
--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
------------------------------
Date: Mon, 14 Jan 2008 11:05:26 -0800 (PST)
From: pgodfrin <pgodfrin@gmail.com>
Subject: Re: Wait for background processes to complete
Message-Id: <a7e5e1f4-ceb5-4550-8481-2a626d7a0619@c23g2000hsa.googlegroups.com>
On Jan 14, 11:11 am, xhos...@gmail.com wrote:
> pgodfrin <pgodf...@gmail.com> wrote:
> > On Jan 13, 9:43 pm, xhos...@gmail.com wrote:
> > > pgodfrin <pgodf...@gmail.com> wrote:
> > > > Greetings,
> > > > Well - I've spent a bunch of time trying to figure this out - to no
> > > > avail.
>
> > > > Here's what I want to do - run several commands in the background and
> > > > have the perl program wait for the commands to complete. Fork doesn't
> > > > do it, nor does wait nor waitpid.
>
> > > None of them individually do it, no. You have to use them together.
>
> > > > Any thoughts?
>
> > > > Here's a sample program which starts the processes:
>
> > > > while (<*.txt>)
> > > > {
> > > > print "Copying $_ \n";
> > > > system("cp $_ $_.old &") ;
>
> > > This starts a shell, which then starts cp in the background. As soon
> > > as the cp is *started*, the shell exits. So Perl has nothing to wait
> > > for, as the shell is already done (and waited for) before system
> > > returns. You need to use fork and system or fork and exec. Or you
> > > could use Parallel::ForkManager, which will wrap this stuff up nicely
> > > for you and also prevent you from fork-bombing your computer if there
> > > are thousands of *.txt
>
> > > > }
> > > > print "End of excercise\n";
> > > > exit;
>
> > > 1 until -1==wait(); # on my system, yours may differ
>
> > Well - that's beginning to make a little sense - the shell completes
> > and perl has nothing to wait for. No wonder I'm pulling out what
> > little of my hair is left! :) I guess the fork process returns the pid
> > of the process, but - if it's the pid of the shell process, then we're
> > back to square one.
>
> The fork returns (to the parent) the pid of the process forked off.
> (but you don't actually need to know the pid if you merely want to wait,
> rather than waitpid.) If that forked-off process then itself starts the cp
> in the background, of course you are no better off. But if the forked-off
> process either becomes cp (using exec) or it starts up cp in the foreground
> (using system without a "&"), then you now have something to wait for. In
> the first case, you wait for cp itself. In the second case, you wait for
> the forked-off perl process which is itself waiting for the cp.
>
> $ perl -wle 'use strict; fork or exec "sleep " . $_*3 foreach 1..3 ; \
> my $x; do {$x=wait; print $x} until $x==-1'
> 438
> 439
> 440
> -1
>
> Xho
>
> --
> --------------------http://NewsReader.Com/--------------------
> The costs of publication of this article were defrayed in part by the
> payment of page charges. This article must therefore be hereby marked
> advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
> this fact.
Hi Xho,
Well - your code and concepts work fine when you want to wait
sequentially. My goal here is to fire off x number of process and then
wait for ALL of them to complete (this is basically rudimentary job
control, trying to use the shell concepts and maximize parallelism).
So, that requires the use of the & to send a process to the
background. It looks like exec doesn't recognize the &, which leaves
the system() command - which leaves us back at square one. The shell
completes and the program has nothing to wait for.
What's interesting is by using the system(" ... &") construct, the
background tasks all get the same PGID, which makes me wonder if
waitpid(0, <flags> ) should wait for wait for process in the process
group of the main perl program...
I wonder...
smiles,
pg
------------------------------
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 V11 Issue 1194
***************************************