[31978] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 3242 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Dec 18 14:09:32 2010

Date: Sat, 18 Dec 2010 11:09:14 -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           Sat, 18 Dec 2010     Volume: 11 Number: 3242

Today's topics:
    Re: code review - JSON parsing and data structures <rvtol+usenet@xs4all.nl>
    Re: code review - JSON parsing and data structures <derykus@gmail.com>
    Re: Entities.pm - How does decode_entities work? <dave@invalid.invalid>
    Re: Entities.pm - How does decode_entities work? <dave@invalid.invalid>
    Re: Entities.pm - How does decode_entities work? <uri@StemSystems.com>
    Re: Entities.pm - How does decode_entities work? <sherm.pendley@gmail.com>
    Re: Entities.pm - How does decode_entities work? <xhoster@gmail.com>
        Pulling pairs of values from an array <RedGrittyBrick@spamweary.invalid>
    Re: Pulling pairs of values from an array <RedGrittyBrick@spamweary.invalid>
    Re: Pulling pairs of values from an array <tadmc@seesig.invalid>
    Re: Pulling pairs of values from an array <tadmc@seesig.invalid>
    Re: Pulling pairs of values from an array <RedGrittyBrick@spamweary.invalid>
    Re: Pulling pairs of values from an array sln@netherlands.com
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Fri, 17 Dec 2010 15:22:38 +0100
From: "Dr.Ruud" <rvtol+usenet@xs4all.nl>
Subject: Re: code review - JSON parsing and data structures
Message-Id: <4d0b722e$0$81476$e4fe514c@news.xs4all.nl>

On 2010-12-17 11:46, Wolf Behrenhoff wrote:

> say $h->{-baden}
> is valid syntax and the same as
> say $h->{"-baden"}

Not true. It is the same as $h->{ -'baden' }.

perl -MO=Deparse -e'$h->{-baden}'
$$h{-'baden'};
-e syntax OK

perl -MO=Deparse -e'$h->{- -baden}'
$$h{-(-'baden')};
-e syntax OK

perl -MData::Dumper -e'$h->{- -baden} = 1; print Dumper($h)'
$VAR1 = {
           '+baden' => 1
         };


-- 
Ruud


------------------------------

Date: Fri, 17 Dec 2010 11:53:39 -0800 (PST)
From: "C.DeRykus" <derykus@gmail.com>
Subject: Re: code review - JSON parsing and data structures
Message-Id: <cc8102a9-7f3e-41fc-b43f-806c5a74dd9f@o23g2000prh.googlegroups.com>

On Dec 17, 2:46=C2=A0am, Wolf Behrenhoff
<NoSpamPleaseButThisIsVal...@gmx.net> wrote:
> On 15.12.2010 22:13, C.DeRykus wrote:
>
> > And a bareword hash key with non \w fails 'use strict';
> > without 'strict', perl can go to really strange places:
>
> > =C2=A0 =C2=A0 perl =C2=A0-Mstrict -wE "say $h->{Baden-Baden}"
> > =C2=A0 =C2=A0 Bareword "Baden" not allowed while "strict subs" in use .=
 ..
>
> Hm...
> I also get the error that
> Global symbol "$h" requires explicit package name at -e line 1.
>
> But even if you "my $h", there is at least one exception to barewords
> with non \w:
> say $h->{-baden}
> is valid syntax and the same as
> say $h->{"-baden"}
>
> That makes it possible to use named parameters like
> aSub(-param1 =3D> "foo", -param2 =3D> "bar");
>

Yes, then there's negation. Useful, as you mention,
for passing named parameters to a hash but it still
seems quirky to me that it doesn't draw a warning.

Neither of these draw a warning either but, arguably,
you'd think since the results might well surprise the
unwary.

perl -MData::Dumper -Mstrict  -wE "my %h=3D (+Baden=3D>'-Baden');
print Dumper \%h"
$VAR1 =3D {
          'Baden' =3D> '-Baden' };


 ... "my %h=3D (!Baden=3D>'-Baden'); print Dumper \%h"
