[31973] in Perl-Users-Digest
Perl-Users Digest, Issue: 3237 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Dec 15 18:09:26 2010
Date: Wed, 15 Dec 2010 15: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 Wed, 15 Dec 2010 Volume: 11 Number: 3237
Today's topics:
Re: code review - JSON parsing and data structures <tadmc@seesig.invalid>
Re: code review - JSON parsing and data structures <tadmc@seesig.invalid>
Re: code review - JSON parsing and data structures sln@netherlands.com
Re: code review - JSON parsing and data structures <mh+usenetspam1002@zugschl.us>
Re: code review - JSON parsing and data structures <uri@StemSystems.com>
Re: code review - JSON parsing and data structures <tadmc@seesig.invalid>
Re: code review - JSON parsing and data structures <hjp-usenet2@hjp.at>
Re: code review - JSON parsing and data structures <hjp-usenet2@hjp.at>
Re: code review - JSON parsing and data structures <nobody@nowhere.net>
Re: code review - JSON parsing and data structures <derykus@gmail.com>
Re: code review - JSON parsing and data structures sln@netherlands.com
Re: Regex to match a numerical IP range <nospam-abuse@ilyaz.org>
Re: Regex to match a numerical IP range sln@netherlands.com
Re: Windows Question Dec. 12, 2010 <edgrsprj@ix.netcom.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Tue, 14 Dec 2010 13:22:45 -0600
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: code review - JSON parsing and data structures
Message-Id: <slrnigfhnt.gue.tadmc@tadbox.sbcglobal.net>
Marc Haber <mh+usenetspam1002@zugschl.us> wrote:
> Tad McClellan <tadmc@seesig.invalid> wrote:
>>Marc Haber <mh+usenetspam1002@zugschl.us> wrote:
>>> And I find it kind of
>>> irritating to quote hard-coded keys like $cfg->{"hosts"} but not when
>>> the key is interpolated.
>>
>>Then don't quote hard-coded keys either!
>
> Didn't that give some bareword diagnostic under use strict, at least
> in earlier perl versions?
I don't think it ever did that.
>>> Is there a possibility to write %{$cfg->{"hosts"}} any less clumsy?
>>
>>Only by removing the unnecessary quotes:
>>
>> %{$cfg->{hosts}}
>
> That's still as clumsy as a C typecast.
Agreed.
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.
------------------------------
Date: Tue, 14 Dec 2010 13:25:56 -0600
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: code review - JSON parsing and data structures
Message-Id: <slrnigfhtt.gue.tadmc@tadbox.sbcglobal.net>
Marc Haber <mh+usenetspam1002@zugschl.us> wrote:
> "Uri Guttman" <uri@StemSystems.com> wrote:
>>if you chomp in scalar mode it will return a scalar of the whole file
>>without newlines (really $/)!! who would want such a scalar??
>
> For example, a parser which treats a newline as regular whitespace.
That cannot be a good example.
"foo\nbar"
would become
"foobar"
where the "regular whitespace", that was likely significant to the parse,
got stripped...
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.
------------------------------
Date: Tue, 14 Dec 2010 12:08:06 -0800
From: sln@netherlands.com
Subject: Re: code review - JSON parsing and data structures
Message-Id: <74jfg61uabhn05mso2bdmbcis8bjq49n26@4ax.com>
On Tue, 14 Dec 2010 18:31:42 +0100, Marc Haber <mh+usenetspam1002@zugschl.us> wrote:
>"Peter J. Holzer" <hjp-usenet2@hjp.at> wrote:
>>The code was (trimmed to the essential lines):
>>
>>for my $file ( @ARGV ) {
>> $string.=scalar file("$file")->slurp;
>>}
>>
>>That doesn't look like reading a file line by line and then
>>concatenating the lines. It looks like slurping each file and
>>concatenating the files.
>
>Yes, that's what I originally intended. Originally, I wanted to have
>different files mixed together. For example, I would have wanted two
>input files:
>
>(1)
>{
> "host": {
> "linux001": {
> "hostcheck": "ping",
> "services": [ "ping", "ssh", "http", ],
> }
> }
>}
>
>(2)
>{
> "host": {
> "mgmt001" : {
> "hostcheck": "ping",
> "services": [ "ping", "rdp", ],
> }
> }
>}
>
>to end up in the same datastructure as if the input file were
>
>{
> "host": {
> "linux001": {
> "hostcheck": "ping",
> "services": [ "ping", "ssh", "http", ],
> }
> }
>
> "host": {
> "mgmt001" : {
> "hostcheck": "ping",
> "services": [ "ping", "rdp", ],
> }
> }
>}
>
>which is neither valid json nor what I originally meant. I thus
>re-redesigned to the current state.
>
This looks pretty Perlish data structure to me.
Start a result hash, read in a file as a string, replace the :'s with =>'s,
eval each to a temporary hash. Walk the temp hash tree, populate the result hash.
If not done yet, Dumper() out the result hash as a string replacing => with :
Send it to json? Simple enough.
-sln
------------------------------
Date: Tue, 14 Dec 2010 22:38:54 +0100
From: Marc Haber <mh+usenetspam1002@zugschl.us>
Subject: Re: code review - JSON parsing and data structures
Message-Id: <ie8o5e$ve$1@news1.tnib.de>
Tad McClellan <tadmc@seesig.invalid> wrote:
>Marc Haber <mh+usenetspam1002@zugschl.us> wrote:
>> "Uri Guttman" <uri@StemSystems.com> wrote:
>>>if you chomp in scalar mode it will return a scalar of the whole file
>>>without newlines (really $/)!! who would want such a scalar??
>>
>> For example, a parser which treats a newline as regular whitespace.
>
>That cannot be a good example.
>
> "foo\nbar"
>
>would become
>
> "foobar"
>
>where the "regular whitespace", that was likely significant to the =
parse,=20
>got stripped...
Isn't the newline replaced by a regular whitespace? Stripping it isn't
a good idea, I agree.
Greetings
Marc
--=20
-------------------------------------- !! No courtesy copies, please !! =
-----
Marc Haber | " Questions are the | Mailadresse im =
Header
Mannheim, Germany | Beginning of Wisdom " | =
http://www.zugschlus.de/
Nordisch by Nature | Lt. Worf, TNG "Rightful Heir" | Fon: *49 621 =
72739834
------------------------------
Date: Tue, 14 Dec 2010 17:18:04 -0500
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: code review - JSON parsing and data structures
Message-Id: <8739q0q9r7.fsf@quad.sysarch.com>
>>>>> "MH" == Marc Haber <mh+usenetspam1002@zugschl.us> writes:
MH> Tad McClellan <tadmc@seesig.invalid> wrote:
>> Marc Haber <mh+usenetspam1002@zugschl.us> wrote:
>>> "Uri Guttman" <uri@StemSystems.com> wrote:
>>>> if you chomp in scalar mode it will return a scalar of the whole file
>>>> without newlines (really $/)!! who would want such a scalar??
>>>
>>> For example, a parser which treats a newline as regular whitespace.
>>
>> That cannot be a good example.
>>
>> "foo\nbar"
>>
>> would become
>>
>> "foobar"
>>
>> where the "regular whitespace", that was likely significant to the parse,
>> got stripped...
MH> Isn't the newline replaced by a regular whitespace? Stripping it isn't
MH> a good idea, I agree.
chomp doesn't replace newlines with spaces. and in the code i pasted
from that module, a scalar slurp with chomp enabled is a bug waiting to
happen. it is a poor slurp option.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
------------------------------
Date: Tue, 14 Dec 2010 16:44:11 -0600
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: code review - JSON parsing and data structures
Message-Id: <slrnigfthj.h7r.tadmc@tadbox.sbcglobal.net>
Marc Haber <mh+usenetspam1002@zugschl.us> wrote:
> Tad McClellan <tadmc@seesig.invalid> wrote:
>>Marc Haber <mh+usenetspam1002@zugschl.us> wrote:
>>> "Uri Guttman" <uri@StemSystems.com> wrote:
>>>>if you chomp in scalar mode it will return a scalar of the whole file
>>>>without newlines (really $/)!! who would want such a scalar??
>>>
>>> For example, a parser which treats a newline as regular whitespace.
>>
>>That cannot be a good example.
>>
>> "foo\nbar"
>>
>>would become
>>
>> "foobar"
>>
>>where the "regular whitespace", that was likely significant to the parse,
>>got stripped...
>
> Isn't the newline replaced by a regular whitespace? Stripping it isn't
> a good idea, I agree.
perldoc -f chomp
...removes any trailing string that corresponds to the
current value of C<$/> ...
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.
------------------------------
Date: Wed, 15 Dec 2010 12:49:18 +0100
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: code review - JSON parsing and data structures
Message-Id: <slrnighapu.pa4.hjp-usenet2@hrunkner.hjp.at>
On 2010-12-14 17:29, Marc Haber <mh+usenetspam1002@zugschl.us> wrote:
> "Uri Guttman" <uri@StemSystems.com> wrote:
>>>>>>> "MH" == Marc Haber <mh+usenetspam1002@zugschl.us> writes:
>> MH> Frankly, I didn't know. I just changed the syntax until it compiled
>> MH> and gave the intended output.
>>
>>that is a bad habit you should break. you should read the useful
>>perldocs on refs and perl data structures
>>
>>perldsc, perllol, perlreftut
>
> I have read these, and the Camel book, and the Cookbook, and the FAQ
> numerous times in the last years, and the notation is still one of
> perl's secrets for me. The docs are useful, but if one doesn't grok
> them the first three times, reading them a fourth time is unlikely to
> help. I'll do so though after christmas.
Start with perlreftut. IMO it's well written and contains everything you
need to use references. I wish it had been around 14 years ago. I had to
make do with perlref and I found that horribly confusing.
hp
------------------------------
Date: Wed, 15 Dec 2010 13:06:24 +0100
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: code review - JSON parsing and data structures
Message-Id: <slrnighbq0.pa4.hjp-usenet2@hrunkner.hjp.at>
On 2010-12-14 19:03, Marc Haber <mh+usenetspam1002@zugschl.us> wrote:
> Anselmo Canfora <nobody@nowhere.net> wrote:
>>map {
>> my $node=$cfg->{hosts}->{$_};
>> print "host $_:\n";
>> print "$$node{$_}->{hostcheck}\n";
>> print "$service\n" for $service @{$node->{services}};
>> print "_________________________\n\n";
>>} keys %{$cfg->{hosts}};
>
> That's a very perlish solution which is probably harder to read for
> the occasional perl programmer,
It is also unidiomatic Perl as it abuses the map function to implement a
loop. Perl has a loop syntax, it can and should be used:
for (keys %{$cfg->{hosts}}) {
my $node=$cfg->{hosts}->{$_};
print "host $_:\n";
print "$$node{$_}->{hostcheck}\n";
print "$service\n" for $service @{$node->{services}};
print "_________________________\n\n";
}
Now we see that $_ and $node are the keys and values of
%{$cfg->{hosts}} respectively, so we can make that more apparent:
while (my ($host, $node) = each %{$cfg->{hosts}}) {
print "host $host:\n";
print "$$node{$host}->{hostcheck}\n";
print "$service\n" for $service @{$node->{services}};
print "_________________________\n\n";
}
And finally we can clean up the mess in third line:
while (my ($host, $node) = each %{$cfg->{hosts}}) {
print "host $host:\n";
print "$node->{$host}{hostcheck}\n";
print "$service\n" for $service @{$node->{services}};
print "_________________________\n\n";
}
(the third line still looks strange: Why do we need $host here again? We
already used it to get to $node.)
hp
------------------------------
Date: Wed, 15 Dec 2010 15:35:52 +0100
From: Anselmo Canfora <nobody@nowhere.net>
Subject: Re: code review - JSON parsing and data structures
Message-Id: <4d08d24a$0$6823$5fc30a8@news.tiscali.it>
I agree about the map abuse, I did it again :)
In the third line there is an error, it should be:
print "$node->{hostcheck}\n";
Il 15/12/2010 13.06, Peter J. Holzer ha scritto:
> On 2010-12-14 19:03, Marc Haber<mh+usenetspam1002@zugschl.us> wrote:
>> Anselmo Canfora<nobody@nowhere.net> wrote:
>>> map {
>>> my $node=$cfg->{hosts}->{$_};
>>> print "host $_:\n";
>>> print "$$node{$_}->{hostcheck}\n";
>>> print "$service\n" for $service @{$node->{services}};
>>> print "_________________________\n\n";
>>> } keys %{$cfg->{hosts}};
>>
>> That's a very perlish solution which is probably harder to read for
>> the occasional perl programmer,
>
> It is also unidiomatic Perl as it abuses the map function to implement a
> loop. Perl has a loop syntax, it can and should be used:
>
> for (keys %{$cfg->{hosts}}) {
> my $node=$cfg->{hosts}->{$_};
> print "host $_:\n";
> print "$$node{$_}->{hostcheck}\n";
> print "$service\n" for $service @{$node->{services}};
> print "_________________________\n\n";
> }
>
> Now we see that $_ and $node are the keys and values of
> %{$cfg->{hosts}} respectively, so we can make that more apparent:
>
> while (my ($host, $node) = each %{$cfg->{hosts}}) {
> print "host $host:\n";
> print "$$node{$host}->{hostcheck}\n";
> print "$service\n" for $service @{$node->{services}};
> print "_________________________\n\n";
> }
>
> And finally we can clean up the mess in third line:
>
> while (my ($host, $node) = each %{$cfg->{hosts}}) {
> print "host $host:\n";
> print "$node->{$host}{hostcheck}\n";
> print "$service\n" for $service @{$node->{services}};
> print "_________________________\n\n";
> }
>
> (the third line still looks strange: Why do we need $host here again? We
> already used it to get to $node.)
>
> hp
>
------------------------------
Date: Wed, 15 Dec 2010 13:13:09 -0800 (PST)
From: "C.DeRykus" <derykus@gmail.com>
Subject: Re: code review - JSON parsing and data structures
Message-Id: <da35e7ac-0fb1-4cdb-af02-23a07b5f683f@c13g2000prc.googlegroups.com>
On Dec 14, 11:03=A0am, Marc Haber <mh+usenetspam1...@zugschl.us> wrote:
> Tad McClellan <ta...@seesig.invalid> wrote:
> >Marc Haber <mh+usenetspam1...@zugschl.us> wrote:
> >> And I find it kind of
> >> irritating to quote hard-coded keys like $cfg->{"hosts"} but not when
> >> the key is interpolated.
>
> >Then don't quote hard-coded keys either!
>
> Didn't that give some bareword diagnostic under use strict, at least
> in earlier perl versions? It's fine now, so it's ok.
>
You might be thinking of a cautionary note in perltrap:
* Avoid barewords if you can, especially all lowercase ones.
You can't tell by just looking at it whether a bareword is
a function or a string. By using quotes on strings and
parentheses on function calls, you won't ever get them
confused.
And a bareword hash key with non \w fails 'use strict';
without 'strict', perl can go to really strange places:
perl -Mstrict -wE "say $h->{Baden-Baden}"
Bareword "Baden" not allowed while "strict subs" in use ...
perl -wE "say $h->{Baden&Baden}"
Operator or semicolon missing before &Baden
Ambiguous use of & resolved as operator &
perl -wE "say $h->{Baden-Baden}"
Argument "Baden" isn't numeric in subtraction (-)
--
Charles DeRykus
------------------------------
Date: Wed, 15 Dec 2010 14:11:27 -0800
From: sln@netherlands.com
Subject: Re: code review - JSON parsing and data structures
Message-Id: <40fig6p1ivpdsmot8i68a9quhdt96b8mf7@4ax.com>
On Tue, 14 Dec 2010 18:39:51 +0100, Marc Haber <mh+usenetspam1002@zugschl.us> wrote:
>"John W. Krahn" <jwkrahn@example.com> wrote:
>>Marc Haber wrote:
>>> code:
>>> #!/usr/bin/perl -w
>>>
>>> use strict;
>>> use JSON::XS;
>>> use Path::Class;
>>>
>>> my %hosts;
>>> for my $file ( @ARGV ) {
>>> print "parsing $file\n";
>>> my $string.=scalar file("$file")->slurp;
>>> my $cfg = JSON::XS->new->relaxed->decode( $string );
>>
>>Of course this is Perl so you could write that as:
>>
>>#!/usr/bin/perl -w
>>
>>use strict;
>>use JSON::XS;
>>use Path::Class;
>>
>>my %hosts;
>>local $/;
>>while ( my $string = <> ) {
>> print "parsing $ARGV\n";
>> my $cfg = JSON::XS->new->relaxed->decode( $string );
>
>That's not functionally identical since all input is first catted
>together and then parsed as a single JSON input, which is different
>from parsing each input file as its own JSON.
>
How is this
for my $file ( @ARGV ) {
print "parsing $file\n";
my $string.=scalar file("$file")->slurp;
my $cfg = JSON::XS->new->relaxed->decode( $string );
not functionally identical to this
local $/;
while ( my $string = <> ) {
print "parsing $ARGV\n";
my $cfg = JSON::XS->new->relaxed->decode( $string );
?
-sln
------------------------------
Date: Tue, 14 Dec 2010 19:53:11 +0000 (UTC)
From: Ilya Zakharevich <nospam-abuse@ilyaz.org>
Subject: Re: Regex to match a numerical IP range
Message-Id: <slrnigfip7.kj8.nospam-abuse@powdermilk.math.berkeley.edu>
On 2010-12-14, Ted Zlatanov <tzz@lifelogs.com> wrote:
> On Mon, 13 Dec 2010 12:25:51 -0800 sln@netherlands.com wrote:
>
> s> On Mon, 13 Dec 2010 10:51:11 -0600, Ted Zlatanov <tzz@lifelogs.com> wrote:
>>> I think Net::Netmask is much better for this task than any custom
>>> solution. Have you tried it?
>
> s> Well, I thought it was just a case of knowing the simple ip
> s> address without knowing anything about the CIDR network (block).
> s> So given a simple quad part notation and range, a simple comparison
> s> would be is all thats needed instead of a full blown cisco type thing.
>
> I wouldn't try to write that code myself because the risk of getting it
> wrong is too high. It's surprisingly hard to do IP ranges well,
> especially if you need fast operations. But it looks so easy, doesn't it...
I added (??{}) for this, but it looks broken now:
perl -wle "123 =~ /^(\d+$)(??{ $1 > 122 ? qr( )x : qr((?!)) })/ or die"
panic: top_env
If I understand the docs correct, this should also work:
perl -wle "123 =~ /^(\d+$)(?(?{$1 > 122})|(?!))/ or die"
It looks like it works fine in 5.8.8 and 5.10.0.
Ilya
------------------------------
Date: Tue, 14 Dec 2010 16:10:26 -0800
From: sln@netherlands.com
Subject: Re: Regex to match a numerical IP range
Message-Id: <bc1gg6lvo7sdni7k77vbh9lqt8fh429cf2@4ax.com>
On Tue, 14 Dec 2010 19:53:11 +0000 (UTC), Ilya Zakharevich <nospam-abuse@ilyaz.org> wrote:
>On 2010-12-14, Ted Zlatanov <tzz@lifelogs.com> wrote:
>> On Mon, 13 Dec 2010 12:25:51 -0800 sln@netherlands.com wrote:
>>
>> s> On Mon, 13 Dec 2010 10:51:11 -0600, Ted Zlatanov <tzz@lifelogs.com> wrote:
>>>> I think Net::Netmask is much better for this task than any custom
>>>> solution. Have you tried it?
>>
>> s> Well, I thought it was just a case of knowing the simple ip
>> s> address without knowing anything about the CIDR network (block).
>> s> So given a simple quad part notation and range, a simple comparison
>> s> would be is all thats needed instead of a full blown cisco type thing.
>>
>> I wouldn't try to write that code myself because the risk of getting it
>> wrong is too high. It's surprisingly hard to do IP ranges well,
>> especially if you need fast operations. But it looks so easy, doesn't it...
>
>I added (??{}) for this, but it looks broken now:
>
> perl -wle "123 =~ /^(\d+$)(??{ $1 > 122 ? qr( )x : qr((?!)) })/ or die"
> panic: top_env
>
>If I understand the docs correct, this should also work:
>
> perl -wle "123 =~ /^(\d+$)(?(?{$1 > 122})|(?!))/ or die"
>
>It looks like it works fine in 5.8.8 and 5.10.0.
>
On your code, > 122 will pass.
Both flavors (??{}) and (?(?{})))? return in-place regex.
About the docs on (?()|),
(?{ CODE }) always succeeds, does it return 1? I don't know..
But (?(condition)yes-pattern) where condition = (?{ CODE }), treats
the code block as the condition. Then apparently, (?(?{ CODE })|) is a special
condition of the conditional expression extension.
So, its seems (?(?{ is itself, a special case.
perl -wle "123 =~ /^(\d+$)(?(?{$1 > 122}) |)/ or die"
dies if it is > 122, otherwise it passes.
I guess thats because if its > 122, the expression
becomes 123 =~ /^(\d+$) / otherwise its /^(\d+$)/
-sln
------------------------------
Date: Wed, 15 Dec 2010 13:14:01 -0600
From: "E.D.G." <edgrsprj@ix.netcom.com>
Subject: Re: Windows Question Dec. 12, 2010
Message-Id: <HoqdnbvzL5n-jpTQnZ2dnUVZ_sOdnZ2d@earthlink.com>
"Jürgen Exner" <jurgenex@hotmail.com> wrote in message
news:h1kag6hfkcves57iftgccmccn21bg6kqit@4ax.com...
> A far more reliable way is to check the process table. Applicable
> modules are readily available on CPAN.
Thanks for the comments. I did take a look at some of those modules.
What I have been doing so far when my Perl program needs to know if some
other program is running is have the other program store something in a file
indicating that it is running. The Perl program then checks that file to
see what other program's status is.
It works. But it is is not the ideal way to get this done.
------------------------------
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:
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests.
#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 3237
***************************************