[28452] in Perl-Users-Digest
Perl-Users Digest, Issue: 9816 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Oct 7 09:05:49 2006
Date: Sat, 7 Oct 2006 06:05:05 -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 Sat, 7 Oct 2006 Volume: 10 Number: 9816
Today's topics:
Re: backreference oddity <rvtol+news@isolution.nl>
Re: backreference oddity <nobull67@gmail.com>
Re: Complex regular expression <bart@nijlen.com>
Re: Dealing with a STRANGE API <ignoramus7272@NOSPAM.7272.invalid>
Re: Hard or Easy? To find string, then grab criterion m <nobull67@gmail.com>
Re: If gethostbyname fails it never recovers <sisyphus1@nomail.afraid.org>
Merits vs costs of Structured Programming Dogma yankeeinexile@gmail.com
new CPAN modules on Sat Oct 7 2006 (Randal Schwartz)
Re: newbie cspan example question <benmorrow@tiscali.co.uk>
Re: newbie structure question <hjp-usenet2@hjp.at>
Re: Notify parent that a background process failed <hjp-usenet2@hjp.at>
Re: Regarding numeric literals <hjp-usenet2@hjp.at>
Regex help for URL <solive@gmail.com>
Re: Regex help for URL anno4000@radom.zrz.tu-berlin.de
Re: Regex help for URL rcia@davidfilmer.com
Re: Script that shows IP address of a server. <nobull67@gmail.com>
Re: Script that shows IP address of a server. <superbaby@myjaring.net>
Re: Unit-testing and mock objects <eugene.morozov@gmail.com>
Re: Unit-testing and mock objects anno4000@radom.zrz.tu-berlin.de
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sat, 7 Oct 2006 11:03:41 +0200
From: "Dr.Ruud" <rvtol+news@isolution.nl>
Subject: Re: backreference oddity
Message-Id: <eg81la.26c.1@news.isolution.nl>
poncenby schreef:
> i have a file which has lines of text with fields separated by a
> space. some of the fields are prefixed with a number and a space,
> like the line below...
>
> bar1 bar2 XX 10 bar3tooten
> foo1 foo2 XX 15 foo3uptofifteen
>
> as you can see, the numbers (10 and 15) are the length of the field
> after the number.
Are these meant for fields with embedded blanks? If not, see split().
> so i want to use these numbers as length specifier to match the field
> after the number, with a regex like either of these:
>
> /(.+)\s(.+)\sXX\s([0-9)+)\s(.{$3})/
In addition to the other comments: the "(.+)\s" might first match up to
the last space, and backtrack from there. Change to "(\S+)\s", or to
"(.+?)\s".
--
Affijn, Ruud
"Gewoon is een tijger."
------------------------------
Date: 7 Oct 2006 02:49:47 -0700
From: "Brian McCauley" <nobull67@gmail.com>
Subject: Re: backreference oddity
Message-Id: <1160214587.812804.84890@i42g2000cwa.googlegroups.com>
On Oct 7, 1:58 am, Eric Amick <eric-am...@comcast.net> wrote:
> Repeat counts in curly
> brackets have to be constants. Try this and see what I mean:
>
> perl -Mre=debug -e "/(.+)\s(.{\1})/"
You can use (??{})
/ (.+) \s ( (??{ ".{$1}" }) )/x
But this is neither vert readable nor very efficient.
------------------------------
Date: 7 Oct 2006 01:13:19 -0700
From: "Bart Van der Donck" <bart@nijlen.com>
Subject: Re: Complex regular expression
Message-Id: <1160208799.249010.18320@m73g2000cwd.googlegroups.com>
attn.steven.kuo@gmail.com wrote:
> jayanthigk2004@yahoo.com wrote:
> > Is it possible to write a regular expression for this ?
> >
> > Pattern: 999-99-999
> >
> >
> > Where 9 is any number from 0 to 9
> >
> >
> > However the user need not enter ALL the digits and dashes as given in
> > the format.
> >
> >
> > Whatever numbers and dashes he had entered must match the above format,
> >
> > from left to right, for only the charcters he has entered.
> >
> >
> > For example
> > 9
> > 99
> > 999
> > 999-
> > 999-9
> > 999-99
> > 999-99-
> > 999-99-9
> > 999-99-99
> > 999-99-999
> >
> >
> > Any of the above should result in a match
> >
> >
> > Next, he can also put * before or after or before and after any of the
> > above combination
>
>
> One way could be to construct the regular expression
> based on the length of the target string itself:
>
>
> my @sequence = (
> '',
> ('\d') x 3,
> '[-]',
> ('\d') x 2,
> '[-]',
> ('\d') x 3
> );
>
>
> while (<DATA>)
> {
> chomp;
> if (length and /^\*?(??{
> no warnings 'uninitialized';
> join '', @sequence[0 .. length() - pos()], '?'})\*?$/)
> {
> print "$_ matches\n";
> }
> else
> {
> print "$_ does NOT match\n";
> }
> }
>
> __DATA__
> 9
> *9
> 9*
> *99-
> 999-
> 999*
> z
> 999-99
> 999-990
> 999-99*
> *999-990*
> 999-99-999
> 999-99-9999
That still allows '**' (says: Matches) or '*' (gives error).
--
Bart
------------------------------
Date: Sat, 7 Oct 2006 04:30:47 +0000 (UTC)
From: Ignoramus7272 <ignoramus7272@NOSPAM.7272.invalid>
Subject: Re: Dealing with a STRANGE API
Message-Id: <c402p.1b6.17.1@news.alt.net>
On 6 Oct 2006 23:12:19 GMT, anno4000@radom.zrz.tu-berlin.de <anno4000@radom.zrz.tu-berlin.de> wrote:
> Ignoramus7272 <ignoramus7272@NOSPAM.7272.invalid> wrote in comp.lang.perl.misc:
>> I am using PayPal API class
>>
>> Business::PayPal::API::TransactionSearch
>>
>> It uses some very weird code
>
> Indeed. Snipped.
>
>> that is, it returns a hash when there is error, and a list reference
>> when there is not.
>>
>> I am bewildered, just how can I get this result into one variable and
>> then figure out if I am dealing with success (so that I can print
>> results from the list reference), or failure (so that I can print a
>> hash element error message).
>>
>> Any idea? I tried obvious things like setting a scalar to the result
>> of this function, and could not.
>
> Untested:
>
> my @mystery = $obj->meth_from_hell( ...);
>
> if ( @mystery == 1 ) {
> my @array = @{ $mystery[ 0] };
> # normal processing
> } else {
> my %hash = @mystery;
> # error
> }
>
> The distinction is reliable because the hash returns an even number of
> scalars.
>
> You shouldn't have to do this.
>
> Anno
OK, thanks. I used your code to write this:
sub run_weird_method {
my ($sub, @args) = @_;
my @mystery = &$sub( @args );
if ( @mystery == 1 ) {
# normal processing
return { success => 1, result => $mystery[0] };
}
# Failure
my %hash = @mystery;
return { success => 0, result => \%hash };
}
I will also look into using other modules that are more sanity
compliant, though there do not seem to be any.
The whole thing is a little strange, returning errors like "too many
results". I will have to look more. I want to pull all my paypal
transaction into a database.
i
------------------------------
Date: 7 Oct 2006 04:09:49 -0700
From: "Brian McCauley" <nobull67@gmail.com>
Subject: Re: Hard or Easy? To find string, then grab criterion matched lines above and below?
Message-Id: <1160219389.213002.262320@i3g2000cwc.googlegroups.com>
On Oct 6, 5:03 pm, sam...@mytrashmail.com wrote:
>
> Here is my perl script solution so far - at the end of this post.
A very good start.
> I am a crude programmer and so I can't attest for the elegance of this
> code or if it's even close to efficient. This code is bits and pieces I
> have put together looking at examples on the net. It mostly works.
I've put "use strict" and "use warnings" at the top and used lexical
variables and put or die on all your open()s. This makes your code look
a lot less crude.
Oh, and I simplified a couple of the regex.
> I haven't been able to get it to put both the %%before and %%after
> sections into the csv file.
Right, you had two parallel hashes. If you added two more you've have
four. By my "rule of three" that means you'd be doing it wrong.
I have replaced your paralell hashes by a has containing records. The
record is actually implemented as a hash, but conceptually it's being
(ab)uses as a record structure not an associative array. This is normal
practice in Perl.
It also actually makes the code simpler because I can attach the record
into the container hash before it's complete.
> I would also like to pull the descriptive title of the registry key
> into the CSV file, ie. "Checking Netmeeting - Disable Remote Desktop
> Sharing" It comes at the end of the lines containing the CSR# such as
> 5.6.1.1
>
> That part is tricky, at least to this noob.
I don't get why that's tricky
You managed to pull out the CSR with a pattern. Extending the pattern
to get the description too is trivial.
#!perl
use strict;
use warnings;
my %sect;
{
#Create a couple of lookup hashes
my $record;
open my $analyze, '<', 'test.dsc' or die $!;
while(<$analyze>) {
$record = { section => $1 } if /^#\s+(.*)\s+#/;
$sect{$1} = $record if /^dialog set,text1,\"([.0-9]+)/;
$record->{regkey}= $1 if /^REGISTRY WRITE,(.*)/;
$record->{$1} = $2 if /^%%(after|before) = \@REGREAD\((.*)\)/;
}
close $analyze;
}
#Scan the Audit file for failures
open my $auditfile, '<', 'server1.aud' or die $!;
open my $csvfile, '>', 'logfile.csv' or die $!;
print $csvfile "CSR #,Decriprion,Section Title,Registry
Key,Before,After\n";
while(<$auditfile>) {
if( my ($csr,$desc) = /^(.*?)~([^~]*).*~FAIL~/) {
if ( my $record = $sect{$csr} ) {
print $csvfile join (',',map {"\"$_\""} $csr, $desc,
@$record{'section','regkey','before','after'}),"\n";
} else {
print "Fail code $csr not found in analyze.dsc\n";
}
}
}
close $auditfile;
close $csvfile;
__END__
------------------------------
Date: Sat, 7 Oct 2006 17:20:08 +1000
From: "Sisyphus" <sisyphus1@nomail.afraid.org>
Subject: Re: If gethostbyname fails it never recovers
Message-Id: <45275636$0$32603$afc38c87@news.optusnet.com.au>
"Andrew DeFaria" <Andrew@DeFaria.com> wrote in message
.
.
> Well in practice it does return 0 when successful and so far 2 when not
> successful. Once unsuccessful it keeps returning 2.
No - I suspect that once $? is set to 2, it stays set to 2 until the next
unsuccessful call - which probably just sets it to 2 again.
Instead of:
my @ipaddrs = gethostbyname $host;
my $status = $?;
try:
$? = 0; # set $? to zero (in case it's already set to another value)
my @ipaddrs = gethostbyname $host;
my $status = $?;
Better still - do what Mark Clements suggested and check $? only when
gethostname() fails.
Cheers,
Rob
------------------------------
Date: 07 Oct 2006 07:53:35 -0500
From: yankeeinexile@gmail.com
Subject: Merits vs costs of Structured Programming Dogma
Message-Id: <87ejtk478w.fsf_-_@gmail.com>
"Peter J. Holzer" <hjp-usenet2@hjp.at> writes:
> On 2006-10-05 23:49, ToddAndMargo@gbis.com <ToddAndMargo@gbis.com> wrote:
> >
> > 1) Under Modula2, best practice (or my practice) was from top
> > to bottom
> > a) make my declarations (which variable did what, etc.),
> > b) write all my modules and functions, and
> > c) in the body, place my code.
> >
> > Is this also the proper structure under Perl as well?
>
> I don't think so. In fact I think this is a particularly bad structure
> and one of the reasons why I don't like Pascal-like languages is that
> they force this structure on the programmer.
>
> [...snippage...]
>
> There are hundreds of lines of code where $some_variable could be
> accessed and changed. When you look at the code you have no idea whether
> you are passing the same value to do_something_with() as you received
> from get_some_value(). $some_variable might have been changed by
> some_code() or any function called by it.
>
I agree wholeheartedly with Peter's analysis. I've recently had to do
a major bit of coding in C after many years of full time Perl work and
the "forced structure" grates against me - I never wrote Pascal code
because it grated on me even when it was novel and exciting :).
However, to play Devil's Advocate for a moment: One of the OTHER
dogmas of the Structured Programming Camp is: You shouldn't have any
block that is "hundreds of lines of code". If you violate rule (n) of
any set of best practices, then rule (n+1) may suddenly not make as
much sense as it used to.
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Lawrence Statton - lawrenabae@abaluon.abaom s/aba/c/g
Computer software consists of only two components: ones and
zeros, in roughly equal proportions. All that is required is to
sort them into the correct order.
------------------------------
Date: Sat, 7 Oct 2006 04:42:09 GMT
From: merlyn@stonehenge.com (Randal Schwartz)
Subject: new CPAN modules on Sat Oct 7 2006
Message-Id: <J6r129.D3L@zorch.sf-bay.org>
The following modules have recently been added to or updated in the
Comprehensive Perl Archive Network (CPAN). You can install them using the
instructions in the 'perlmodinstall' page included with your Perl
distribution.
Bundle-DateTime-Complete-0.15
http://search.cpan.org/~pijll/Bundle-DateTime-Complete-0.15/
Bundle with all DateTime related modules
----
Business-PayPal-API-0.40
http://search.cpan.org/~scottw/Business-PayPal-API-0.40/
PayPal API
----
CORBA-omniORB-0.8
http://search.cpan.org/~housel/CORBA-omniORB-0.8/
Perl module implementing CORBA 2.x via omniORB
----
Catalyst-Example-InstantCRUD-v0.0.17
http://search.cpan.org/~zby/Catalyst-Example-InstantCRUD-v0.0.17/
----
DBIx-Web-0.61a
http://search.cpan.org/~makarow/DBIx-Web-0.61a/
Active Web Database Layer
----
Devel-EvalContext-0.08
http://search.cpan.org/~bsmith/Devel-EvalContext-0.08/
Save lexicals and hints between calls to eval
----
EekBoek-1.00.03
http://search.cpan.org/~jv/EekBoek-1.00.03/
Bookkeeping software for small and medium-size businesses
----
Lingua-Alphabet-Phonetic-0.052
http://search.cpan.org/~mthurn/Lingua-Alphabet-Phonetic-0.052/
map ABC's to phonetic alphabets
----
Linux-Statistics-1.15
http://search.cpan.org/~bloonix/Linux-Statistics-1.15/
Collect linux system statistics.
----
MIME-Charset-0.01
http://search.cpan.org/~nezumi/MIME-Charset-0.01/
Charset Informations for MIME
----
MIME-Charset-0.02
http://search.cpan.org/~nezumi/MIME-Charset-0.02/
Charset Informations for MIME
----
Mail-QmailRemoteXS-1.2
http://search.cpan.org/~rsandberg/Mail-QmailRemoteXS-1.2/
Lightweight C-code (XS) SMTP send function based on Qmail's qmail-remote
----
Net-Appliance-Session-0.06
http://search.cpan.org/~oliver/Net-Appliance-Session-0.06/
Run command-line sessions to network appliances
----
Object-Destroyer-1.02
http://search.cpan.org/~adamk/Object-Destroyer-1.02/
Make objects with circular references DESTROY normally
----
Object-InsideOut-2.05
http://search.cpan.org/~jdhedden/Object-InsideOut-2.05/
Comprehensive inside-out object support module
----
Oracle-CAPI-0.01
http://search.cpan.org/~rsandberg/Oracle-CAPI-0.01/
Perl XS wrapper extension for the Oracle Collaboration Suite CAPI SDK
----
POE-Component-IRC-5.05
http://search.cpan.org/~bingos/POE-Component-IRC-5.05/
a fully event-driven IRC client module.
----
POE-Loop-Glib-0.0023
http://search.cpan.org/~martijn/POE-Loop-Glib-0.0023/
a bridge that supports Glib's event loop from POE
----
POE-Session-Irssi-0.2
http://search.cpan.org/~martijn/POE-Session-Irssi-0.2/
emit POE events for Irssi signals
----
POE-Session-Irssi-0.3
http://search.cpan.org/~martijn/POE-Session-Irssi-0.3/
emit POE events for Irssi signals
----
Parallel-Pvm-Scheduler-0.01
http://search.cpan.org/~golharam/Parallel-Pvm-Scheduler-0.01/
Perl extension for distributing jobs through PVM
----
Perl-Metrics-Simple-0.013
http://search.cpan.org/~matisse/Perl-Metrics-Simple-0.013/
Count packages, subs, lines, etc. of many files.
----
RPC-Object-0.04
http://search.cpan.org/~jwu/RPC-Object-0.04/
A lightweight implementation for remote procedure calls
----
Rose-DB-0.727
http://search.cpan.org/~jsiracusa/Rose-DB-0.727/
A DBI wrapper and abstraction layer.
----
Rose-DB-Object-0.754
http://search.cpan.org/~jsiracusa/Rose-DB-Object-0.754/
Extensible, high performance RDBMS-OO mapper.
----
Rose-DateTime-0.531
http://search.cpan.org/~jsiracusa/Rose-DateTime-0.531/
DateTime helper functions and objects.
----
Rose-HTML-Objects-0.54
http://search.cpan.org/~jsiracusa/Rose-HTML-Objects-0.54/
Object-oriented interfaces for HTML.
----
Rose-Object-0.82
http://search.cpan.org/~jsiracusa/Rose-Object-0.82/
A simple object base class.
----
SSN-Validate-0.15
http://search.cpan.org/~kmeltz/SSN-Validate-0.15/
Perl extension to do SSN Validation
----
SWISH-Prog-0.03
http://search.cpan.org/~karman/SWISH-Prog-0.03/
build Swish-e programs
----
Search-Tools-0.02
http://search.cpan.org/~karman/Search-Tools-0.02/
tools for building search applications
----
Template-TAL-0.9
http://search.cpan.org/~tomi/Template-TAL-0.9/
Process TAL templates with Perl
----
Test-Exception-0.23
http://search.cpan.org/~adie/Test-Exception-0.23/
Test exception based code
----
Test-Harness-2.64
http://search.cpan.org/~petdance/Test-Harness-2.64/
Run Perl standard test scripts with statistics
----
Test-Run-0.0100_11
http://search.cpan.org/~shlomif/Test-Run-0.0100_11/
----
Test-WWW-Selenium-1.04
http://search.cpan.org/~lukec/Test-WWW-Selenium-1.04/
Test applications using Selenium Remote Control
----
URI-ParseSearchString-0.6
http://search.cpan.org/~sden/URI-ParseSearchString-0.6/
parse Apache refferer logs and extract search engine query strings.
----
Win32-Env-0.02
http://search.cpan.org/~rowaa/Win32-Env-0.02/
set and retrieve global system and user environment variables under Win32.
----
Win32-EventLog-Carp-1.40
http://search.cpan.org/~dland/Win32-EventLog-Carp-1.40/
for carping in the Windows NT Event Log
----
threads-1.43
http://search.cpan.org/~jdhedden/threads-1.43/
Perl interpreter-based threads
If you're an author of one of these modules, please submit a detailed
announcement to comp.lang.perl.announce, and we'll pass it along.
This message was generated by a Perl program described in my Linux
Magazine column, which can be found on-line (along with more than
200 other freely available past column articles) at
http://www.stonehenge.com/merlyn/LinuxMag/col82.html
print "Just another Perl hacker," # the original
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
------------------------------
Date: Sat, 7 Oct 2006 04:19:59 +0100
From: Ben Morrow <benmorrow@tiscali.co.uk>
Subject: Re: newbie cspan example question
Message-Id: <vvijv3-jht.ln1@osiris.mauzo.dyndns.org>
Quoth ToddAndMargo@gbis.com:
>
> Mirco Wahab wrote:
> > Thus spoke ToddAndMargo@gbis.com (on 2006-10-06 02:02):
> > >
> > > Having run the example and being told that something important
> > > was missing (Popup::new) I though I had better ask the following
> > > questions:
> >
> > 'paw' seems to be an old SuSE-related package
> > for drawing text shapes and stuff on top of
> > the unix 'curses' library. You need to have
> > that installed.
> >
> > [use CPAN.pm to install it]
>
> The cpan script sounds a lot like YUM.
If you use an OS with a package manager (which I presume is what YUM is)
then it is probably worth looking to see if you can install Perl modules
as packages, rather than using CPAN.pm. If the module you want isn't
provided by your OS as a package, then at least make sure CPAN.pm
installs its packages somewhere different from your OS's package
manager: having two package management systems fighting over one set of
files is a recipe for Pain. Recent(ish) versions of Perl, if configured
as recommended, have three separate module trees (per installed version
of Perl): one for 'core modules', which come with Perl; one for 'vendor
modules', which are installed by the OS package manager; and one for
'site modules', installed with CPAN.pm or manually by the sysadmin.
> And, the "use" statement is on
> the same
> lines as Modula2's use of external modules. :-)
Yes. Pretty much every modern language provides this facility.
Ben
--
And if you wanna make sense / Whatcha looking at me for? (Fiona Apple)
* benmorrow@tiscali.co.uk *
------------------------------
Date: Sat, 7 Oct 2006 14:26:22 +0200
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: newbie structure question
Message-Id: <slrneif77e.b0r.hjp-usenet2@yoyo.hjp.at>
On 2006-10-05 23:49, ToddAndMargo@gbis.com <ToddAndMargo@gbis.com> wrote:
> A am real new to Perl. My background is Modula2 and Bash scripts.
> Please premit a newbie question:
>
> 1) Under Modula2, best practice (or my practice) was from top
> to bottom
> a) make my declarations (which variable did what, etc.),
> b) write all my modules and functions, and
> c) in the body, place my code.
>
> Is this also the proper structure under Perl as well?
I don't think so. In fact I think this is a particularly bad structure
and one of the reasons why I don't like Pascal-like languages is that
they force this structure on the programmer.
I believe (and I know many other perl-programmers agree) that variables
should always be declared in the smallest possible scope. I.e., don't do
something like this:
my $some_variable;
sub a_subroutine {
...
}
# many more subroutines - hundreds of lines of code
for (...) {
if (...) {
# $some_variable is only used in this block:
$some_variable = get_some_value();
some_code();
do_something_with($some_variable);
more_code();
}
}
There are hundreds of lines of code where $some_variable could be
accessed and changed. When you look at the code you have no idea whether
you are passing the same value to do_something_with() as you received
from get_some_value(). $some_variable might have been changed by
some_code() or any function called by it.
Instead, write it like this:
sub a_subroutine {
...
}
# many more subroutines - hundreds of lines of code
for (...) {
if (...) {
# $some_variable is only used in this block:
my $some_variable = get_some_value();
some_code();
do_something_with($some_variable);
more_code();
}
}
That way $some_variable is visible only visible inside of the if block.
Nothing outside can change it unless $some_variable is explicitely
passed to it (that is do_something_with() could change $some_variable).
Browsing through Conway's "Perl Best Practice", I was surprised that I
couldn't find that advice, although he does follow that style. Maybe he
thought that's so self-evident that he doesn't have to spell it out.
hp
--
_ | Peter J. Holzer | > Wieso sollte man etwas erfinden was nicht
|_|_) | Sysadmin WSR | > ist?
| | | hjp@hjp.at | Was sonst wäre der Sinn des Erfindens?
__/ | http://www.hjp.at/ | -- P. Einstein u. V. Gringmuth in desd
------------------------------
Date: Sat, 7 Oct 2006 12:39:44 +0200
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: Notify parent that a background process failed
Message-Id: <slrneif0vg.b0r.hjp-usenet2@yoyo.hjp.at>
On 2006-10-05 22:37, pat <patty_rv@hotmail.com> wrote:
> I have a Perl script that basically takes a list of Unix commands and
> executes them one by one, does something with the output and goes to
> execute the next, some of these commands have to be executed in the
> background, the script does not wait for them to finish, just starts it
> and keeps going, the problem is that I do need to know if any of the
> background processes fail and the return code. I tried using $SIG{CHLD}
> but I get a signal for every one of the commands I run, I'm only
> interested in the background ones. Is there a way to do a non-blocking
> wait on a specific process id?
perldoc -f waitpid
But if you are starting several background processes, you probably don't
want to wait for a specific process. Instead you want to record the pids
of the background processes, and when a process terminates you check if
it is one of those you are interested in, else you ignore it.
> I use fork to start the process:
>
> if (!defined($pid = fork))
> { # Fork failed
[...]
> }
> elsif ($pid==0)
> {
[...]
> }
> else
> {
> # Parent process continues here
[...]
> # Make sure process started
> unless (kill 0 => $pid)
> {
> print "Background process is not running \n";
> return 1;
> }
There is a race condition here. The child process may already have
terminated when the parent gets here. Also, it is useless: You already
know that the child process was started since fork didn't return undef.
hp
--
_ | Peter J. Holzer | > Wieso sollte man etwas erfinden was nicht
|_|_) | Sysadmin WSR | > ist?
| | | hjp@hjp.at | Was sonst wäre der Sinn des Erfindens?
__/ | http://www.hjp.at/ | -- P. Einstein u. V. Gringmuth in desd
------------------------------
Date: Sat, 7 Oct 2006 09:53:00 +0200
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: Regarding numeric literals
Message-Id: <slrneien6s.786.hjp-usenet2@yoyo.hjp.at>
On 2006-10-05 19:15, Paul Lalli <mritty@gmail.com> wrote:
> chait...@yahoo.com wrote:
>> I'm wondering why Perl treats a numeric literal with underscores in it
>> and a variable with a value made of numbers punctuated by underscores
>> so differently......what difference would (or SHOULD perhaps?) it make
>> for it to evaluate 1_23_456 + 1 to 123457 and $num + 1 (where $num
>> holds 1_23_456) to 2 ?
[...]
> But how would you make that determination in a non-literal value? If I
> put in the code:
> my $x = <STDIN>;
> and the user enters:
> 878_912_793_109
> how is your program supposed to tell whether the user meant the number
> 878 billion (etc), or the string "8 7 8 underscore (etc)"? More to the
> point, how is Perl supposed to tell?
If the user enters the line "878912793109", how is perl supposed to
tell whether the user meant a string string or a number?
It doesn't know. For now, it is just a string. Only when the scalar
is used in a numeric context it is gets a numerical value in addition to
the string value. Perl computes the the numerical value by parsing the
string value according to some rules.
These rules are not the same as numeric constants in perl code: For
example, the base is always ten:
the numerical literal 012 has the value ten (zero times sixty-four plus
one times eight plus two), but the string '012' has the numerical value
twelve (zero times hundred plus one times ten plus two).
Newer versions of perl (it works with 5.8.8, but not with 5.8.4)
recognize "inf" and "nan" in strings, but you can't use them as
numerical literals (you get the error 'Bareword "inf" not allowed ...').
As the OP noted, underscores are ignored in numerical literals, but not in
strings.
Why are these rules as they are?
The main objective is probably reversability: If a numerical value is
converted to a string and back, it should not change. So perl must be
able to parse all number formats it produces: decimal integers, decimal
fractions, scientific notation and the special values "inf" and "nan".
However, it never produces strings with leading zeros. So why does it
parse them as decimals and not as octal? I guess that leading zeros in
decimal numbers are relatively frequent in "real world" files, while
numbers in mixed base in C notation are not. So ignoring leading zeros
is less surprising.
As for underscores, I don't have a good explanation. Why doesn't perl
ignore underscores in strings just like in numerical literals? I don't
see where it would do any harm, but it would be handy in some cases.
hp
--
_ | Peter J. Holzer | > Wieso sollte man etwas erfinden was nicht
|_|_) | Sysadmin WSR | > ist?
| | | hjp@hjp.at | Was sonst wäre der Sinn des Erfindens?
__/ | http://www.hjp.at/ | -- P. Einstein u. V. Gringmuth in desd
------------------------------
Date: 6 Oct 2006 21:33:08 -0700
From: "Seb" <solive@gmail.com>
Subject: Regex help for URL
Message-Id: <1160195588.317620.196210@i3g2000cwc.googlegroups.com>
Hi,
I am trying to find the right regular expression which would only
validate a URL with a given number of folders.
Example:
http://www.abc.com/folder/page.htm --> Valid (4 slahes)
http://www.abc.com/folder/subfolder/ --> not valid (5 slashes)
Basically, any URL not made of 4 slahes would be invalid.
However, the URL:
http://www.abc.com/folder/subfolder --> would also be invalid
Any ideas?
Thanks,
Seb
------------------------------
Date: 7 Oct 2006 08:23:08 GMT
From: anno4000@radom.zrz.tu-berlin.de
Subject: Re: Regex help for URL
Message-Id: <4op6fcFfnrg7U1@news.dfncis.de>
Seb <solive@gmail.com> wrote in comp.lang.perl.misc:
> Hi,
>
> I am trying to find the right regular expression which would only
> validate a URL with a given number of folders.
>
> Example:
>
> http://www.abc.com/folder/page.htm --> Valid (4 slahes)
>
> http://www.abc.com/folder/subfolder/ --> not valid (5 slashes)
>
> Basically, any URL not made of 4 slahes would be invalid.
> However, the URL:
>
> http://www.abc.com/folder/subfolder --> would also be invalid
>
> Any ideas?
What have you tried?
We can help you fix your code, but we rarely deliver solutions
to specification.
Anno
------------------------------
Date: 7 Oct 2006 01:29:23 -0700
From: rcia@davidfilmer.com
Subject: Re: Regex help for URL
Message-Id: <1160209763.465231.101940@i3g2000cwc.googlegroups.com>
Seb wrote:
> http://www.abc.com/folder/page.htm --> Valid (4 slahes)
> http://www.abc.com/folder/subfolder/ --> not valid (5 slashes)
perldoc -q count
How can I count the number of occurrences of a substring within a
string?
> http://www.abc.com/folder/subfolder --> would also be invalid
That's arbitrary. The client has no way to tell if "subfolder" is a
directory (where a default DirectoryIndex document presumably exists)
or a document/file named "subfolder". Only the server knows for sure.
--
David Filmer (http://DavidFilmer.com)
------------------------------
Date: 7 Oct 2006 02:38:34 -0700
From: "Brian McCauley" <nobull67@gmail.com>
Subject: Re: Script that shows IP address of a server.
Message-Id: <1160213914.903736.160190@i3g2000cwc.googlegroups.com>
On Oct 6, 8:51 pm, yankeeinex...@gmail.com wrote:
> Blue <superb...@myjaring.net> writes:
>
> > I need a simple script that show the IP address of the server (where
> > the script is residing).Your question is broken.
>
> A server does not have "an" IP address.
> Most hosts have not less than two IP addresses, and many have more.
> My development machine has five interfaces each with an IP address.
You can make a TCP connection to somewhere then look at the address of
the local socket. This should give you a fair guess the IP address
"facing" your chosen target.
------------------------------
Date: Sat, 07 Oct 2006 20:34:39 +0800
From: Blue <superbaby@myjaring.net>
Subject: Re: Script that shows IP address of a server.
Message-Id: <45279edc$1_2@news.tm.net.my>
Sorry about the confusion. I found the solution with PHP. Thanks.
<html>
<body>
<font face=arial size=2>
<?php
$server = $_SERVER['SERVER_ADDR'];
print "<b>SERVER_ADDR:</b> $server";
print "<p>";
$http = $_SERVER['HTTP_HOST'];
print "<b>HTTP_HOST:</b> $http";
print "<p>";
$remote = $_SERVER['REMOTE_ADDR'];
print "<b>REMOTE_ADDR:</b> $remote";
?>
------------------------------
Date: 7 Oct 2006 01:11:55 -0700
From: "jmv" <eugene.morozov@gmail.com>
Subject: Re: Unit-testing and mock objects
Message-Id: <1160208715.255236.10310@m7g2000cwm.googlegroups.com>
Ben Morrow wrote:
> Quoth "jmv" <eugene.morozov@gmail.com>:
> [snip]
> A search on CPAN for 'mock' reveals Test::MockModule. Is this the sort
> of thing you mean?
Thank you, I didn't see this module. But I'm afraid it wouldn't work.
I've tried Test::MockObject and Sub::Override and they doesn't handle
this situation (overriding subs in module B which is used by module A
which is used by unit-test).
Eugene
------------------------------
Date: 7 Oct 2006 09:39:39 GMT
From: anno4000@radom.zrz.tu-berlin.de
Subject: Re: Unit-testing and mock objects
Message-Id: <4opaurFf9m8uU1@news.dfncis.de>
jmv <eugene.morozov@gmail.com> wrote in comp.lang.perl.misc:
> Hello,
> I have a problem creating mock objects for the unit tests. I'm writing
> a unit tests for the rather old proprietary OO application that has all
> sort of code smells.
>
> The code I want to test looks like this:
>
> =cut=
> use Corp::System; # exports runCommand()
> use Logcheck;
>
> sub new { ... }
>
> sub createUser {
> ...
> my $exitval = runCommand("useradd ...");
> ...
> Logcheck::createLogDir();
> ...
> }
> =cut=
>
>
> I want to override the runCommand method from Corp::System module.
I don't see how runCommand is a method, it isn't called as one. So
inheritance-based overriding is out.
> I've
> found only one way to do it. In my unit test module I write:
>
> =cut=
> BEGIN {
> use Corp::System;
> package Corp::System;
> sub runCommand { push @COMMANDS, shift; return 0; }
> 1;
The return value of a BEGIN block is ignored. It isn't the same
as module. "1;" isn't needed. I wonder if you even need the
BEGIN block.
> }
> =cut=
>
> But this method is very boring, unelegant and error-prone, because sub
> createUser, which I'm testing, uses subroutines from a lot of modules
> and there're numerous dependencies between them.
I'm not sure what exactly you dislike about your method. If there
are many subroutines to override, there will be that many actions
to take, and interdependencies will make life harder. There's no
way a clever overriding method will change that.
> Is there more elegant and flexible method for testing such classes
> without refactoring them? In any case, tests should exist before
> refactoring.
You can change the behavior of Corp::System::runCommand() any time
and from any place through
*Corp::System::runCommand = sub { ...};
It doesn't have to happen at compile time as long as it happens before
the first call.
Anno
------------------------------
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 9816
***************************************