$VAR1 =3D { '' =3D> '-Baden' };


 ...  "my %h=3D (~Baden=3D>'-Baden'); print Dumper \%h"
$VAR1 =3D { '=E2=95=9C=E2=82=A7=C2=A2=C3=9C=C3=A6' =3D> '-Baden' };

--
Charles DeRykus


------------------------------

Date: Fri, 17 Dec 2010 11:25:23 +0000 (UTC)
From: "Dave Saville" <dave@invalid.invalid>
Subject: Re: Entities.pm - How does decode_entities work?
Message-Id: <fV45K0OBJxbE-pn2-fFus2x1YbrjX@localhost>

On Thu, 16 Dec 2010 23:40:51 UTC, "Uri Guttman" <uri@StemSystems.com> 
wrote:

<snip>
> it is in XS as the comment says. so it is somewhere else in the build
> for HTML::Parser. you need to explore deeper. and it will be in c for
> speed.

Having had a poke around the c code, I still don't understand *how* 
the routine is found. 

HTML::Entities exports encode_entities and decode_entities, which it 
does not have, plus a few other things. 
It requires HTML:: Parser (which requires HTML::Entities) which does 
have decode_entities buried in the .XS but not in the .pm. My 
understanding of "use" is that it looks for <whatever>.pm in @INC 
directories or if of the form FOO::bar it looks for FOO/bar.pm in 
@INC. Further, "use lib some-directory;" prepends that directory to 
@INC. so

use lib dir1;
use lib dir2;
 
results in searching dir2, dir1 and then @INC.

So HTML::Entities is exporting a routine it does not have and 
HTML::Parser is supplying it but does not appear to export it - and it
works.

What would be nice would be:

print which <some exported thingy> and it tells you *where* it comes 
from.
-- 
Regards
Dave Saville


------------------------------

Date: Fri, 17 Dec 2010 12:02:18 +0000 (UTC)
From: "Dave Saville" <dave@invalid.invalid>
Subject: Re: Entities.pm - How does decode_entities work?
Message-Id: <fV45K0OBJxbE-pn2-cgVK0NByqYHM@localhost>

On Thu, 16 Dec 2010 23:40:51 UTC, "Uri Guttman" <uri@StemSystems.com> 
wrote:

<snip>

> it is in XS as the comment says. so it is somewhere else in the build
> for HTML::Parser. you need to explore deeper. and it will be in c for
> speed.

Just read up on XS which I had not met before - I *think* I now 
understand. A module defined with XS puts it's "exported" stuff direct
to the interpreter using the perl API - which I guess is searched 
*before* @INC. 
-- 
Regards
Dave Saville


------------------------------

Date: Fri, 17 Dec 2010 10:00:39 -0500
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: Entities.pm - How does decode_entities work?
Message-Id: <87tyicculk.fsf@quad.sysarch.com>

>>>>> "DS" == Dave Saville <dave@invalid.invalid> writes:

  DS> On Thu, 16 Dec 2010 23:40:01 UTC, "Uri Guttman" <uri@StemSystems.com> 
  DS> wrote:

  DS> Please read what I wrote. :-)

  DS> Calling script has use "Entities;"

  DS> Entities.pm has "package Entities;"

  DS> HTML:: is not mentioned anywhere else.

from /usr/lib/perl5/HTML/Entities.pm:

package HTML::Entities;

and since it doesn't even export that sub anymore, you need to check the
XS code and see what package it uses. of course it will be
HTML::Entities since that is the proper name for the module. again, you
need to use what IT wants and not what you think it wants to import the
sub.

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: Fri, 17 Dec 2010 10:30:07 -0500
From: Sherm Pendley <sherm.pendley@gmail.com>
Subject: Re: Entities.pm - How does decode_entities work?
Message-Id: <m27hf8quww.fsf@sherm.shermpendley.com>

"Dave Saville" <dave@invalid.invalid> writes:

> Please read what I wrote. :-)

He did. Please try to understand his answer.

