[13743] in Perl-Users-Digest
Perl-Users Digest, Issue: 1153 Volume: 9
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Oct 22 15:10:24 1999
Date: Fri, 22 Oct 1999 12:10:15 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <940619415-v9-i1153@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Fri, 22 Oct 1999 Volume: 9 Number: 1153
Today's topics:
Re: Reference challenge <aqumsieh@matrox.com>
Re: Reference challenge <*@qz.to>
Re: Reference challenge <*@qz.to>
Re: Reference challenge (Sean McAfee)
Re: Reference challenge (Sean McAfee)
Re: REFERENCE CHALLENGE: final (?) realref() test <jeffp@crusoe.net>
Re: REFERENCE CHALLENGE: final (?) realref() test (Sean McAfee)
Re: REFERENCE CHALLENGE: final (?) realref() test (Neko)
Re: SGML/HTML parsing tool kent@darwin.eeb.uconn.edu
Re: sorting hashes in an array of hashes <lr@hpl.hp.com>
Re: use 'use' and 'no' like pragmas?? <rootbeer@redcat.com>
Re: XML::XSL (Steve Driscoll)
Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Fri, 22 Oct 1999 11:43:27 -0400
From: Ala Qumsieh <aqumsieh@matrox.com>
Subject: Re: Reference challenge
Message-Id: <x3yiu3z8kps.fsf@tigre.matrox.com>
mcafee@waits.facilities.med.umich.edu (Sean McAfee) writes:
>
> In article <Pine.GSO.4.10.9910212309090.15158-100000@crusoe.crusoe.net>,
> Jeff Pinyan <japhy@pobox.com> wrote:
> >On Oct 22, Sean McAfee blah blah blah:
> >> Write a 100% Perl, 100% portable, 100% bulletproof subroutine that will
> >> determine whether its single argument is a reference, and if so, what type
> >> of reference it is.
>
> >How is my regular expression,
> > ($type) = $obj =~ /([A-Z]+)\(0x[a-f0-9]+\)\z/;
> >not valid as the subroutine?
> > sub trueref { (shift =~ /([A-Z]+)\(0x[a-f0-9]+\)\z/)[0] }
>
> Because it fails if I pass it, say, the string "ARRAY(0xfffff)".
Ok. How about a combination of two posts I just saw:
sub isref {
ref $_[0]
and
$_[0] =~ /([A-Z]+)\(0x[a-f0-9]+\)\z/
and
$1
}
Does this make me a winner?
--Ala
------------------------------
Date: 22 Oct 1999 17:58:46 GMT
From: Eli the Bearded <*@qz.to>
Subject: Re: Reference challenge
Message-Id: <eli$9910212026@qz.little-neck.ny.us>
In comp.lang.perl.misc,
Sean McAfee <mcafee@waits.facilities.med.umich.edu> wrote:
> The challenge:
> Write a 100% Perl, 100% portable, 100% bulletproof subroutine that will
> determine whether its single argument is a reference, and if so, what type
> of reference it is.
Maybe I'm missing something here, but what's wrong with the
builtin function ref()?
$ perldoc -f ref
=item ref EXPR
=item ref
Returns a TRUE value if EXPR is a reference, FALSE otherwise. If EXPR
is not specified, C<$_> will be used. The value returned depends on the
type of thing the reference is a reference to.
Builtin types include:
...
> This challenge comes from my investigations into the issues raised by the
> thread entitled "Underlying data structure behind blessed reference". I
> don't believe a solution to this challenge exists, though I'm not
> completely certain.
I haven't been following this thread, after all.
> Sean McAfee mcafee@umich.edu
> print eval eval eval eval eval eval eval eval eval eval eval eval eval eval
> q!q@q#q$q%q^q&q*q-q=q+q|q~q:q? Just Another Perl Hacker ?:~|+=-*&^%$#@!
This looks better, to my eye:
print eval eval eval eval eval eval eval eval eval eval eval eval eval eval
q!q@q#q$q%q^q&q*q-q=q+q|q~q:qq< Just Another Perl Hacker \cj>:~|+=-*&^%$#@!
I really like the lines to be the same length.
Elijah
------
perl -we 'print reverse scalar reverse eval q Echr eval q W(eval q Qord q lLlQ
)-(eval q Qord q bBbQ)W E,reverse q JrekcahJ.q A lrepA.q P rehtonaP.q H tsujH'
------------------------------
Date: 22 Oct 1999 18:06:40 GMT
From: Eli the Bearded <*@qz.to>
Subject: Re: Reference challenge
Message-Id: <eli$9910221403@qz.little-neck.ny.us>
In comp.lang.perl.misc, Jeff Pinyan <japhy@pobox.com> wrote:
> On Oct 22, Rick Delaney blah blah blah:
>
> > my $array = bless [1, 2] => 'HASH';
> > print isref($array), "\n";
>
> Your code, Rick, only did 5 of the reference types...
>
> SCALAR, ARRAY, HASH, CODE, IO, REF, GLOB
>
> I believe those are all seven.
$ perl5.00503 -lwe '$a = qr/RE/i; print ref $a'
Regexp
Elijah
------
sent a documentation patch for this, but it was rejected as inaccurate
------------------------------
Date: Fri, 22 Oct 1999 18:18:46 GMT
From: mcafee@waits.facilities.med.umich.edu (Sean McAfee)
Subject: Re: Reference challenge
Message-Id: <aU1Q3.431$4G.83878@news.itd.umich.edu>
In article <x3yiu3z8kps.fsf@tigre.matrox.com>,
Ala Qumsieh <aqumsieh@matrox.com> wrote:
>> >On Oct 22, Sean McAfee blah blah blah:
>> >> Write a 100% Perl, 100% portable, 100% bulletproof subroutine that will
>> >> determine whether its single argument is a reference, and if so, what type
>> >> of reference it is.
>Ok. How about a combination of two posts I just saw:
> sub isref {
> ref $_[0]
> and
> $_[0] =~ /([A-Z]+)\(0x[a-f0-9]+\)\z/
> and
> $1
> }
>Does this make me a winner?
It would if it correctly identified the expression
bless [], "0"
as an ARRAY!
--
Sean McAfee mcafee@umich.edu
print eval eval eval eval eval eval eval eval eval eval eval eval eval eval
q!q@q#q$q%q^q&q*q-q=q+q|q~q:q? Just Another Perl Hacker ?:~|+=-*&^%$#@!
------------------------------
Date: Fri, 22 Oct 1999 18:23:44 GMT
From: mcafee@waits.facilities.med.umich.edu (Sean McAfee)
Subject: Re: Reference challenge
Message-Id: <QY1Q3.432$4G.83855@news.itd.umich.edu>
In article <eli$9910212026@qz.little-neck.ny.us>,
Eli the Bearded <*@qz.to> wrote:
>In comp.lang.perl.misc,
>Sean McAfee <mcafee@waits.facilities.med.umich.edu> wrote:
>> The challenge:
>> Write a 100% Perl, 100% portable, 100% bulletproof subroutine that will
>> determine whether its single argument is a reference, and if so, what type
>> of reference it is.
>Maybe I'm missing something here, but what's wrong with the
>builtin function ref()?
First, it returns the package a blessed reference has been blessed into,
not the type of the reference. ref(bless [], "FooBar") return "FooBar",
not (say) "ARRAY".
Secondly, it cannot portably distinguish between a non-reference and a
reference blessed into package "0".
--
Sean McAfee mcafee@umich.edu
print eval eval eval eval eval eval eval eval eval eval eval eval eval eval
q!q@q#q$q%q^q&q*q-q=q+q|q~q:q? Just Another Perl Hacker ?:~|+=-*&^%$#@!
------------------------------
Date: Fri, 22 Oct 1999 13:43:00 -0400
From: Jeff Pinyan <jeffp@crusoe.net>
Subject: Re: REFERENCE CHALLENGE: final (?) realref() test
Message-Id: <Pine.GSO.4.10.9910221342150.15158-100000@crusoe.crusoe.net>
# fix to realref
# note: conversation has been going on in P5P
# apparently, this already exists :(
#!/usr/bin/perl
use Benchmark qw( timethese );
use overload;
use strict;
use EvilClass;
sub realref1 (;$@);
sub realref2 (;$@);
my $t = new EvilClass ("EvilClass(0xfffff)");
my @test = (
[],
\[],
bless([], "ARRAY=ARRAY"),
bless([], "FOO"),
bless({}, "BAR"),
sub {},
"ARRAY(0xfffff)",
bless([], "HASH=ARRAY"),
bless({}, "Fruit\ny=ARRAY(0xffffff)"),
bless([], ""),
bless({}),
bless(sub {}, "GLOB"),
"ARRAY(0xfffff)",
bless(\*STDOUT, "HASH=ARRAY"),
bless(\[], "Fruit\ny=ARRAY(0xffffff)"),
$t,
\$t,
);
for (@test){
($a,$b) = (realref1,realref2);
print "$_\n\t'$a'\n\t'$b'\n\n" if $a ne $b;
}
timethese(500000, {
'1 - regex' => q{ for (@test){ $ref = realref1 } },
'2 - str' => q{ for (@test){ $ref = realref2 } },
});
sub realref1 (;$@) {
my $save = my $obj = (defined $_[0] ? shift : $_);
my ($type,$ret) = ref $obj;
wantarray ? return (undef, @_) : return unless length $type;
$obj = overload::StrVal($obj);
($ret) = $obj =~ /([A-Z]+)\(0x[a-f0-9]+\)$/;
$ret = "REF" if $ret eq "SCALAR" and (caller())[3] ne "main::realref1" and realref1($$save);
wantarray ? return ($ret, @_) : return $ret;
}
sub realref2 (;$@) {
my $save = my $obj = (defined $_[0] ? shift : $_);
my ($type,$ret,$pos) = ref $obj;
wantarray ? return (undef, @_) : return unless length $type;
$obj = overload::StrVal($obj);
$pos = ($pos = rindex($obj,"=")) == -1 ? 0 : $pos+1;
$ret = substr($obj,$pos,rindex($obj,"(")-$pos);
$ret = "REF" if $ret eq "SCALAR" and (caller())[3] ne "main::realref2" and realref2($$save);
wantarray ? return ($ret, @_) : return $ret;
}
package EvilClass;
use overload (
'""' => stringy,
);
sub new {
my ($class,$text) = @_;
return bless { text => $text }, $class;
}
sub stringy {
return shift->{text};
}
1;
--
MIDN 4/C PINYAN, USNR, NROTCURPI
jeff pinyan japhy@pobox.com
perl stuff japhy+perl@pobox.com
CPAN ID: PINYAN http://www.perl.com/CPAN/authors/id/P/PI/PINYAN/
------------------------------
Date: Fri, 22 Oct 1999 18:13:59 GMT
From: mcafee@waits.facilities.med.umich.edu (Sean McAfee)
Subject: Re: REFERENCE CHALLENGE: final (?) realref() test
Message-Id: <HP1Q3.430$4G.83809@news.itd.umich.edu>
In article <Pine.GSO.4.10.9910221342150.15158-100000@crusoe.crusoe.net>,
Jeff Pinyan <japhy@pobox.com> wrote:
>sub realref1 (;$@) {
> my $save = my $obj = (defined $_[0] ? shift : $_);
> my ($type,$ret) = ref $obj;
> wantarray ? return (undef, @_) : return unless length $type;
# ...
>}
>sub realref2 (;$@) {
> my $save = my $obj = (defined $_[0] ? shift : $_);
> my ($type,$ret,$pos) = ref $obj;
> wantarray ? return (undef, @_) : return unless length $type;
# ...
>}
Sorry, but these won't do. You've fallen afoul of the second condition of
the challenge, which is that you not use any undocumented implementation
details of your version of Perl. ref() is guaranteed only to return FALSE
if its argument is not a reference, but realref1 and realref2 both use
the fact that it returns a null string on your particular version of Perl.
Another version of Perl, or even the same version on a different
architecture, would be perfectly within its rights to return a 0 to
indicate that its argument is not a reference, making it impossible to
portably use ref() to separate non-references from references blessed into
package "0".
And incidentally, both realref1 and realref2 would incorrectly operate on
$_ if the value undef were passed as their first argument. Better to use
the length of @_ instead:
my $save = my $obj = @_ ? shift : $_;
--
Sean McAfee mcafee@umich.edu
print eval eval eval eval eval eval eval eval eval eval eval eval eval eval
q!q@q#q$q%q^q&q*q-q=q+q|q~q:q? Just Another Perl Hacker ?:~|+=-*&^%$#@!
------------------------------
Date: Fri, 22 Oct 1999 11:17:44 -0700
From: tgy@chocobo.org (Neko)
Subject: Re: REFERENCE CHALLENGE: final (?) realref() test
Message-Id: <a6gQOEA+jtGuPyf9Vl1GyCEHUN2P@4ax.com>
On Fri, 22 Oct 1999 13:43:00 -0400, Jeff Pinyan <jeffp@crusoe.net> wrote:
># fix to realref
[snip]
>use overload;
The following does not return 'ARRAY' as expected:
tie $mytie, 'MYTIE';
print realref1 $mytie;
package MYTIE;
sub TIESCALAR { bless [], 'MYTIE' }
sub STORE {}
sub FETCH {}
--
Neko | tgy@chocobo.org | Will hack Perl for a moogle stuffy! =^.^=
------------------------------
Date: 22 Oct 1999 13:02:47 -0400
From: kent@darwin.eeb.uconn.edu
Subject: Re: SGML/HTML parsing tool
Message-Id: <wk66zzmiq0.fsf@darwin.eeb.uconn.edu>
>>>>> "Tad" == Tad McClellan <tadmc@metronet.com> writes:
Tad> nsgmls (sgmls is way out of date) is an application built
Tad> with a toolkit (in C++).
I was referring to SGMLS.pm, which provides a Perl interface to the
output of nsgmls.
Kent
--
Kent E. Holsinger Kent@Darwin.EEB.UConn.Edu
http://darwin.eeb.uconn.edu
-- Department of Ecology & Evolutionary Biology
-- University of Connecticut, U-43
-- Storrs, CT 06269-3043
------------------------------
Date: Fri, 22 Oct 1999 10:18:22 -0700
From: Larry Rosler <lr@hpl.hp.com>
Subject: Re: sorting hashes in an array of hashes
Message-Id: <MPG.127a3c362ff2f65998a0fd@nntp.hpl.hp.com>
In article <7upj3r$o2c$1@nnrp1.deja.com> on Fri, 22 Oct 1999 11:51:24
GMT, c_j_marshall@my-deja.com <c_j_marshall@my-deja.com> says...
+ > foreach my $href (@ptr)
+ > {
+ > print "\n";
+ > foreach my $term ( sort { $href->{$a} <=> $href->{$b} }
+ > keys %{$href} )
+ > {
+ > print "$term -> $href->{$term}\n";
+ > }
+ > }
+
+ This worries me because my first reaction to accessing an array of
+ hashes would be:
+
+ foreach my $hash_ref (@array)
+ {
+ # de-ref the hash
+ my %hash = %$hash_ref;
+ foreach my $keyfield (sort keys %hash)
+ {
+ print "$keyfield : $hash{$keyfield}\n";
+ }
+ }
+
+ I don't doubt for a minute this is the slowest and least efficient way
+ of doing things - but would like to learn why.
Because copying the hash is much more expensive than accessing each
element via a reference to the hash.
--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: Fri, 22 Oct 1999 11:30:42 -0700
From: Tom Phoenix <rootbeer@redcat.com>
Subject: Re: use 'use' and 'no' like pragmas??
Message-Id: <Pine.GSO.4.10.9910221122480.29843-100000@user2.teleport.com>
On Wed, 20 Oct 1999, James Tolley wrote:
> I am writing a module which ties STDOUT when you 'use Module;'.
>
> I'm hoping that there's a way to (temporarily (and locally, if
> possible)) untie STDOUT with a simple 'no Module;' - similar to turning
> on and off strict with a "no strict 'refs'".
If you mean you want it lexically scoped, no. Unless this ability snuck in
when I wasn't watching....
> I think not. unimport() gets called as if the 'no' is in a BEGIN
> block...
Right. 'no' (like 'use') happens at compile time.
Maybe you should be using the one-arg select instead? Or maybe someone who
temporarily wants the untied STDOUT should do this?
use Your_module qw/ $real_STDOUT /;
print STDOUT "This is sent to the tied filehandle.\n";
{
local(*STDOUT) = $real_STDOUT;
print STDOUT "This is untied.\n";
...
}
print STDOUT "This is restored to what it was before.\n";
Cheers!
--
Tom Phoenix Perl Training and Hacking Esperanto
Randal Schwartz Case: http://www.rahul.net/jeffrey/ovs/
------------------------------
Date: Fri, 22 Oct 1999 17:35:20 GMT
From: sdriscol@oclc.org (Steve Driscoll)
Subject: Re: XML::XSL
Message-Id: <3810a01e.1801564392@24.95.45.103>
I'd be interested in this too! So Kevin, if you get any replies,
please let me know.
Steve Driscoll sdriscol@oclc.org
P.S. I tried to reply via email, but your address is (obviously)
invalid.
On 22 Oct 1999 15:05:36 GMT, johnDoe@noSpamToday.com (Kevin Scott)
wrote:
>Has anyone come a across any perl that can take an XML file and apply a XSL
>styleseet to it and return the resulting document. This would be used to
>convert an XML document to HTML for excample.
>
>
>Kevin Scott
>
------------------------------
Date: 16 Sep 99 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 16 Sep 99)
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: The mail to news gateway, and thus the ability to submit articles
| through this service to the newsgroup, has been removed. I do not have
| time to individually vet each article to make sure that someone isn't
| abusing the service, and I no longer have any desire to waste my time
| dealing with the campus admins when some fool complains to them about an
| article that has come through the gateway instead of complaining
| to the source.
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 V9 Issue 1153
**************************************