[14002] in Perl-Users-Digest
Perl-Users Digest, Issue: 1412 Volume: 9
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Nov 17 18:23:13 1999
Date: Wed, 17 Nov 1999 15:20:32 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <942880831-v9-i1412@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Wed, 17 Nov 1999 Volume: 9 Number: 1412
Today's topics:
Re: Trying to use a variable as an operator (Mark-Jason Dominus)
Re: Trying to use a variable as an operator (Kragen Sitaker)
Re: Trying to use a variable as an operator <cassell@mail.cor.epa.gov>
Re: Trying to use a variable as an operator (Mark-Jason Dominus)
Re: Trying to use a variable as an operator (Kragen Sitaker)
Re: Trying to use a variable as an operator (Kragen Sitaker)
Re: Use of uninitialized value <aqumsieh@matrox.com>
Re: Use sort function with reference to subroutine? <uri@sysarch.com>
Re: Use sort function with reference to subroutine? <uri@sysarch.com>
Re: Use sort function with reference to subroutine? <aqumsieh@matrox.com>
Re: Wierd List Behavior tigger149@my-deja.com
xml:rss <darren.shrubsole@bbc.co.uk>
Re: xml:rss (Eric Bohlman)
Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 17 Nov 1999 19:42:51 GMT
From: mjd@op.net (Mark-Jason Dominus)
Subject: Re: Trying to use a variable as an operator
Message-Id: <80v0eu$b1g$1@monet.op.net>
In article <cnzY3.21345$YI2.959667@typ11.nn.bcandid.com>,
Kragen Sitaker <kragen@dnaco.net> wrote:
>In article <38328215.D08A6D61@burkby.com>, Joe Burpee <jeb@burkby.com> wrote:
>Obviously this is the correct thing to do when it is possible. But
>being given an arbitrary code block makes it difficult for reduce to
>deduce the identity element, if there is one.
That was what I had in mind when I prototyped my perl `reduce'
function as (&$@) rather than (&@). The idea was that the required
scalar was the identity element.
A cooler version of the function would have detected when the identity
was the only element present, and return a closure which implemented
the appropriate reducing function, like this:
# I didn't test this.
sub reduce (&$@) {
my $code = shift;
my $identity = shift;
my $reducer = sub {
local $a = $identity;
local $b;
for $b (@_) {
$a = &$code;
}
return $a;
};
return @_ ? $sub->(@_) : $sub;
}
Now you don't have to say
reduce { $a + $b } 0, (1, 4, 2, 8, 5, 7);
Instead, you can say
# Honk if you'd rather be programming in ML
$sum_them = reduce { $a + $b } 0;
$sum_them->(1, 4, 2, 8, 5, 7);
$sum_them->(@array);
A few weeks ago someone convinced me that the (&$@) prototype had been
a bad decision, but I don't remember why. I remember being really
really sleepy at the time, and not being able to remember all the cool
reasons why I did it in the first place, and not even being able to
make any up on the spot, so there's a good likelihood that he was
wrong.
------------------------------
Date: Wed, 17 Nov 1999 19:47:49 GMT
From: kragen@dnaco.net (Kragen Sitaker)
Subject: Re: Trying to use a variable as an operator
Message-Id: <FDDY3.21970$YI2.992906@typ11.nn.bcandid.com>
In article <3832DFD6.8458EF@burkby.com>, Joe Burpee <jeb@burkby.com> wrote:
>Kragen Sitaker wrote:
>> Joe Burpee wrote:
>> >BTW, one thing missing from your reduce seems to be the handling of the
>> >empty-list case,
>> being called with undef arguments, and having it return its identity
>> element. This is probably not optimal from a readability and efficiency
>> standpoint.)
>
>Right, that was what I was thinking of, and the phrase "not optimal" is
>probably far too kind. I really didn't know how to do it elegantly, and
>was hoping maybe you knew. ;-)
Well, you could pass two items -- the block and the identity element --
and the list. Actually, it turns out doing this doesn't require any
change to the reduce function -- you can just say reduce {$a + $b} 0,
@list;. You could package the block and the identity element into an
array: my @by_adding = sub {$a + b}, 0);
>At any rate the fact that it is doable at all in Perl suggests an
>interesting potential advantage over APL. As you're probably aware, in
>APL (but not in J) a user-defined function can be dyadic, monadic, or
>niladic -- 2, 1, or 0 args respectively. An ambivalent function may be
>called monadically, and you can trap the condition of no left arg. But
>you cannot readily trap the no-args situation.
Perl is good at introspection.
>Anyway, I find your efforts inspiring. I have not tended to do much
>vector-algebraic stuff in Perl, except what I can do directly with map
>and maybe grep. Knowing APL is always there makes me lazy I guess.
APL isn't there for me; I guess maybe I should get it.
--
<kragen@pobox.com> Kragen Sitaker <http://www.pobox.com/~kragen/>
The Internet stock bubble didn't burst on 1999-11-08. Hurrah!
<URL:http://www.pobox.com/~kragen/bubble.html>
------------------------------
Date: Wed, 17 Nov 1999 12:06:18 -0800
From: David Cassell <cassell@mail.cor.epa.gov>
Subject: Re: Trying to use a variable as an operator
Message-Id: <38330ABA.7FA1804C@mail.cor.epa.gov>
Mark-Jason Dominus wrote:
[snip]
> Yeah. Someday I'm going to write APL.pm. Then the world will
> tremble. [my snip]
Well, my sysadmins will, when I begin pestering them to let me
install it. :-)
reductions.. scans.. inner products.. outer products...
[big snip]
> o I borrowed this from the APL folks
>
> o Those APL folks are twisted. Stay away from them.
Hey, how can you say bad things about a language with a domino
for an operator? [I wrote a lot of programs using that domino.]
David
--
David Cassell, OAO cassell@mail.cor.epa.gov
Senior computing specialist
mathematical statistician
------------------------------
Date: Wed, 17 Nov 1999 20:28:30 GMT
From: mjd@op.net (Mark-Jason Dominus)
Subject: Re: Trying to use a variable as an operator
Message-Id: <80v344$bnv$1@monet.op.net>
In article <_zzY3.21355$YI2.960985@typ11.nn.bcandid.com>,
Kragen Sitaker <kragen@dnaco.net> wrote:
>Hmm, what do you do when the arrays aren't conformable?
The code I showed represents a conscious and specific design choice
about what to do if the arrays aren't conformable. (Believe it or not.)
>In general I don't like dynamic scoping, but I guess it's worth it
>here, for readability and consistency with sort.
Yeah, I agree entirely.
>> But of course > doesn't yield enough information.
>
>It depends on your sorting algorithm, I think.
Naah, I was just half asleep.
# Turn a function like `less than' into a spaceship
sub spaceship {
my $op = shift;
return -1 if $op->($a, $b);
return 1 if $op->($b, $a); # Duhh.
return 0;
}
That proves that there's enough information, as long as $op is
actually a reflexive, transitive, nonsymmetric relation. And if it
isn't, then the result of the sort isn't well-defined anyway.
There's another one that would have ben easier in ML.
- fun spaceship x (a,b) =
if x(a,b) then ~1
else if x(b,a) then 1
else 0
;
val spaceship = fn : ('a * 'a -> bool) -> 'a * 'a -> int
- val <=> = spaceship (op < : int * int -> bool);
val <=> = fn : int * int -> int
- infix <=>;
- val cmp = spaceship (op < : string * string -> bool);
val cmp = fn : string * string -> int
- infix cmp;
- 3 <=> 4;
val it = ~1 : int
- "go" cmp "away";
val it = 1 : int
------------------------------
Date: Wed, 17 Nov 1999 20:30:27 GMT
From: kragen@dnaco.net (Kragen Sitaker)
Subject: Re: Trying to use a variable as an operator
Message-Id: <DfEY3.22006$YI2.996954@typ11.nn.bcandid.com>
In article <80v0eu$b1g$1@monet.op.net>, Mark-Jason Dominus <mjd@op.net> wrote:
>That was what I had in mind when I prototyped my perl `reduce'
>function as (&$@) rather than (&@). The idea was that the required
>scalar was the identity element.
Ah, I see.
>A cooler version of the function would have detected when the identity
>was the only element present, and return a closure which implemented
>the appropriate reducing function, like this:
IMHO, it is bad interface design to return a closure sometimes and the
result of applying the closure (which could be a number, or worse,
could be another closure) other times.
>Now you don't have to say
>
> reduce { $a + $b } 0, (1, 4, 2, 8, 5, 7);
>
>Instead, you can say
>
> # Honk if you'd rather be programming in ML
> $sum_them = reduce { $a + $b } 0;
> $sum_them->(1, 4, 2, 8, 5, 7);
> $sum_them->(@array);
Niftaroni. I'd forgotten how handy closures were for this sort of
thing. And if you don't want to name $sum_them, of course, you can say
(reduce {$a + $b} 0)->(1, 4, 2, 8, 5, 7); # UNTESTED
It's a little nicer in Scheme; you can talk about + by itself instead
of having to talk about sub {$a + $b} (in Scheme, (lambda () (+ a
b))).
This one again evaluates from right to left:
(define (reduce op identity)
(letrec ((reducer
(lambda (list)
(if (null? list) identity
(op (car list) (reducer (cdr list)))))))
(lambda list (reducer list))))
; Then you can say:
(define +/ (reduce + 0))
(display (+/ 1 4 2 8 5 7)) (newline)
; or just
(display ((reduce + 0) 1 4 2 8 5 7)) (newline)
(Tested in DrScheme, version 53 I think.)
I think the Scheme version captures much of the flavor of the APL
original, at least for 1-D arrays.
>A few weeks ago someone convinced me that the (&$@) prototype had been
>a bad decision, but I don't remember why.
The prototype does have some bad effects, even with (&@):
#!/usr/bin/perl -w
use strict;
sub reduce (&@) {
my $op = shift;
my $first = shift;
return @_ ? $op->($first, reduce($op, @_)) : $first;
}
my @with_plus = (sub {$_[0] + $_[1]}, 0);
# CAN'T DO THIS: print reduce @with_plus, 1, 2, 3, 4;
print &reduce(@with_plus, 1, 2, 3, 4); # can do this instead
But I can't see why (&$@) is any worse than (&@).
--
<kragen@pobox.com> Kragen Sitaker <http://www.pobox.com/~kragen/>
The Internet stock bubble didn't burst on 1999-11-08. Hurrah!
<URL:http://www.pobox.com/~kragen/bubble.html>
------------------------------
Date: Wed, 17 Nov 1999 20:36:10 GMT
From: kragen@dnaco.net (Kragen Sitaker)
Subject: Re: Trying to use a variable as an operator
Message-Id: <_kEY3.22009$YI2.995528@typ11.nn.bcandid.com>
In article <80v344$bnv$1@monet.op.net>, Mark-Jason Dominus <mjd@op.net> wrote:
>In article <_zzY3.21355$YI2.960985@typ11.nn.bcandid.com>,
>Kragen Sitaker <kragen@dnaco.net> wrote:
>>Hmm, what do you do when the arrays aren't conformable?
>
>The code I showed represents a conscious and specific design choice
>about what to do if the arrays aren't conformable. (Believe it or not.)
Oops, I missed the @$a && @$b part. :)
That is a very Perlish choice.
>>> But of course > doesn't yield enough information.
>>
>>It depends on your sorting algorithm, I think.
> # Turn a function like `less than' into a spaceship
>sub spaceship { . . . }
Oh, OK. Dunno why I didn't think of that.
--
<kragen@pobox.com> Kragen Sitaker <http://www.pobox.com/~kragen/>
The Internet stock bubble didn't burst on 1999-11-08. Hurrah!
<URL:http://www.pobox.com/~kragen/bubble.html>
------------------------------
Date: Wed, 17 Nov 1999 15:29:23 -0500
From: Ala Qumsieh <aqumsieh@matrox.com>
Subject: Re: Use of uninitialized value
Message-Id: <x3yu2mkzwt8.fsf@tigre.matrox.com>
Rowan Blaqflame <blaqflame@hotmail.com> writes:
> Can anyone tell me a better way to deal with null variables? I can
> ignore the errors, but I hate leaving something alone that I don't
> understand.
perldoc -f defined
--Ala
------------------------------
Date: 17 Nov 1999 13:07:23 -0500
From: Uri Guttman <uri@sysarch.com>
Subject: Re: Use sort function with reference to subroutine?
Message-Id: <x766z1t2jo.fsf@home.sysarch.com>
>>>>> "TL" == Thelma Lubkin <thelma@alpha2.csd.uwm.edu> writes:
TL> : So don't dereference your coderef.
TL> : @aray = sort $subref @aray;
TL> : Requires 5.005_03 or higher.
TL> ..ah, there's my problem, then. I actually did try
TL> that, and got this:
TL> Not a GLOB reference at try1 line 9.
TL> But perl -v shows,
TL> This is perl, version 5.004_04 built for alpha-dec_osf
simple fix (which is documented in perldoc -f sort):
*sortglob = $subref ;
or
*sortglob = \&sortsub ;
@sorted = sort sortglob @array ;
that will allow sort to call any arbitrary sub.
uri
--
Uri Guttman --------- uri@sysarch.com ---------- http://www.sysarch.com
SYStems ARCHitecture, Software Engineering, Perl, Internet, UNIX Consulting
The Perl Books Page ----------- http://www.sysarch.com/cgi-bin/perl_books
The Best Search Engine on the Net ---------- http://www.northernlight.com
------------------------------
Date: 17 Nov 1999 13:18:52 -0500
From: Uri Guttman <uri@sysarch.com>
Subject: Re: Use sort function with reference to subroutine?
Message-Id: <x7zowdrng2.fsf@home.sysarch.com>
>>>>> "TL" == Thelma Lubkin <thelma@alpha2.csd.uwm.edu> writes:
TL> : @aray = sort { &$subref() } @aray;
TL> Thank you -- that does it.
TL> This is a tremendously helpful group.
check out my typeglob solution (posted earlier). it will be faster as it
has one less level of sub calling. and it looks cleaner IMO.
uri
--
Uri Guttman --------- uri@sysarch.com ---------- http://www.sysarch.com
SYStems ARCHitecture, Software Engineering, Perl, Internet, UNIX Consulting
The Perl Books Page ----------- http://www.sysarch.com/cgi-bin/perl_books
The Best Search Engine on the Net ---------- http://www.northernlight.com
------------------------------
Date: Wed, 17 Nov 1999 14:54:19 -0500
From: Ala Qumsieh <aqumsieh@matrox.com>
Subject: Re: Use sort function with reference to subroutine?
Message-Id: <x3yvh70zyfq.fsf@tigre.matrox.com>
Thelma Lubkin <thelma@alpha2.csd.uwm.edu> writes:
> I am trying to use the perl sort function with a reference to a subroutine
> that will define the particular sort wanted for various cases.
Good. You read the docs on sort(), didn't you?
> I've been unable to figure out how to do this; apparently the syntax
> I'm using makes the array to be sorted an argument of the sort subroutine,
> and arguments to this subroutine are not allowed. Here's an example,
> along with the error message it generates:
The syntax for sort() is explained well in perlfunc.
> What I ran:
> ----------------------
> #!/usr/bin/perl -w
> use strict;
>
> sub asub; #### Declare subroutine with sorting instructions
> my $subref = \&asub; #### Reference to the subroutine
>
> my @aray = (9, 14, 6, 11); #### An array to be sorted
>
> @aray = sort &$subref @aray; #### Attempt to use sort function: fails
Of course this will fail. From perlfunc:
sort SUBNAME LIST
sort BLOCK LIST
sort LIST
Sorts the LIST and returns the sorted list value. If
SUBNAME or BLOCK is omitted, `sort()'s in standard
string comparison order. If SUBNAME is specified, it
gives the name of a subroutine that returns an integer
less than, equal to, or greater than `0', depending on
how the elements of the array are to be ordered. (The
`<=>' and `cmp' operators are extremely useful in such
routines.) SUBNAME may be a scalar variable name
(unsubscripted), in which case the value provides the
name of (or a reference to) the actual subroutine to
use. In place of a SUBNAME, you can provide a BLOCK as
an anonymous, in-line sort subroutine.
The las two sentences explain it all.
> print "aray: @aray\n";
>
> sub asub #### Subroutine with sorting instructions
> { $a <=> $b; }
>
> When I ran it
> ---------------------
> Script started on Wed Nov 17 07:50:25 1999
> ~/www.cgi-bin/ Wed 7:50am 17 Nov alpha2
> Array found where operator expected at try line 11, at end of line
> (Missing operator before ?)
> syntax error at try line 11, near "$subref @aray"
> Execution of try aborted due to compilation errors.
> ~/www.cgi-bin/ Wed 7:50am 17 Nov alpha2
> exit
> script done on Wed Nov 17 07:50:47 1999
>
> What do I need to correct?
Refer to the above.
Please try to consult the docs before posting.
HTH,
--Ala
------------------------------
Date: Wed, 17 Nov 1999 20:18:49 GMT
From: tigger149@my-deja.com
Subject: Re: Wierd List Behavior
Message-Id: <80v2j5$c8u$1@nnrp1.deja.com>
In article <3831FBBF.673CB2F1@nortelnetworks.com>,
Brandon Metcalf <bmetcalf@nortelnetworks.com> wrote:
> > Ok I found some wierd behavior. I am wondering if this is a perl
bug or
> > if it is documented somewhere?
> >
> > Here is some example code:
> >
> > ($a,$b,$) = ('First',,'Third');
> >
> > print "A: $a B: $b C: $c";
> >
> > -------------------
> >
> > The output of the code would look like:
> >
> > A: First B: Third C:
> >
> > Why does this happen this way. It is particularly disturbing
because if
> > you did something like
> >
> > &foo(1,,,4); Your function arguments all get scooted down.
>
> That's why you prototype your functions or either check inside your
> functions for the correct number of arguments.
>
> This is not a bug.
>
> Brandon
>
>
Well the first example is not a function and Perl with -w doesn't even
scream about it. Function prototypes might or might not help because
the function we are using has optional parameters and there are cases
when you need the 1st paramater and the 6th parameter but not the
intervening ones. I admit it's easy enough to just write &foo
($a,undef,undef,...) to fill in the empty parameters and that is what
we're doing now. I was just wondering why perl doesn't even scream
about this. Or even better throw a parse error.
Sent via Deja.com http://www.deja.com/
Before you buy.
------------------------------
Date: Wed, 17 Nov 1999 17:01:26 -0000
From: "Darren Shrubsole" <darren.shrubsole@bbc.co.uk>
Subject: xml:rss
Message-Id: <80un50$f0f$1@nntp0.reith.bbc.co.uk>
Does anybody have a copy of xml:rss module compiled for Win32?
Regards, Darren.
------------------------------
Date: 17 Nov 1999 19:18:44 GMT
From: ebohlman@netcom.com (Eric Bohlman)
Subject: Re: xml:rss
Message-Id: <80uv2k$981$1@nntp6.atl.mindspring.net>
Darren Shrubsole (darren.shrubsole@bbc.co.uk) wrote:
: Does anybody have a copy of xml:rss module compiled for Win32?
XML::RSS is pure Perl, so all you need to do is uncompress the package
(WinZip will extract from .tar.gz files, or else you can get Win32 ports
of tar and gzip) and copy RSS.pm to the appropriate directory in your lib
tree (which is e:/perl/site/lib/xml in my installation, but then I have a
peculiar attitude toward drive letters).
------------------------------
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 1412
**************************************