> Calling script has use "Entities;"
>
> Entities.pm has "package Entities;"
>
> HTML:: is not mentioned anywhere else.

Yes it is. Parse() is an XS function - so check the package declaration
in parser.xs.

sherm--

-- 
Sherm Pendley
                                   <http://camelbones.sourceforge.net>
Cocoa Developer


------------------------------

Date: Thu, 16 Dec 2010 20:32:25 -0800
From: Xho Jingleheimerschmidt <xhoster@gmail.com>
Subject: Re: Entities.pm - How does decode_entities work?
Message-Id: <4d0c033a$0$23344$ed362ca5@nr5-q3a.newsreader.com>

Dave Saville wrote:
> On Thu, 16 Dec 2010 17:45:55 UTC, "Uri Guttman" <uri@StemSystems.com> 
> wrote:
> 
>>>>>>> "DS" == Dave Saville <dave@invalid.invalid> writes:
>>   DS> I then see that the sub line in Entities.pm is sub 
>>   DS> decode_entities_old. OK so it's not amazing it could not find it. But 
>>   DS> the question is how on earth does it work when the use HTML::Entities 
>>   DS> is in effect? Which it does. I ran a search down the entire perl tree 
>>   DS> looking for any file with a "sub decode_entities" in it and 
>>   DS> Entities.pm is the only file and then it is decode_entities_old. So 
>>   DS> how *does* it work?
>>
>>   DS> Is there some way to find out where perl is getting a particular 
>>   DS> routine from - rather like the *nix command line "which"?
>>
>> if you read the source and look for decode_entities there is a comment
>> which says where it is located.
> 
> Yes I see - a require for HTML::Parser - But Parser does not have a 
> decode_entities so I repeat *how* does the routine reference get 
> resolved. I do not understand. 

The module is not pure Perl.  It links in a C library.  That is what 
XSLoader does.

Xho


------------------------------

Date: Fri, 17 Dec 2010 11:59:38 +0000
From: RedGrittyBrick <RedGrittyBrick@spamweary.invalid>
Subject: Pulling pairs of values from an array
Message-Id: <4d0b50a3$0$2520$db0fefd9@news.zen.co.uk>

Consider

     #!/usr/bin/perl
     use strict;
     use warnings;

     while(<DATA>) {
       my($t1,$t2,$value);
       ($t1,$t2)=qw(A P); $value = $1 if /^$t1.*$t2=(.)/;
       ($t1,$t2)=qw(B Q); $value = $1 if /^$t1.*$t2=(.)/;
       ($t1,$t2)=qw(C R); $value = $1 if /^$t1.*$t2=(.)/;
       print "$value\n";
     }

     __DATA__
     A P=1 Q=2 R=3
     B P=8 Q=2 R=7
     C Q=2 P=1 R=3

I'd like to replace the repetition with an elegant loop over pairs of 
$t1,$t2 values stored in an array (or other structure) like one of

     my @pairs = qw (A,P   B,Q   C,R);
     my @pairs = qw (A P   B Q   C R);

I've not had much success with a brief attempt at combining `while`, 
`split` and `unshift`.

What concise, elegant solution am I missing?


(Also posted to stackoverflow.com, let me know if you object. I'll post 
a combined summary of answers to both fora as penance)

-- 
RGB


------------------------------

Date: Fri, 17 Dec 2010 12:48:27 +0000
From: RedGrittyBrick <RedGrittyBrick@spamweary.invalid>
Subject: Re: Pulling pairs of values from an array
Message-Id: <4d0b5c12$0$12169$fa0fcedb@news.zen.co.uk>

