[10282] in Perl-Users-Digest
Perl-Users Digest, Issue: 3875 Volume: 8
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Oct 2 14:07:16 1998
Date: Fri, 2 Oct 98 11:00:17 -0700
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Fri, 2 Oct 1998 Volume: 8 Number: 3875
Today's topics:
Re: ****Can you show me a simple perl script to run the <gellyfish@btinternet.com>
Re: Benchmark: ST vs OM (Larry Rosler)
Re: connecting to a Mysql via command line ok FAILs wit <dwatanab@uci.edu>
Re: DBI/mysql <dwatanab@uci.edu>
Re: FAQ: Daylightsavings problem (Larry Rosler)
Re: how can I build array of lists <michel.prevost@cactuscom.ca_REMOVE_TO_MAIL>
How do I remove a carriage return or space from a strin <brettr@centuryinter.net>
Re: How do I remove a carriage return or space from a s <cbandon_nospam@tiac.net>
Re: How do I remove a carriage return or space from a s <baliga@synopsys.com>
HTTP_REFERER missing when it shouldn't be <richw3@ix.netcom.com>
Re: I hate it when I do that. <jdporter@min.net>
Re: Messages I never read <cbandon_nospam@tiac.net>
Re: Messages I never read <eashton@bbnplanet.com>
Re: per <jdporter@min.net>
Performance issue with not reassigning referenced argum <mattdb@syntrillium.com>
Perl as solution? <eashton@bbnplanet.com>
Re: send geroge reese (was Re: Call for Participation: (Abigail)
Re: string from array (Larry Rosler)
Re: What is the maining of "Bare word" <michel.prevost@cactuscom.ca_REMOVE_TO_MAIL>
Special: Digest Administrivia (Last modified: 12 Mar 98 (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 2 Oct 1998 01:36:31 -0000
From: Jonathan Stowe <gellyfish@btinternet.com>
Subject: Re: ****Can you show me a simple perl script to run the POST method?
Message-Id: <6v1amv$pu$1@gellyfish.btinternet.com>
On Fri, 02 Oct 1998 10:50:00 GMT James Bond 098 <burningboy@hotmail.com> wrote:
> Plz show me a simple script to POST method(Don't USE any module)
I would look inside a module or library that does this already. Trying to
reimplement this is bound to end in tears - it could be done simply but it
might not be done simply *well* . If you really must do this then I would
recommend reading the CGI specification from http://hoohoo.ncsa.uiuc.edu and
also rfc1945 and understanding what they are saying before to trying to
reinvent the wheel.
/J\
--
Jonathan Stowe <jns@btinternet.com>
Some of your questions answered:
<URL:http://www.btinternet.com/~gellyfish/resources/wwwfaq.htm>
------------------------------
Date: Fri, 2 Oct 1998 10:16:17 -0700
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: Benchmark: ST vs OM
Message-Id: <MPG.107eaa3ca4baec0b9897d4@nntp.hpl.hp.com>
[Posted to comp.lang.perl.misc and a copy mailed.]
In article <3614F674.CF723742@min.net> on Fri, 02 Oct 1998 11:51:16 -
0400, John Porter <jdporter@min.net> says...
> I've recently been benchmarking the relative performance of
> the Schwartzian Transform (ST) (aka. map-sort-map) vs.
> the Orcish Maneuver (say "OR-cache").
>
> I recently posted some results that indicate that ST is
> more than an order of magnitude slower than OM.
> However, those tests were not quite realistic, in that they
> discarded the results of the operation. Apparently Perl can
> optimize for this, under the right conditions. What follows
> is a benchmark wherein the results are not discarded.
...
> Benchmark: timing 10 iterations of OM, ST, c0, ct...
> OM: 33 secs (32.51 usr 0.05 sys = 32.56 cpu)
> ST: 30 secs (29.70 usr 0.09 sys = 29.79 cpu)
> c0: 2 secs ( 2.52 usr 0.00 sys = 2.52 cpu)
> ct: 12 secs (11.48 usr 0.00 sys = 11.48 cpu)
It's good that you got this straightened out, because the first results
you posted were troubling. OM runs slower than ST because an extra test
is needed (for TRUE) every time a comparison value is used.
It's perhaps worth pointing out that when any of the comparison values
computes to FALSE, OR will run even more slowly than ST, because the
FALSE value will be recomputed and stored every time the comparison value
is needed.
So, while OR is nice for such things as setting defaults:
$value ||= $default_value;
it is dominated in sort comparisons by ST.
--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: Fri, 02 Oct 1998 09:56:54 -0700
From: dana watanabe <dwatanab@uci.edu>
To: randy.paries@avex.com
Subject: Re: connecting to a Mysql via command line ok FAILs with perl
Message-Id: <3615059D.9FA6A83B@uci.edu>
To mimic others, install DBI and the MySQL/mSQL DBD driver,
i used to use the Mysql package and DBI is much nicer
But, what you should do is:
$dbh = Mysql->connect("node.com","unitdb","foo","username")
or die "Could not connect: $Mysql::db_errstr\n";
$Mysql::db_err gives you an error code i believe
and $Mysql::db_errstr gives the error string
If you are connecting to a database on the host that your perl script is on
then leave the 'host' entry to the connect procedure empty, that is:
$dbh = Mysql->connect("","unitdb","foo","username")
or die "Could not connect: $Mysql::db_errstr\n";
randy.paries@avex.com wrote:
> Hello,
> Please help..
>
> I can connect to my database via command line
> /usr/local/mysql/bin/mysql --password=foo unitdb
>
> this works ok.
>
> I can not attatch via a very simple perl script(see error below code):
> #!/usr/local/bin/perl
>
> use Mysql;
>
> $dbh = Mysql->connect("node.com","unitdb","foo","username");
>
------------------------------
Date: Fri, 02 Oct 1998 09:41:23 -0700
From: dana watanabe <dwatanab@uci.edu>
To: dpuryear@usa.net
Subject: Re: DBI/mysql
Message-Id: <361501FB.B89C1B92@uci.edu>
Since i didn't see this stuff mentioned...
Check out:
http://www.turbolift.com/mysql/DBD_3.21.X.html
which is titled: MySQL Perl DBI/DBD Manual version 0.4
I haven't read it, but for MySQL, http://www.turbolift.com/mysql
is pretty invaluable overall. (It has the MySQL docs and other stuff)
Skimming that Manual, it has a good quick guide to all the variables
and procedures and whatnaughts that you might need
In the main, MySQL docs, you get decent coverage of the SQL commands
you want to run in:
http://www.turbolift.com/mysql/chapter3.html
Also, you might want to check out
http://w3.one.net/~jhoffman/sqltut.htm
(An SQL Tutorial)
Dustin Puryear wrote:
> How do I *create* a table in a database using DBI? I read the DBI
> info. and couldn't find it. I tried to create a program, taken from the
> documentation, but it doesn't work. ANY help would be appreciated since
> I can't find _any_ information on how to create a table rather than
> retrieve information from one. Please email a copy of your reply, if
> possible, because my access to a news server is only 1 to 2 days a week.
>
> Regards, Dustin
>
>
------------------------------
Date: Fri, 2 Oct 1998 10:42:50 -0700
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: FAQ: Daylightsavings problem
Message-Id: <MPG.107eb077c38a69e59897d6@nntp.hpl.hp.com>
[Posted to comp.lang.perl.misc and a copy mailed.]
In article <3614FE20.41C6@arch.ethz.ch> on Fri, 02 Oct 1998 18:24:00
+0200, Patrick Sibenaler <patrick@arch.ethz.ch> says...
...
> localtime (909352800) = [1998_10_26_00_00_00]
>
> ... very handy, but adding 1 day (84600secs):
>
> localtime (909439200) = [1998_10_26_23_00_00]
>
> instead of the expected [1998_10_27_00_00_00]
> ... daylight savings switch.
>
> How is this usually solved? I didn't find much in the faq
> about it...
Look in DejaNews. There has been lots of discussion of this.
You have discovered that 1 day != 86400 secs twice a year (and on the
same days in Switzerland?!), and there are various ways around that,
depending on your goal.
--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: Fri, 02 Oct 1998 12:37:53 -0400
From: Michel Prevost <michel.prevost@cactuscom.ca_REMOVE_TO_MAIL>
To: nfigaro@rocketmail.com
Subject: Re: how can I build array of lists
Message-Id: <36150161.CF811CC4@cactuscom.ca_REMOVE_TO_MAIL>
my $list1 = [a, b, c, d];
my $list2 = [1, 2, 3, 4];
nfigaro@rocketmail.com wrote:
> hello,
>
> I'd like to make an array of lists.
> ( or an array of arrays )
>
> my $list1 = (a b c d);
> my $list2 = (1 2 3 4);
>
> and
> my @TABLEAU;
>
> @TABLEAU[1] = $list1;
> @TABLEAU[2] = $list2;
>
> but it doesn't work.
>
> If anyone could help ,
>
> answer by email please
>
> Nicolas Figaro
>
> -----== Posted via Deja News, The Leader in Internet Discussion ==-----
> http://www.dejanews.com/rg_mkgrp.xp Create Your Own Free Member Forum
------------------------------
Date: Fri, 2 Oct 1998 12:05:30 -0500
From: "brettr" <brettr@centuryinter.net>
Subject: How do I remove a carriage return or space from a string?
Message-Id: <6v31b4$ckg$1@newsread1-mx.centuryinter.net>
Suppose the var $hello contains the string "hi " and has a space or carriage
return at the end of it. How would I remove the space or carriage return so
that only the word "hi" exist?
brettr
------------------------------
Date: Fri, 2 Oct 1998 13:30:42 -0400
From: "Craig Bandon" <cbandon_nospam@tiac.net>
Subject: Re: How do I remove a carriage return or space from a string?
Message-Id: <6v32le$qnr@news-central.tiac.net>
$hello = chop($hello);
--
Craig
to reply remove _nospam
brettr wrote in message <6v31b4$ckg$1@newsread1-mx.centuryinter.net>...
>Suppose the var $hello contains the string "hi " and has a space or
carriage
>return at the end of it. How would I remove the space or carriage return so
>that only the word "hi" exist?
>
>brettr
>
>
------------------------------
Date: Fri, 02 Oct 1998 10:49:02 -0700
From: Yogish Baliga <baliga@synopsys.com>
To: brettr <brettr@centuryinter.net>
Subject: Re: How do I remove a carriage return or space from a string?
Message-Id: <3615120E.2310EC25@synopsys.com>
use
$hello =~ s/\s*$//g;
This means remove all the whitespaces at end of string with null.
-- Baliga
brettr wrote:
> Suppose the var $hello contains the string "hi " and has a space or carriage
> return at the end of it. How would I remove the space or carriage return so
> that only the word "hi" exist?
>
> brettr
------------------------------
Date: Fri, 2 Oct 1998 09:52:15 -0700
From: "Richard Waddell" <richw3@ix.netcom.com>
Subject: HTTP_REFERER missing when it shouldn't be
Message-Id: <6v308u$kt6@dfw-ixnews9.ix.netcom.com>
Greetings,
This is really a CGI question, but I haven't found a CGI newsgroup.
Scenario:
You have a target script that generates an html page listing all the
environment variable names and values.
(1) Target script is posted to from a script generated html page, The
posting script is located on the same server as the target script,
HTTP_REFERER shows up as expected. Both scripts are written in perl
(2) Same target script is posted to from another script generated html
page. The posting script is located on a different server from the target
script. HTTP_REFERER does not show up in the list of environment variables.
The only other significant difference between the two situations is that in
(2), the Perl script doing the posting is located on a secure server. I
don't have access yet to a secure server to see if I can duplicate it. I
don't see why this should make a difference.
I've also tried saving off the generated page in (2) and running it from the
server in (1). Same result, HTTP_REFERER shows up when the posting page is
on the same server as the target script.
The only meaningful documentation I've been able to find on HTTP_REFERER is
in 'CGI Programming' by Shishir Gundavaram, and none of the discussion seems
to apply to this situation.
I don't know that being on different servers is significant, I don't know
why it would be. The only other difference I can think of is that the
posting page in (2) is the result of a sequence of script-generated html
pages.
Thanks,
Rich Waddell
------------------------------
Date: Fri, 02 Oct 1998 12:53:38 -0400
From: John Porter <jdporter@min.net>
Subject: Re: I hate it when I do that.
Message-Id: <36150512.AF5E4F33@min.net>
Andrew M. Langmead wrote:
>
> Its just that there is a school of
> thought that in writing substantial programs one should avoid implicit
> $_. I don't necessarily agree with it, but I do see their
> point. (Although I'd probably feel comforable with the rule "assign to
> an explicit variable instead of $_ if you can no longer see in your
> text editor both where $_ gets set and where it is used without
> scrolling.") If you were either in that camp, or straddling on the
> edge, it would be a good way of enforcing the rule.
In *my* country...
I use $_ as much as reasonably possible.
But I change it to a normal variable (hopefully 'my') whenever
I find myself needing to mention $_ explicitly more than about
2 or 3 times.
--
John "Many Jars" Porter
baby mother hospital scissors creature judgment butcher engineer
------------------------------
Date: Fri, 2 Oct 1998 13:02:52 -0400
From: "Craig Bandon" <cbandon_nospam@tiac.net>
Subject: Re: Messages I never read
Message-Id: <6v311b$poe@news-central.tiac.net>
Andy,
I guess I fall in the Bug thing. I have done a dejanews search and found
nothing on problems with foreach type problems. I have played around with my
code on several machines/hosts. I am really hunting right now as to what
rocks I haven't unturned. I can usually find answers to my questions in
books or in dejanews. So I have never posted hear and don't regularly read
hear.
Well for anyone that would be so kind as to take a moment to look at my code
I am having problems getting the CGI environment variables out of the %ENV.
I do a foreach on the keys of %ENV and only get back one environment
variable. But then when I go to a specific variable I get the information.
When I go back and do a foreach I get that specific variable listed.
My next thought was to create a new associative array and then see if I got
that back. My server is down right now so I can't test this but I suspect
that this will work (since when I get a specific variable that it then
appears in a for each).
Thanks for the kind words and advice. I am sorry if my post original post
(even this one) is lame but I am really stumped. I am not looking for
someone to solve my problem but if someone has seen something like this
before or has an idea to try I would greatly appreciate it.
Here is the output/code from my original post.
OUTPUT
Perl Version 5.00307
PERLXS='PerlIS'
ENV{"REQUEST_METHOD"}='GET'
REQUEST_METHOD='GET'
PERLXS='PerlIS'
CODE
#!/usr/local/bin/perl5
$| = 1;
print "Content-Type: text/html\n\n";
print "Perl Version " . $] . "<br><br>\n";
foreach $var ( keys %ENV )
{
print $var . "='" . $ENV{$var} . "'<br>\n";
}
print "<br>\n";
print "ENV{\"REQUEST_METHOD\"}='". $ENV{"REQUEST_METHOD"} . "'<br>\n";
print "<br>\n";
foreach $var ( keys %ENV )
{
print $var . "='" . $ENV{$var} . "'<br>\n";
}
--
Craig
to reply remove _nospam
Andy Lester wrote in message <6v2uir$f13$1@netnews.mc.net>...
>For all the novices out there, here are some sure ways to get the more
>knowledgeable of us to ignore your message:
>
>* Put "newbie" in the title.
>
>* Ask us "which is better, Perl or [x]", where [x] is something entirely
>non-comparable, such as Javascript.
>
>* Ask if there's a bug in Perl, usually in the form of a subject line like
>"Bug in Perl?!?!?!?"
>
>* Ask a question that makes it clear that you have done exactly zero
>research and reading on your own. Prime example: "Can Perl find a string
>in another string?"
>
>* Ask a question that makes it clear that you're pretending to be a
>programmer. Prime example: "I'm trying to write an online game. Can Perl
>do this?"
>
>Grumpily,
>xoxo,
>Andy
>
>--
>--
>Andy Lester: <andy@petdance.com> http://www.petdance.com/andy/
>Chicago Shows: <shows@chicagomusic.com> http://www.ChicagoMusic.com/
>
------------------------------
Date: Fri, 02 Oct 1998 17:14:41 GMT
From: Elaine -HappyFunBall- Ashton <eashton@bbnplanet.com>
Subject: Re: Messages I never read
Message-Id: <3615078C.440ED0D3@bbnplanet.com>
Andy Lester wrote:
> For all the novices out there, here are some sure ways to get the more
> knowledgeable of us to ignore your message:
Yeah, just put 'Hot Sex Here' in the title and I'm sure it will get read
first :) *mwwahaahaa*
/me just trying to lighten a friday afternoon :)
e.
Would I live my life over again?
Make the same unforgivable mistakes?
Yes, given half a chance. Yes -R. Carver-
------------------------------
Date: Fri, 02 Oct 1998 12:58:41 -0400
From: John Porter <jdporter@min.net>
Subject: Re: per
Message-Id: <36150641.12411DDA@min.net>
Ian Lowe wrote:
>
> I have just compiled perl 5.005_02 for the Solaris platform and
> everything seems to be working o.k. except for one thing. I have a
> couple of scripts that look for filesystem size and capacity, so they
> are using `df`. The scripts still operate properly, but they return the
> error message : df: cannot statvfs /code: No such file or directory
Not a perl problem.
If df can't statvfs /code, then that's because there is no /code.
Sounds like you brought these scripts over from another machine
where there was a /code. I would err on the side of safety and
assume that because these scripts want to look at a directory
named /code, then they should NOT be run on your current machine.
Maybe you can tell by looking at the perl code; maybe there's
another directory which might be appropriate; or maybe you can
mount that /code from the machine where it lives.
--
John "Many Jars" Porter
baby mother hospital scissors creature judgment butcher engineer
------------------------------
Date: Fri, 2 Oct 1998 10:42:31 -0700
From: "Matt Bieber" <mattdb@syntrillium.com>
Subject: Performance issue with not reassigning referenced arguments passed to a function?
Message-Id: <6v33cj$7mi$1@nnrp03.primenet.com>
Can anyone comment on weather there are any performance issues if you do not
bother reassigning referenced arguments passed to a function?
For example, I have several functions that are passed references to multiple
hashes (\%hash1, \%hash2, etc.), where none of the hashes or hash values
need to be changed or added to, only output or otherwise referred to. In
these functions right now I am simply referring to the reference whenever I
need the value, as in:
print $$hash1{'value1'};
Usually what I see in others scripts' is that the reference is assigned to a
local variable at the top of the function:
%myhash = %$hash1;
and then used. Does anyone have any thoughts on if there's a performance or
other issue to not reassigning? The hashes I'm using are not that big
really, about 20 pairs, and at most I'm passing 3 of these to a function. By
the way, these functions exist inside a module if that makes any difference.
Thanks for any info!
I'm using: NT 4/Perl 5.003
Matt
------------------------------
Date: Fri, 02 Oct 1998 17:29:48 GMT
From: Elaine -HappyFunBall- Ashton <eashton@bbnplanet.com>
Subject: Perl as solution?
Message-Id: <36150B17.F0938BD3@bbnplanet.com>
My neighbor has one of those darned car alarms that goes off every
morning at 3am with that 'dive dive dive' sound.
For hours.
I would like to solve this annoying problem. Could perl help me develop
a guidance system for the missile I would like to launch at it? Or
should I just rely on my trusty tesla coil? Karo syrup in the oil? So
many choices, so little sleep. Maybe I could stick one of those magnetic
perl kits all over the hood?
e.
Would I live my life over again?
Make the same unforgivable mistakes?
Yes, given half a chance. Yes -R. Carver-
------------------------------
Date: 2 Oct 1998 17:07:54 GMT
From: abigail@fnx.com (Abigail)
Subject: Re: send geroge reese (was Re: Call for Participation: Python Conference)
Message-Id: <6v319a$m4s$1@client3.news.psi.net>
Jonathan Biggar (jon@floorboard.com) wrote on MDCCCLVIII September
MCMXCIII in <URL:news:361435F6.702C6EDD@floorboard.com>:
++
++ Not to brag :-), but I am partly to blame for Perl 5, by pushing Larry
++ to add the OO stuff.
So, it's you who I need to club over his head because of the ugly
mess Perl OO is? ;)
Luckely, there's a cleaner way: closures. With (multiple) inheritance and
autoloading.
package OO::Closures;
################################################################################
#
# $Author: abigail $
#
# $Date: 1998/10/01 22:54:57 $
#
# $Id: Closures.pm,v 1.1 1998/10/01 22:54:57 abigail Exp abigail $
#
# $Log: Closures.pm,v $
# Revision 1.1 1998/10/01 22:54:57 abigail
# Initial revision
#
#
#
################################################################################
use vars qw /$VERSION/;
($VERSION) = '$Revision: 1.1 $' =~ /(\d+.\d+)/;
sub import {
my $my_package = __PACKAGE__;
my $foreign_package = caller;
my $my_sub = 'create_object';
shift;
my $foreign_sub = @_ ? shift : $my_sub;
no strict 'refs';
*{"${foreign_package}::$foreign_sub"} = \&{"${my_package}::$my_sub"};
}
sub croak {
require Carp;
goto &Carp::croak;
}
sub create_object {
my ($methods, $ISA, $is_base) = @_;
sub {
my ($method, @args) = @_;
my ($call_super, $class);
if ($method =~ /^([^:]+|:[^:])*::(.*)/s) {
$class = $1;
$method = $2;
}
return $methods -> {$method} -> (@args) if exists
$methods -> {$method} && !defined $class;
my @supers;
if (defined $class && ($class ne 'SUPER' || exists $ISA -> {$class})) {
unless (exists $ISA -> {$class}) {
croak "No class ($class) found\n";
}
@supers = ($ISA -> {$class});
}
else {@supers = values %$ISA}
foreach my $super (@supers) {
if (wantarray) {
my @tmp;
eval {@tmp = $super -> ($method => @args)};
return @tmp unless $@;
}
elsif (defined wantarray) {
my $tmp;
eval {$tmp = $super -> ($method => @args)};
return $tmp unless $@;
}
else {
eval {$super -> ($method => @args)};
return unless $@;
}
croak $@ unless $@ =~ /No such method/;
}
unless ($is_base) {
# This is the base class. So, we'll look for AUTOLOAD.
return $methods -> {AUTOLOAD} -> ($method => @args) if exists
$methods -> {AUTOLOAD} && !defined $class;
my @supers;
if (defined $class &&
($class ne 'SUPER' || exists $ISA -> {$class})) {
@supers = ($ISA -> {$class})
}
else {@supers = values %$ISA}
# Check %ISA for AUTOLOAD.
foreach my $super (@supers) {
if (wantarray) {
my @tmp;
eval {@tmp = $super -> (AUTOLOAD => $method, @args)};
return @tmp unless $@;
}
elsif (defined wantarray) {
my $tmp;
eval {$tmp = $super -> (AUTOLOAD => $method, @args)};
return $tmp unless $@;
}
else {
eval {$super -> ($method => @args)};
return unless $@;
}
croak $@ unless $@ =~ /No such method/;
}
}
croak "No such method ($method) found";
}
}
1;
__END__
=pod
=head1 NAME
OO::Closures - Object Oriented Programming using Closures.
=head1 SYNOPSIS
use OO::Closures;
sub new {
my (%methods, %ISA, $self);
$self = create_object (\%methods, \%ISA, !@_);
...
$self;
}
=head1 DESCRIPTION
This package gives you a way to use Object Oriented programming using
Closures, including multiple inheritance, C<SUPER::> and autoloading.
To create the object, call the function C<create_object> with three
arguments, a reference to a reference of a hash containing the method
of the object, a reference to an array containing the inherited
objects, and a flag determining whether the just created object is
the base object or not. This latter flag is important when it comes
to trying C<AUTOLOAD> after not finding a method.
C<create_object> returns a closure which will act as the new object.
Here is an example of the usage:
use OO::Closures;
sub dice {
my (%methods, %ISA, $self);
$self = make_object (\%methods, \%ISA, !@_);
my $faces = 6;
$methods -> {set} = sub {$faces = shift;};
$methods -> {roll} = sub {1 + int rand $faces};
$self;
}
It is a simple object representing a die, with 2 methods, C<set>, to set
the number of faces, and C<roll>, to roll the die. It does not inherit
anything. To make a roll on a 10 sided die, use:
(my $die = dice) -> (set => 10);
print $die -> ('roll');
Note that since the objects are closures, method names are the first
arguments of the calls.
=head1 INHERITANCE
To use inheritance, we need to set the C<@ISA> array. We also need to
pass ourselves to the classes we inherited, so an inherited class can
find the base object. (This is similar to the first argument of the
constructor when using regular objects).
Here is an example that implements multi dice, by subclassing C<dice>.
We will also give C<dice> a method C<print_faces> that prints the number
of faces and returns the object.
use OO::Closures;
sub dice {
my (%methods, %ISA, $self);
$self = create_object (\%methods, \%ISA, !@_);
my $this_object = shift || $self;
my $faces = 6;
$methods {set} = sub {$faces = shift};
$methods {roll} = sub {1 + int rand $faces};
$methods {print_faces} = sub {print "Faces: $faces\n"; $this_object};
$self;
}
sub multi_dice {
my (%methods, %ISA, $self);
$self = create_object (\%methods, \%ISA, !@_);
my $this_object = shift || $self;
%ISA = (dice => dice $this_object);
my $amount = 1;
$methods {amount} = sub {$amount = shift};
$methods {roll} = sub {
my $sum = 0;
foreach (1 .. $amount) {$sum += $self -> ('dice::roll')}
$sum;
};
$self;
}
my $die = multi_dice;
$die -> (set => 7);
$die -> (amount => 4);
print $die -> ('print_faces') -> ('roll'), "\n";
__END__
Notice the line C<my $this_object = shift || $self;>. That will make
C<$this_object> contain the base object, unlike C<$self> which is the
instance of the current class.
The class C<dice> is subclassed in C<multi_dice> by calling C<dice>
with an extra argument, the base object. Now it's known that C<dice>
is subclassed, and looking for C<AUTOLOAD> if it cannot find the
requested method should not happen; that will be triggered by the
base object.
Inherited classes are named, but they are named by the inheriter,
not the inheritee. This allows you to inherit the same class multiple
times, and getting separate data and method space for it.
When searching for methods in the inheritance tree, no order will be
garanteed. If you subclass multiple classes defining the methods with
the same name, it's better to mask those methods and explicitely
redirect the call to the class you want it to handle.
You can call a method by prepending its class name(s); just like
regular objects.
Inherited classes are stored in the C<%ISA> hash, but since this variable
is private to the object, each object can have its own inheritance
structure. If you change a class, existing objects of the class will not
be modified.
The pseudo class 'SUPER::' works the same way as regular objects do,
except that it works the right way. It will resolve 'SUPER::' depending
on the inherited classes of the object the method is called in; not
on the C<@ISA> of the package the call is made from.
=head1 C<use OO::Closures;>
By default, the module C<OO::Closures> exports the function C<create_object>.
If you want this function to be known by another name, give that name
as an argument to the C<use> statement.
use OO::Closure 'other_name';
Now you create objects with C<other_name (\%methods, \%ISA, !@_);>
=head1 BUGS
This documentation uses the word 'class' in cases where it's not really
a class in the sense of the usual object oriented way.
=head1 HISTORY
$Log: Closures.pm,v $
Revision 1.1 1998/10/01 22:54:57 abigail
Initial revision
=head1 AUTHOR
This package was written by Abigail.
=head1 COPYRIGHT
This package is distributed under the Artistic License.
=cut
--
perl -MTime::JulianDay -lwe'@r=reverse(M=>(0)x99=>CM=>(0)x399=>D=>(0)x99=>CD=>(
0)x299=>C=>(0)x9=>XC=>(0)x39=>L=>(0)x9=>XL=>(0)x29=>X=>IX=>0=>0=>0=>V=>IV=>0=>0
=>I=>$r=-2449231+gm_julian_day+time);do{until($r<$#r){$_.=$r[$#r];$r-=$#r}for(;
!$r[--$#r];){}}while$r;$,="\x20";print+$_=>September=>MCMXCIII=>()'
------------------------------
Date: Fri, 2 Oct 1998 10:02:45 -0700
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: string from array
Message-Id: <MPG.107ea712d49e5a6c9897d3@nntp.hpl.hp.com>
[Posted to comp.lang.perl.misc and a copy mailed.]
In article <6v2urj$237$1@earth.superlink.net> on Fri, 2 Oct 1998
12:26:27 EDT, David A. Black <dblack@saturn.superlink.net> says...
...
!> lr@hpl.hp.com (Larry Rosler) writes:
...
!> > { local $" = ""; $string = "@ary" }
!>
!> >and then we have some quiet also.
!>
!>
!> orpheus:~/perl$ perl -v
!>
!> This is perl, version 5.005_02 built for i586-linux
!>
!> [snip]
!>
!> orpheus:~/perl$ perl -we '{local $"}'
!> orpheus:~/perl$
!>
!>
!> Sounds pretty quiet to me :-)
Gosh. The '-w' flag doesn't complain about the *declaration* of a
variable with an undefined value (God forbid it should!). It complains
only when you *use* such a variable. And '$string = "@ary"' uses $".
As Larry Wall pointed out, '"@ary" is translated to join($", @ary)
internally.'.
Use of unitialized value at -e line 1.
See -- 'Use'. :-)
--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: Fri, 02 Oct 1998 12:45:06 -0400
From: Michel Prevost <michel.prevost@cactuscom.ca_REMOVE_TO_MAIL>
To: Wong Kin Sang <wongksp@netvigator.com>
Subject: Re: What is the maining of "Bare word"
Message-Id: <36150312.EB12F774@cactuscom.ca_REMOVE_TO_MAIL>
A bare word is a word, that is not a keyword, unquoted
Wong Kin Sang wrote:
> Hi all of you,
>
> Recently I've got a question about wirting perl script. When I try to
> run a perl script, an error " Bare word found where operatior
> expected" has appear. What should I do/check for this ? (I've check
> that I haven't miss { ; " or wrong spelling.
>
> Thanks.
>
> Patrick
------------------------------
Date: 12 Jul 98 21:33:47 GMT (Last modified)
From: Perl-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Special: Digest Administrivia (Last modified: 12 Mar 98)
Message-Id: <null>
Administrivia:
Special notice: in a few days, the new group comp.lang.perl.moderated
should be formed. I would rather not support two different groups, and I
know of no other plans to create a digested moderated group. This leaves
me with two options: 1) keep on with this group 2) change to the
moderated one.
If you have opinions on this, send them to
perl-users-request@ruby.oce.orst.edu.
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.
To submit articles to comp.lang.perl.misc (and this Digest), send your
article to perl-users@ruby.oce.orst.edu.
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.
The Meta-FAQ, an article containing information about the FAQ, is
available by requesting "send perl-users meta-faq". The real FAQ, as it
appeared last in the newsgroup, can be retrieved with the request "send
perl-users FAQ". Due to their sizes, neither the Meta-FAQ nor the FAQ
are included in the digest.
The "mini-FAQ", which is an updated version of the Meta-FAQ, is
available by requesting "send perl-users mini-faq". It appears twice
weekly in the group, but is not distributed in the digest.
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 V8 Issue 3875
**************************************