[30337] in Perl-Users-Digest
Perl-Users Digest, Issue: 1580 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon May 26 18:09:45 2008
Date: Mon, 26 May 2008 15:09:07 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Mon, 26 May 2008 Volume: 11 Number: 1580
Today's topics:
Re: blogs, longbets.org, and education of sociology <java.oke@gmail.com>
Consolidating a list of networks in Perl (J.D. Baldwin)
Re: Consolidating a list of networks in Perl <bjoern@hoehrmann.de>
Re: FAQ 5.12 How can I open a filehandle to a string? <bill@ts1000.us>
Re: FAQ 5.12 How can I open a filehandle to a string? <uri@stemsystems.com>
Re: FAQ 5.12 How can I open a filehandle to a string? <brian.d.foy@gmail.com>
IPC::Shareable. Does it work? <reply_in_group@mouse-potato.com>
Perldoc recommendation <bill@ts1000.us>
Re: Perldoc recommendation <uri@stemsystems.com>
Re: Perldoc recommendation <dragnet\_@_/internalysis.com>
Re: Perldoc recommendation <bill@ts1000.us>
Re: Perldoc recommendation <uri@stemsystems.com>
Re: reinstall perl <yingun@gmail.com>
Re: Why reading the FAQs is good (example) <tadmc@seesig.invalid>
Re: Why reading the FAQs is good (example) <tadmc@seesig.invalid>
Re: Why reading the FAQs is good (example) <1usa@llenroc.ude.invalid>
Re: Why reading the FAQs is good (example) <rvtol+news@isolution.nl>
Re: Why reading the FAQs is good (example) <dragnet\_@_/internalysis.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Mon, 26 May 2008 13:52:37 -0700 (PDT)
From: "j.oke" <java.oke@gmail.com>
Subject: Re: blogs, longbets.org, and education of sociology
Message-Id: <358ec7c3-4a25-4591-a705-03446e074b20@25g2000hsx.googlegroups.com>
On 26 Mag, 01:25, "xah...@gmail.com" <xah...@gmail.com> wrote:
> For about the past 10 years, ...
Q: How many comp.lang.X trolls does it take to change a light bulb?
A: One, and Superman. One to hold the bulb, and Superman to screw the
planet.
-JO
------------------------------
Date: Mon, 26 May 2008 21:22:07 +0000 (UTC)
From: INVALID_SEE_SIG@example.com.invalid (J.D. Baldwin)
Subject: Consolidating a list of networks in Perl
Message-Id: <g1f9lv$gd6$1@reader2.panix.com>
I have a couple of ideas about this, I think, but maybe someone has
solved the problem cleverly already.
Given a very big list of networks (they happen to be in first / last
IP format, but I can convert easily enough to subnet/mask form, even
if it's an approximation) ... I want to make a smaller list of
networks that subsumes all the nets in the big list. e.g.,
10.28.45.0 - 10.28.46.255
10.28.42.0 - 10.28.42.255
10.28.47.0 - 10.28.49.255
10.28.43.0 - 10.28.44.255
... would be "consolidated" down into 10.28.42.0 - 10.28.49.255.
Any ideas? Modules? Thoughts?
--
_+_ From the catapult of |If anyone disagrees with any statement I make, I
_|70|___:)=}- J.D. Baldwin |am quite prepared not only to retract it, but also
\ / baldwin@panix.com|to deny under oath that I ever made it. -T. Lehrer
***~~~~-----------------------------------------------------------------------
------------------------------
Date: Mon, 26 May 2008 23:39:02 +0200
From: Bjoern Hoehrmann <bjoern@hoehrmann.de>
Subject: Re: Consolidating a list of networks in Perl
Message-Id: <p3bm345e75bnukpgkf2cmr1o74u88be4ca@hive.bjoern.hoehrmann.de>
* J.D. Baldwin wrote in comp.lang.perl.misc:
>I have a couple of ideas about this, I think, but maybe someone has
>solved the problem cleverly already.
>
>Given a very big list of networks (they happen to be in first / last
>IP format, but I can convert easily enough to subnet/mask form, even
>if it's an approximation) ... I want to make a smaller list of
>networks that subsumes all the nets in the big list. e.g.,
>
> 10.28.45.0 - 10.28.46.255
> 10.28.42.0 - 10.28.42.255
> 10.28.47.0 - 10.28.49.255
> 10.28.43.0 - 10.28.44.255
>
>... would be "consolidated" down into 10.28.42.0 - 10.28.49.255.
You can turn the addresses into integers and treat the whole range as
integer span, a module like Set::IntSpan will then automatically give
you the shortest list of ranges that form the set. You can then turn
the integers back into the form above.
--
Björn Höhrmann · mailto:bjoern@hoehrmann.de · http://bjoern.hoehrmann.de
Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/
------------------------------
Date: Mon, 26 May 2008 12:19:21 -0700 (PDT)
From: Bill H <bill@ts1000.us>
Subject: Re: FAQ 5.12 How can I open a filehandle to a string?
Message-Id: <6669364b-16cb-4252-b0c7-8ab49bf242af@26g2000hsk.googlegroups.com>
[snip]
> 5.12: How can I open a filehandle to a string?
>
> , , ,
> =A0 =A0 (contributed by Peter J. Holzer, hjp-usen...@hjp.at)
>
> =A0 =A0 Since Perl 5.8.0, you can pass a reference to a scalar instead of =
the
> =A0 =A0 filename to create a file handle which you can used to read from o=
r
> =A0 =A0 write to a string:
>
> =A0 =A0 =A0 =A0 =A0 =A0 open(my $fh, '>', \$string) or die "Could not open=
string for writing";
> =A0 =A0 =A0 =A0 =A0 =A0 print $fh "foo\n";
> =A0 =A0 =A0 =A0 =A0 =A0 print $fh "bar\n"; =A0 =A0 =A0# $string now contai=
ns "foo\nbar\n"
>
> =A0 =A0 =A0 =A0 =A0 =A0 open(my $fh, '<', \$string) or die "Could not open=
string for reading";
> =A0 =A0 =A0 =A0 =A0 =A0 my $x =3D <$fh>; =A0# $x now contains "foo\n"
>
> =A0 =A0 With older versions of Perl, the "IO::String" module provides simi=
lar
> =A0 =A0 functionality.
>
[snip]
This is interesting, but what real world examples would there be to
doing this instead of just using string functions? The only one I can
think of is to redirect file output to a string for debugging
purposes, or maybe for duplication purposes, direct to a string, write
to a file, write to another etc.
Bill H
------------------------------
Date: Mon, 26 May 2008 19:40:13 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: FAQ 5.12 How can I open a filehandle to a string?
Message-Id: <x7wslgkg6b.fsf@mail.sysarch.com>
>>>>> "BH" == Bill H <bill@ts1000.us> writes:
BH> [snip]
>> 5.12: How can I open a filehandle to a string?
>>
>> , , ,
>> (contributed by Peter J. Holzer, hjp-usen...@hjp.at)
>>
>> Since Perl 5.8.0, you can pass a reference to a scalar instead of the
>> filename to create a file handle which you can used to read from or
>> write to a string:
>>
>> open(my $fh, '>', \$string) or die "Could not open string for writing";
>> print $fh "foo\n";
>> print $fh "bar\n"; # $string now contains "foo\nbar\n"
>>
>> open(my $fh, '<', \$string) or die "Could not open string for reading";
>> my $x = <$fh>; # $x now contains "foo\n"
>>
>> With older versions of Perl, the "IO::String" module provides similar
>> functionality.
>>
BH> [snip]
BH> This is interesting, but what real world examples would there be to
BH> doing this instead of just using string functions? The only one I can
BH> think of is to redirect file output to a string for debugging
BH> purposes, or maybe for duplication purposes, direct to a string, write
BH> to a file, write to another etc.
there are many uses. sometimes you have data and some parsing modules
expect a file handle. or some module expects to print to a file and you
want to put the data elsewhere (or use it in two places). even the list
you provided are all real world uses. sure you don't do this everyday
but it will come up once in a while and it is good that it is easy to
use. the previous way was with a tied handle module which is much
slower.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
------------------------------
Date: Mon, 26 May 2008 22:46:49 +0200
From: brian d foy <brian.d.foy@gmail.com>
Subject: Re: FAQ 5.12 How can I open a filehandle to a string?
Message-Id: <260520082246498062%brian.d.foy@gmail.com>
In article
<6669364b-16cb-4252-b0c7-8ab49bf242af@26g2000hsk.googlegroups.com>,
Bill H <bill@ts1000.us> wrote:
> [snip]
> > 5.12: How can I open a filehandle to a string?
> This is interesting, but what real world examples would there be to
> doing this instead of just using string functions?
I usually need this where some module insists on having a filehandle to
send its data to, but I want it in a string without all the extra work
and left-over files. Now I can print to a string directly.
------------------------------
Date: Mon, 26 May 2008 14:25:58 -0700
From: Tim Smith <reply_in_group@mouse-potato.com>
Subject: IPC::Shareable. Does it work?
Message-Id: <reply_in_group-CB23F3.14255826052008@news.supernews.com>
When I download the IPC::Shareable tarball and expand it, then "perl
Makefile.PL", "make", and "make test", the results are...odd.
On my Ubuntu 8.04 system (Athlon 64, but I have the 32-bit version of
Ubuntu installed), it passes all tests. I have done "make test" about
40 times, and every time, it passes all the tests.
On my Ubuntu 6.06 system (P4), it hangs on test t/38ipchv.
Same on one of my SUSE 10.1 systems and one of my SuSE 10.3 systems
(both using P4).
On my iMac (Intel Core Duo), it usually passes, but sometimes hangs on
test t/35clean, and sometimes fails test t/38ipchv, complaining of an
interrupted system call at line 32 (and then other complaints probably
stemming from that first failure).
On my PowerBook (PPC G4), it almost always has the failure in t/38ipchv,
and usually also fails t/35clean, with an error from
IPC::Sareable::SharedMem saying "shmget: No such file or directory". In
50 tries, I only saw one where it didn't have one or both of the above
failures (that one had t/30lock fail...I've never seen the PowerBook
pass).
Finally, my PowerMac (PPC G5) gets results similar to the PowerBook.
I don't seem to be alone in running into problems with this:
<http://testers.cpan.org/show/IPC-Shareable.html>
The nature of some of the errors I'm getting, and the fact that the
propensity to error seems to depend somewhat on the speed of the system,
leads me to suspect that the problem may actually be in the tests
themselves. They may have timing dependencies and race conditions.
Thus, I'm thinking of just going ahead and installing IPC::Shareable,
despite the test results. Or would that be really stupid, on the theory
that if the people writing the tests found it so hard to use
IPC::Shareable correctly, I'm probably not going to do any better, and
so the tests are in fact indicative of problems I'm likely to encounter
in my application if I foolishly go ahead and install and use
IPC::Shareable?
--
--Tim Smith
------------------------------
Date: Mon, 26 May 2008 12:26:07 -0700 (PDT)
From: Bill H <bill@ts1000.us>
Subject: Perldoc recommendation
Message-Id: <5d97bc0c-7770-43f6-a894-be795abcf338@y38g2000hsy.googlegroups.com>
Many times I have seen posted on here to consult perldoc for
information, and there is a wealth of information in there.
But if I could offer a suggestion, if the perldocs are online list
that url also.
Personally I do not surf the internet or read newsgroups on my
development computers, I use a different machine in my living room for
that (the one I am on) and I do not have perl installed on it. So to
look at some suggested information in perldoc I have to either email
myself a copy of the text, or print it, and then look it up on the
development systems when I am in the office. If there was / is a
perldocs online this would be very educational when I see those post
about looking up certain information in the docs I could just jump to
that site and read more.
Just my 2 cents :)
Bill H
------------------------------
Date: Mon, 26 May 2008 19:42:16 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Perldoc recommendation
Message-Id: <x7skw4kg2v.fsf@mail.sysarch.com>
>>>>> "BH" == Bill H <bill@ts1000.us> writes:
BH> Many times I have seen posted on here to consult perldoc for
BH> information, and there is a wealth of information in there.
BH> But if I could offer a suggestion, if the perldocs are online list
BH> that url also.
that is more pain for the responder and it is useful for the questioner
to do. perldoc.perl.org has all the docs in an easy to navigate and
search format. no need to post that each time if you know how to use
perldoc or that site.
BH> Personally I do not surf the internet or read newsgroups on my
BH> development computers, I use a different machine in my living room
BH> for that (the one I am on) and I do not have perl installed on
BH> it. So to look at some suggested information in perldoc I have to
BH> either email myself a copy of the text, or print it, and then look
BH> it up on the development systems when I am in the office. If there
BH> was / is a perldocs online this would be very educational when I
BH> see those post about looking up certain information in the docs I
BH> could just jump to that site and read more.
you should install perl on any machine where you might need perldocs. no
reason not to do it as it is free and easy to install.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
------------------------------
Date: Mon, 26 May 2008 15:07:03 -0500
From: Marc Bissonnette <dragnet\_@_/internalysis.com>
Subject: Re: Perldoc recommendation
Message-Id: <Xns9AAAA3F2A6A0Ddragnetinternalysisc@216.196.97.131>
Uri Guttman <uri@stemsystems.com> fell face-first on the keyboard. This was
the result: news:x7skw4kg2v.fsf@mail.sysarch.com:
>>>>>> "BH" == Bill H <bill@ts1000.us> writes:
>
> BH> Many times I have seen posted on here to consult perldoc for
> BH> information, and there is a wealth of information in there.
>
> BH> But if I could offer a suggestion, if the perldocs are online list
> BH> that url also.
>
> that is more pain for the responder and it is useful for the questioner
> to do. perldoc.perl.org has all the docs in an easy to navigate and
> search format. no need to post that each time if you know how to use
> perldoc or that site.
Ahh! Bookmarked!
Sadly, my machine here is Windows Vista (Yes, I know - boneheaded) with my
work being done on remote machines. While perl is installed locally,
Microsoft decided brilliantly (!!!) to make the DOS box only open in a
narrow window - For someone who's eyesight isn't what it used to be, this
doesn't make for great long content reading :(
The website, OTOH, allows me to use Opera's "Magnify - 200%" :) :) :)
Thanks!
--
Marc Bissonnette
Looking for a new ISP? http://www.canadianisp.com
Largest ISP comparison site across Canada.
------------------------------
Date: Mon, 26 May 2008 13:17:22 -0700 (PDT)
From: Bill H <bill@ts1000.us>
Subject: Re: Perldoc recommendation
Message-Id: <421dfe48-c6fd-483b-aa80-be42fac995ec@c58g2000hsc.googlegroups.com>
[snip]
> that is more pain for the responder and it is useful for the questioner
> to do. perldoc.perl.org has all the docs in an easy to navigate and
> search format. no need to post that each time if you know how to use
> perldoc or that site.
>
[snip]
Thanks Uri - I have that site bookmarked now on this system.
Bill H
------------------------------
Date: Mon, 26 May 2008 22:05:23 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Perldoc recommendation
Message-Id: <x7ve10iuvw.fsf@mail.sysarch.com>
>>>>> "MB" == Marc Bissonnette <dragnet\_@_/internalysis.com> writes:
MB> Uri Guttman <uri@stemsystems.com> fell face-first on the keyboard. This was
MB> the result: news:x7skw4kg2v.fsf@mail.sysarch.com:
>> that is more pain for the responder and it is useful for the questioner
>> to do. perldoc.perl.org has all the docs in an easy to navigate and
>> search format. no need to post that each time if you know how to use
>> perldoc or that site.
MB> Ahh! Bookmarked!
MB> Sadly, my machine here is Windows Vista (Yes, I know - boneheaded)
MB> with my work being done on remote machines. While perl is
MB> installed locally, Microsoft decided brilliantly (!!!) to make the
MB> DOS box only open in a narrow window - For someone who's eyesight
MB> isn't what it used to be, this doesn't make for great long content
MB> reading :(
MB> The website, OTOH, allows me to use Opera's "Magnify - 200%" :) :) :)
from what i know perl on winblows installs its docs in html format so
you can use a browser on that. relying on the web for docs is not as
good as having them on your box.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
------------------------------
Date: Mon, 26 May 2008 08:18:54 -0700 (PDT)
From: Keenlearner <yingun@gmail.com>
Subject: Re: reinstall perl
Message-Id: <2e72b326-f5ad-4c97-abc3-d79d7b51dbee@u12g2000prd.googlegroups.com>
As I said, once I got the answer from one post, I would post the
solution to the other post. That would solve most of it.
>it can increase the possible
> results (since they are completely separate.)
It happened here that it has higher probability of getting various
and
better result. Also I learned more. I would not want to waste my time
too, if posting at multi places would give the same result.
http://www.nntp.perl.org/group/perl.beginners/2008/05/msg100775.html
http://groups.google.com/group/comp.lang.perl.misc/browse_thread/thread/5e2d923994e833d9#
> There may be an argument for posting first in one place, waiting a week
> and posting in another place if no answers have been obtained. In this
> case it would be polite to mention that the question has already been
> posted in the other place.
I would try hard not to multi-post, but if I need so I would do the
above.
Rather than keep arguing this, does anyone still remember the main
problem ?
After I ran,
./Configure -Dusethreads -Dprefix=p/perl/ -Doptimize='-g'
Then
make
Making DynaLoader (static)
make[1]: Entering directory `/home/william/p/perl-5.8.8/ext/
DynaLoader'
Makefile out-of-date with respect to ../../lib/Config.pm ../../
config.h
Cleaning current config before rebuilding Makefile...
make -f Makefile.old clean > /dev/null 2>&1
../../miniperl "-I../../lib" "-I../../lib" Makefile.PL
"INSTALLDIRS=perl" "PERL_CORE=1" "LIBPERL_A=libperl.a"
Processing hints file hints/linux.pl
Writing Makefile for DynaLoader
==> Your Makefile has been rebuilt. <==
==> Please rerun the make command. <==
false
make[1]: *** [Makefile] Error 1
make[1]: Leaving directory `/home/william/p/perl-5.8.8/ext/
DynaLoader'
Extracting xsubpp (with variable substitutions)
make[1]: Leaving directory `/home/william/p/perl-5.8.8/utils'
Making x2p stuff
make[1]: Entering directory `/home/william/p/perl-5.8.8/x2p'
You haven't done a "make depend" yet!
make[1]: *** [hash.o] Error 1
make[1]: Leaving directory `/home/william/p/perl-5.8.8/x2p'
make: *** [translators] Error 2
Above are the two portions of errors that I am having. Thanks
------------------------------
Date: Mon, 26 May 2008 10:30:23 -0500
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: Why reading the FAQs is good (example)
Message-Id: <slrng3llsf.38a.tadmc@tadmc30.sbcglobal.net>
Marc Bissonnette <dragnet> wrote:
> While I know there are all lower-case modules out there, I
> don't think I've ever installed one (odd point, but...)
Modules named in all-lowercase are pragmas (compiler hints) by convention.
(see "Pragmatic Modules" in perldoc perlmodlib.)
So they (mostly?) are installed along with the perl distribution.
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
------------------------------
Date: Mon, 26 May 2008 11:30:20 -0500
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: Why reading the FAQs is good (example)
Message-Id: <slrng3lpcs.38a.tadmc@tadmc30.sbcglobal.net>
Marc Bissonnette <dragnet> wrote:
> So I was commenting some code I wrote for a friend who's new to perl and
> I came across the following in my code:
>
> # replace decimals with 999999999
When you choose to do this, it is responsible to figure out what
is to be done when the data already contains the "flag" value.
And also what's to be done when your manipulations create a
"flag" string when there was not one in the original.
> in order to check for non-numerical
> # data, then switch it back (this is a lazyman's shortcut)
[snip the-bad-kind-of-lazy code]
> And I realized I've been too lazy with this for too long. So my first
> thought was to post here with "how do I test for non-numerical in decimal
> number data?" but, of course, that violates the "How to ask intelligent
> questions in CPLM", so I googled it.
>
> Came up with a bunch of non-relevant results
Your Question is Asked Frequently but perhaps you missed the
right search term to uncover it:
perldoc -q number
How do I determine whether a scalar is a number/whole/integer/float?
> until I saw a similar
> question and the answer was "Your answer is in perldoc perldata"
>
> So I did exactly that. Of course, the answer *is* right there:
>
> warn "not a decimal number" unless /^-?\d+\.?\d*$/;
Note that this is an application of the common Perl idiom for
validating data.
ie:
anchor the beginning
anchor the end
write a pattern in between that accounts for everything you want to allow
> Which got me thinking - I take waaaay too many long-trips when there is
> stuff in the regex language that would make my life easier, so I realized
> that I need to understand the above, rather than just copy/paste into
> code.
>
> So, here's my understanding of
>
> /^-?\d+\.?\d*$/;
>
> / start the search pattern
> ^ match the beginning of the line
> -? match a minus sign once or not at all
> \d+ match a digit character zero or more times
> \.? match a decimal once or not at all
> \d match a digit character
> *$ all to the end of the line
The [*+?] quantifiers all apply to "the previous thing", so
it is a syntax error if there is no "previous thing".
The asterisk "goes with" the digit character class, not with the anchor.
> / end the search pattern
/
^ # beginning of the string (not line)
-? # optional minus sign
\d+ # run of digit characters
[.]? # optional dot character (backslashes annoy me)
\d* # optional run of digit characters
$ # end of the string (not line)
/x # note eXtended regular expression modifier
Look Ma!
Real Perl syntax!
> if ($in{hours} !~ /^-?\d+\.?\d*$/) {
I'd prefer to write that as:
unless ($in{hours} =~ /^-?\d+\.?\d*$/) {
as that puts the "not" out where it is harder to miss...
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
------------------------------
Date: Mon, 26 May 2008 16:40:13 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Why reading the FAQs is good (example)
Message-Id: <Xns9AAA80E298E59asu1cornelledu@127.0.0.1>
Marc Bissonnette <dragnet\_@_/internalysis.com> wrote in
news:Xns9AAA65B92A048dragnetinternalysisc@216.196.97.131:
> "A. Sinan Unur" <1usa@llenroc.ude.invalid> fell face-first on the
> keyboard. This was the result:
> news:Xns9AAA6479B6C95asu1cornelledu@127.0.0.1:
<snip> for brevity.
> You're right - I think I caught all of those in my latest interation,
> but as Uri points out, it passes a blank string, which isn't a good
> thing, so Regexp::Common looks like the smartest thing for the best
> coverage.
Always recommended.
Sinan
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)
comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
------------------------------
Date: Mon, 26 May 2008 20:00:03 +0200
From: "Dr.Ruud" <rvtol+news@isolution.nl>
Subject: Re: Why reading the FAQs is good (example)
Message-Id: <g1f56f.148.1@news.isolution.nl>
Tad J McClellan schreef:
> /^-?\d+\.?\d*$/
I would write the
"\.?\d*"
part as
"(?:\.[0-9]*)?",
or as
"(?:\.[0-9]+)?"
to force at least one trailing digits if there was a decimal point.
If numerics like "-0" and "-0." and "-.0" are allowed, but not "-." or
".", then I would do
/^-?(?:[0-9]+(?:\.[0-9]*)?|[0-9]*\.[0-9]+)$/
--
Affijn, Ruud
"Gewoon is een tijger."
------------------------------
Date: Mon, 26 May 2008 13:45:21 -0500
From: Marc Bissonnette <dragnet\_@_/internalysis.com>
Subject: Re: Why reading the FAQs is good (example)
Message-Id: <Xns9AAA961859EA1dragnetinternalysisc@216.196.97.131>
Tad J McClellan <tadmc@seesig.invalid> fell face-first on the keyboard.
This was the result: news:slrng3lpcs.38a.tadmc@tadmc30.sbcglobal.net:
> Marc Bissonnette <dragnet> wrote:
>> So I was commenting some code I wrote for a friend who's new to perl
>> and I came across the following in my code:
>>
>> # replace decimals with 999999999
>
> When you choose to do this, it is responsible to figure out what
> is to be done when the data already contains the "flag" value.
>
> And also what's to be done when your manipulations create a
> "flag" string when there was not one in the original.
Yeah, Ben pointed that out, too - Glaring error in my "shortcut" that
would have ended up completely invalidating/changing the original data
>> in order to check for non-numerical
>> # data, then switch it back (this is a lazyman's shortcut)
>
> [snip the-bad-kind-of-lazy code]
>
>> And I realized I've been too lazy with this for too long. So my first
>> thought was to post here with "how do I test for non-numerical in
>> decimal number data?" but, of course, that violates the "How to ask
>> intelligent questions in CPLM", so I googled it.
>>
>> Came up with a bunch of non-relevant results
>
> Your Question is Asked Frequently but perhaps you missed the
> right search term to uncover it:
>
> perldoc -q number
>
> How do I determine whether a scalar is a
> number/whole/integer/float?
Indeed I did - I can't for the life of me remember how to bring up a list
of the perldoc subject titles - I'll google it after I get the kids from
school.
>> until I saw a similar
>> question and the answer was "Your answer is in perldoc perldata"
>>
>> So I did exactly that. Of course, the answer *is* right there:
>>
>> warn "not a decimal number" unless /^-?\d+\.?\d*$/;
>
>
> Note that this is an application of the common Perl idiom for
> validating data.
>
> ie:
> anchor the beginning
> anchor the end
> write a pattern in between that accounts for everything you want
> to allow
>
>
>> Which got me thinking - I take waaaay too many long-trips when there
>> is stuff in the regex language that would make my life easier, so I
>> realized that I need to understand the above, rather than just
>> copy/paste into code.
>>
>> So, here's my understanding of
>>
>> /^-?\d+\.?\d*$/;
>>
>> / start the search pattern
>> ^ match the beginning of the line
>> -? match a minus sign once or not at all
>> \d+ match a digit character zero or more times
>> \.? match a decimal once or not at all
>> \d match a digit character
>> *$ all to the end of the line
>
>
> The [*+?] quantifiers all apply to "the previous thing", so
> it is a syntax error if there is no "previous thing".
>
> The asterisk "goes with" the digit character class, not with the
> anchor.
>
>
>> / end the search pattern
>
>
> /
> ^ # beginning of the string (not line)
> -? # optional minus sign
> \d+ # run of digit characters
> [.]? # optional dot character (backslashes annoy me)
> \d* # optional run of digit characters
> $ # end of the string (not line)
> /x # note eXtended regular expression modifier
>
>
> Look Ma!
>
> Real Perl syntax!
Many thanks - I've put this in my favourites.pl which I refer to for
learning and re-use stuff
>> if ($in{hours} !~ /^-?\d+\.?\d*$/) {
>
> I'd prefer to write that as:
>
> unless ($in{hours} =~ /^-?\d+\.?\d*$/) {
>
> as that puts the "not" out where it is harder to miss...
Maybe a silly question, but would that mean that if there are only two
conditions (true|false), use unless and if there are more than two, use
if/elsif as better code ?
--
Marc Bissonnette
Looking for a new ISP? http://www.canadianisp.com
Largest ISP comparison site across Canada.
------------------------------
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 1580
***************************************