On 17/12/2010 11:59, RedGrittyBrick wrote:
> Consider
>
> #!/usr/bin/perl
> use strict;
> use warnings;
>
> while(<DATA>) {
> my($t1,$t2,$value);
> ($t1,$t2)=qw(A P); $value = $1 if /^$t1.*$t2=(.)/;
> ($t1,$t2)=qw(B Q); $value = $1 if /^$t1.*$t2=(.)/;
> ($t1,$t2)=qw(C R); $value = $1 if /^$t1.*$t2=(.)/;
> print "$value\n";
> }
>
> __DATA__
> A P=1 Q=2 R=3
> B P=8 Q=2 R=7
> C Q=2 P=1 R=3
>
> I'd like to replace the repetition with an elegant loop over pairs of
> $t1,$t2 values stored in an array (or other structure) like one of
>
> my @pairs = qw (A,P B,Q C,R);
> my @pairs = qw (A P B Q C R);
>
> I've not had much success with a brief attempt at combining `while`,
> `split` and `unshift`.
>
> What concise, elegant solution am I missing?
>
>
> (Also posted to stackoverflow.com, let me know if you object. I'll post
> a combined summary of answers to both fora as penance)
>

In 
<http://stackoverflow.com/questions/4470189/perl-pulling-pairs-of-values-from-an-array/4470453#4470453>
Sorpigal suggested

    #!/usr/bin/perl
    use strict;
    use warnings;

    my %pairs = qw/A P   B Q   C R/;

    foreach my $data (<DATA>) {
        while(my($t1, $t2) = each(%pairs)){
            $data =~ /^$t1.*$t2=(.)/ && print "$1\n";
        }
    }

Which I like. (YMMV)

-- 
RGB


------------------------------

Date: Fri, 17 Dec 2010 07:59:27 -0600
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: Pulling pairs of values from an array
Message-Id: <slrnigmrtg.s33.tadmc@tadbox.sbcglobal.net>

RedGrittyBrick <RedGrittyBrick@spamweary.invalid> wrote:

>     foreach my $data (<DATA>) {

> Which I like. (YMMV)


You should change your mileage so that you don't load the whole
file into memory...

    while (my $data = <DATA>) {


-- 
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: Fri, 17 Dec 2010 08:01:22 -0600
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: Pulling pairs of values from an array
Message-Id: <slrnigms14.s33.tadmc@tadbox.sbcglobal.net>

RedGrittyBrick <RedGrittyBrick@spamweary.invalid> wrote:
> Consider
>
>      #!/usr/bin/perl
>      use strict;
>      use warnings;
>
>      while(<DATA>) {
>        my($t1,$t2,$value);
>        ($t1,$t2)=qw(A P); $value = $1 if /^$t1.*$t2=(.)/;
>        ($t1,$t2)=qw(B Q); $value = $1 if /^$t1.*$t2=(.)/;
>        ($t1,$t2)=qw(C R); $value = $1 if /^$t1.*$t2=(.)/;
>        print "$value\n";
>      }
>
>      __DATA__
>      A P=1 Q=2 R=3
>      B P=8 Q=2 R=7
>      C Q=2 P=1 R=3


That data looks like an HoH to me...


> I'd like to replace the repetition with an elegant loop over pairs of 
> $t1,$t2 values stored in an array (or other structure) like one of
>
>      my @pairs = qw (A,P   B,Q   C,R);
>      my @pairs = qw (A P   B Q   C R);


    my %pairs = qw/A P   B Q   C R/;

    while (<DATA>) {
        my($type, %values) = split /[\s=]/;
        print "$values{$pairs{$type}}\n";
    }


-- 
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: Fri, 17 Dec 2010 15:29:51 +0000
From: RedGrittyBrick <RedGrittyBrick@spamweary.invalid>
Subject: Re: Pulling pairs of values from an array
Message-Id: <4d0b81e5$0$12167$fa0fcedb@news.zen.co.uk>

On 17/12/2010 14:01, Tad McClellan wrote:
> RedGrittyBrick<RedGrittyBrick@spamweary.invalid>  wrote:
>> Consider
>>
>>       #!/usr/bin/perl
>>       use strict;
>>       use warnings;
>>
>>       while(<DATA>) {
>>         my($t1,$t2,$value);
>>         ($t1,$t2)=qw(A P); $value = $1 if /^$t1.*$t2=(.)/;
>>         ($t1,$t2)=qw(B Q); $value = $1 if /^$t1.*$t2=(.)/;
>>         ($t1,$t2)=qw(C R); $value = $1 if /^$t1.*$t2=(.)/;
>>         print "$value\n";
>>       }
>>
>>       __DATA__
>>       A P=1 Q=2 R=3
>>       B P=8 Q=2 R=7
>>       C Q=2 P=1 R=3
>
>
> That data looks like an HoH to me...
>
>
>> I'd like to replace the repetition with an elegant loop over pairs of
>> $t1,$t2 values stored in an array (or other structure) like one of
>>
>>       my @pairs = qw (A,P   B,Q   C,R);
>>       my @pairs = qw (A P   B Q   C R);
>
>
>      my %pairs = qw/A P   B Q   C R/;
>
>      while (<DATA>) {
>          my($type, %values) = split /[\s=]/;
>          print "$values{$pairs{$type}}\n";
>      }
>
>

Thanks. The inventiveness of clpm contributors is a great source of 
inspiration.

A Sinan Ünür posted four solutions with some commentary in 
<http://stackoverflow.com/questions/4470189/perl-pulling-pairs-of-values-from-an-array/4470720#4470720>. 
I'm not sure I can summarise it adequately so I'll just post the URL 
here rather than paste it all in verbatim.

-- 
RGB


------------------------------

Date: Fri, 17 Dec 2010 09:29:31 -0800
From: sln@netherlands.com
Subject: Re: Pulling pairs of values from an array
Message-Id: <ru6ng69rivcnmivnusmk1butclj1elo441@4ax.com>

On Fri, 17 Dec 2010 11:59:38 +0000, RedGrittyBrick <RedGrittyBrick@spamweary.invalid> wrote:

>Consider
>
>     #!/usr/bin/perl
>     use strict;
>     use warnings;
>
>     while(<DATA>) {
>       my($t1,$t2,$value);
>       ($t1,$t2)=qw(A P); $value = $1 if /^$t1.*$t2=(.)/;
>       ($t1,$t2)=qw(B Q); $value = $1 if /^$t1.*$t2=(.)/;
>       ($t1,$t2)=qw(C R); $value = $1 if /^$t1.*$t2=(.)/;
>       print "$value\n";
>     }
>
>     __DATA__
>     A P=1 Q=2 R=3
>     B P=8 Q=2 R=7
>     C Q=2 P=1 R=3
>
>I'd like to replace the repetition with an elegant loop over pairs of 
>$t1,$t2 values stored in an array (or other structure) like one of
>
>     my @pairs = qw (A,P   B,Q   C,R);
>     my @pairs = qw (A P   B Q   C R);
>
>I've not had much success with a brief attempt at combining `while`, 
>`split` and `unshift`.
>
>What concise, elegant solution am I missing?
>
>
>(Also posted to stackoverflow.com, let me know if you object. I'll post 
>a combined summary of answers to both fora as penance)

You don't have to check all the pairs.
You can just get the value with a single if statement
per line of data. Its must faster this way.

Either of these will work (and there is validation so warnings
are not emitted) :

/^(\S+)/g && exists $pairs{$1} && /\G.*$pairs{$1}=(\S+)/
/^(\S+)(??{exists $pairs{$1} ? ".*$pairs{$1}=" : "(*FAIL)"})(\S+)/

-sln

-----------------------------
use strict;
use warnings;

my %pairs = qw/A P   B Q   C R/;

while ( <DATA> )
{
  if (/^(\S+)/g && exists $pairs{$1} && /\G.*$pairs{$1}=(\S+)/) {
      print $1,"\n";
  }
}
__DATA__

A P=1 Q=2 R=3
B P=8 Q=2 R=7
C Q=2 P=1 R=3


Optional, uses extended regex (??{..}):
while ( <DATA> )
{
  if (/^(\S+)(??{exists $pairs{$1} ? ".*$pairs{$1}=" : "(*FAIL)"})(\S+)/) {
      print $2,"\n";
  }
}



------------------------------

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 3242
***************************************


home help back first fref pref prev next nref lref last post