[17882] in Perl-Users-Digest
Perl-Users Digest, Issue: 42 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Jan 11 18:10:46 2001
Date: Thu, 11 Jan 2001 15:10:24 -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: <979254623-v10-i42@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Thu, 11 Jan 2001 Volume: 10 Number: 42
Today's topics:
Re: Problem w/ hash of references to hashes (or maybe s <uri@sysarch.com>
Re: regex help, please <elijah@workspot.net>
Re: regex help, please <bart.lateur@skynet.be>
Returning a filename from FILEHANDLE <president@webticker.com>
Re: Returning a filename from FILEHANDLE <president@webticker.com>
setsockopt (Paul Falbe)
Simple project zatlas@juno.com
Re: Simple project (Jerome O'Neil)
Re: Simple project rbfitzpa@my-deja.com
Re: Simple project gungeek@my-deja.com
Re: Simple project zatlas@juno.com
Re: Simple project zatlas@juno.com
Re: Simple project zatlas@juno.com
Re: Simple project (Jerome O'Neil)
Re: socket send to client problem <uri@sysarch.com>
Re: strange end-of-line <mischief@velma.motion.net>
Re: system() method won't change directories! (Richard J. Rauenzahn)
Re: Want to create class/sub-class with no package <mjcarman@home.com>
Re: Want to create class/sub-class with no package egwong@netcom.com
XS DLL Wrapper Code: Please Review <tjones@computer.org>
Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Thu, 11 Jan 2001 20:20:30 GMT
From: Uri Guttman <uri@sysarch.com>
Subject: Re: Problem w/ hash of references to hashes (or maybe scoping?)
Message-Id: <x78zoh4z8x.fsf@home.sysarch.com>
>>>>> "WC" == Weston Cann <iowa88_song88.remove_eights@hotmail.com> writes:
WC> In article <x74rz75n58.fsf@home.sysarch.com>, Uri Guttman
WC> <uri@sysarch.com> wrote:
>> you are using the pink camel? burn it or put it in a museum. always use
>> the online docs for a reference anyway. read perlsub and learn about &,
>> why it is not needed in basic sub calling and why is still is around
>> (for a few special cases).
WC> Well, _using_ isn't quite accurate. I actually don't crack it open
WC> much, and usually do use the online docs for things I don't
WC> already think I understand. But I did use it as my intro to Perl
WC> back in 1996. It was probably old even then, but I was poor, and
WC> that's the book that my co-worker had. He then left the country
WC> for two years as an LDS missionary, and left the book. I really
WC> should give it back. I have some irrational fondness for it,
WC> though.
keep it as a collector's item (i am keeping my copy). but get the 3rd
edition which is very good. the 2nd edition came out in fall '96 so you
could have gotten it then. the biggest problem with the pink camel is
that so much of perl5 was not in perl4 that you miss out by using that
book.
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: 11 Jan 2001 19:05:10 GMT
From: Eli the Bearded <elijah@workspot.net>
Subject: Re: regex help, please
Message-Id: <eli$0101111401@qz.little-neck.ny.us>
In comp.lang.perl.misc, Tad McClellan <tadmc@augustmail.com> wrote:
> Bernard El-Hagin <bernard.el-hagin@lido-tech.net> wrote:
> >>print "Yipee" if /[a-z\-A-Z_0-9 .#\/%\(\)]{1,32}/;
> Let's just make it as pretty as we can then:
> m([-\w .#/%()]{1,32}) # look Ma, no backslashes!
\w is not necessarily the same as [a-zA-Z0-9_]
perlre from 5.6.0:
A "\w" matches a single alphanumeric character, not a
whole word. Use "\w+" to match a string of Perl-
identifier characters (which isn't the same as matching an
English word). If "use locale" is in effect, the list of
alphabetic characters generated by "\w" is taken from the
current locale. See the perllocale manpage.
Elijah
------
also notes the original poster appeared to have a space at the end of the RE
------------------------------
Date: Thu, 11 Jan 2001 19:31:50 GMT
From: Bart Lateur <bart.lateur@skynet.be>
Subject: Re: regex help, please
Message-Id: <tf2s5t891bj1advdt6o0b69ii5et5hsmh8@4ax.com>
Stan Brown wrote:
>I have a regexy that looks like this:
>
>'[a-z\-A-Z_0-9 .#/%\(\)]{1,32} '
>
>I am using this a the pattern for Parse::Lex to scan for. I intend for it
>to accept ALL of the folowing:
>Any Alph character (upper or lower case),
>Any number,
>The folowing non aplhanumeric charcaters .#/()%
Then why are you including the minus sign and underscore as well?
Sorry to be not more helpful, but I have no experience with Parse::Lex.
--
Bart.
------------------------------
Date: Thu, 11 Jan 2001 14:44:36 -0500
From: Julian Cook/Sherab Gyatso <president@webticker.com>
Subject: Returning a filename from FILEHANDLE
Message-Id: <3A5E0D23.E70B3169@webticker.com>
Hi folks!
My thanks in advance to all who make this a great place to
be. I have certainly gotten more than my fair share of free advice.
Is there a function is PERL that will return what a filehandle is
associated with?
Where <FILES> refers to "files.txt" Could I somehow ask <FILES>
to give me what it's reading?
Many thanks again.
--
Julian Cook
President
WebTicker News Syndicate
president@webticker.com
----------------------------------------------------------------------
7,000,000 monthly news deliveries, a network of 20,000 sites,
16 channels of information, one news leader,assisting numerous
charities...WebTicker News Syndicate
http://www.webticker.com
----------------------------------------------------------------------
------------------------------
Date: Thu, 11 Jan 2001 15:06:17 -0500
From: Julian Cook/Sherab Gyatso <president@webticker.com>
Subject: Re: Returning a filename from FILEHANDLE
Message-Id: <3A5E1238.3D39CE34@webticker.com>
I found the answer thanks to Deja.com. For anyone else using deja who may
have this same
question.....
The answer is no.
----------------------------------------------
Julian Cook/Sherab Gyatso wrote:
> Hi folks!
> My thanks in advance to all who make this a great place to
> be. I have certainly gotten more than my fair share of free advice.
>
> Is there a function is PERL that will return what a filehandle is
> associated with?
>
> Where <FILES> refers to "files.txt" Could I somehow ask <FILES>
> to give me what it's reading?
>
> Many thanks again.
> --
> Julian Cook
> President
> WebTicker News Syndicate
> president@webticker.com
> ----------------------------------------------------------------------
> 7,000,000 monthly news deliveries, a network of 20,000 sites,
> 16 channels of information, one news leader,assisting numerous
> charities...WebTicker News Syndicate
> http://www.webticker.com
> ----------------------------------------------------------------------
--
Julian Cook
President
WebTicker News Syndicate
president@webticker.com
202-761-0036 - Capitol Hill Office
----------------------------------------------------------------------
6,000,000 monthly news deliveries, a network of 16,000 sites,
16 channels of information, one news leader,assisting numerous
charities...WebTicker News Syndicate
http://www.webticker.com
----------------------------------------------------------------------
------------------------------
Date: 11 Jan 01 20:30:36 GMT
From: paul@cassens.com (Paul Falbe)
Subject: setsockopt
Message-Id: <19937.979245036@cassens.com>
Keywords: socket
I am wanting to to send some information via a socket using Perl. However
I am wanting to interface eth1 under linux. I know in C you would
use the setsockopt function. I know Perl has this function also. In looking
through the manual I see little documentation. Has anyone there done this?
example c code
int skfd;
struct ifreq interface;
skfd = socket(AF_INET, SOCK_DGRAM, 0);
strncpy(interface.ifr_ifrn.ifrn_name, "eth1", IFNAMSIZ);
if (setsockopt(skfd, SOL_SOCKET, SO_BINDTODEVICE, (char *)&interface,
sizeof(interface)) < 0) {
perror("sendpacket: setting SO_BINDTODEVICE");
exit(1);
}
Thanks for any help!
-Paul
--
Internet: | Paul J. Falbe |
falbe@cassens.com | Cassens Transport | Std disclaimers apply.
Voice: | 145 N. Kansas Str. | (But you knew that!)
618-656-3006 | Edwardsville, IL 62025 |
------------------------------
Date: Thu, 11 Jan 2001 19:55:18 GMT
From: zatlas@juno.com
Subject: Simple project
Message-Id: <93l330$ltq$1@nnrp1.deja.com>
Hi everybody I do not know Perl at all but was told this is the way to go
with my little project: (I did install Perl on my WinNT workstation) I need
to read all text files in a subdirectory and find:
1. How many of these files contain any one of these three combinations: a.
the word BIND followed immediately by the word MAP or b. the word CHANGE
followed immediately by the word PRIORITY or c. the word CHECK followed
immediately by the word TERMINAL
2. How many appearances of these patterns are on each of these files
3. What are the names of these files
I know how to program in Visual Basic and I've just finished basic course in
Java. I could program the thing in VB but it seems to be too big of a
project and I need it ASAP.
Could anybody please guide me where to look.
I thank all of you in advance
--
ZAConsaltants, the Performance Experts
Sent via Deja.com
http://www.deja.com/
------------------------------
Date: Thu, 11 Jan 2001 20:15:43 GMT
From: jerome@activeindexing.com (Jerome O'Neil)
Subject: Re: Simple project
Message-Id: <Pvo76.1456$dy.302789@news.uswest.net>
zatlas@juno.com elucidates:
> Hi everybody I do not know Perl at all but was told this is the way to go
> with my little project: (I did install Perl on my WinNT workstation) I need
> to read all text files in a subdirectory and find:
Perl is *a* way to go. But for the task you are attempting, basic unix tools
like grep would more than likely be easier and faster. I believe MS
distributes a unix tools package for Windows boxes.
HTH
--
If men could learn from history, what lessons it might teach us! But
passion and party blind our eyes, and the light which experience gives
is a lantern on the stern, which shines only on the waves behind us.
--Samuel Taylor Coleridge, "Recollections"
------------------------------
Date: Thu, 11 Jan 2001 20:23:29 GMT
From: rbfitzpa@my-deja.com
Subject: Re: Simple project
Message-Id: <93l4nk$ni9$1@nnrp1.deja.com>
Use your imagination. If you write it down it's easy:
1. List all the files in the dir
2. Parse each file for 'BIND MAP', 'CHANGE PRIORITY', 'CHECK TERMINAL'
3. Each time you find an occurance, increment a counter
4. At the end print out the counter totals
example:
$CP=0;
$XY=0;
fils='/dir/where/files/are'
foreach fil (@fils)
{
open (tmp,"<$fil")
while (<tmp>)
{
$line=$_;
if ($line =~ "CHANGE PRIORITY")
{
$CP++;
}
if ($line =~ "XYZ")
{
$XY++;
}
}
close (tmp);
}
print "CHANGE PRIORITY OCCURRENCES: $CP\n";
*** I PURPOSELY DID NOT FINISH THE SCRIPT - ITS UP TO YOU
*** TO FIGURE IT OUT
In article <93l330$ltq$1@nnrp1.deja.com>,
zatlas@juno.com wrote:
> Hi everybody I do not know Perl at all but was told this is the way to
go
> with my little project: (I did install Perl on my WinNT workstation) I
need
> to read all text files in a subdirectory and find:
>
> 1. How many of these files contain any one of these three
combinations: a.
> the word BIND followed immediately by the word MAP or b. the word
CHANGE
> followed immediately by the word PRIORITY or c. the word CHECK
followed
> immediately by the word TERMINAL
>
> 2. How many appearances of these patterns are on each of these files
>
> 3. What are the names of these files
>
> I know how to program in Visual Basic and I've just finished basic
course in
> Java. I could program the thing in VB but it seems to be too big of a
> project and I need it ASAP.
>
> Could anybody please guide me where to look.
>
> I thank all of you in advance
>
> --
> ZAConsaltants, the Performance Experts
>
> Sent via Deja.com
> http://www.deja.com/
>
Sent via Deja.com
http://www.deja.com/
------------------------------
Date: Thu, 11 Jan 2001 20:33:06 GMT
From: gungeek@my-deja.com
Subject: Re: Simple project
Message-Id: <93l5a2$o6l$1@nnrp1.deja.com>
In article <93l330$ltq$1@nnrp1.deja.com>,
zatlas@juno.com wrote:
> Hi everybody I do not know Perl at all but was told this is the way
to go
> with my little project: (I did install Perl on my WinNT workstation)
I need
> to read all text files in a subdirectory and find:
>
> 1. How many of these files contain any one of these three
combinations: a.
> the word BIND followed immediately by the word MAP or b. the word
CHANGE
> followed immediately by the word PRIORITY or c. the word CHECK
followed
> immediately by the word TERMINAL
>
> 2. How many appearances of these patterns are on each of these files
>
> 3. What are the names of these files
>
> I know how to program in Visual Basic and I've just finished basic
course in
> Java. I could program the thing in VB but it seems to be too big of a
> project and I need it ASAP.
>
> Could anybody please guide me where to look.
You might want to start by learning Perl. Surprisingly, there is a
good book that is called "Learning Perl." If you're familiar with
programming, you could probably learn enough to whip out a script
tonight. Then, if you have problems, post your code and the problem
description to this group. You won't have much luck with trying to get
people to write your code for you, though.
Sent via Deja.com
http://www.deja.com/
------------------------------
Date: Thu, 11 Jan 2001 20:59:36 GMT
From: zatlas@juno.com
Subject: Re: Simple project
Message-Id: <93l6rj$ps1$1@nnrp1.deja.com>
In article <93l5a2$o6l$1@nnrp1.deja.com>,
gungeek@my-deja.com wrote:
> You might want to start by learning Perl. Surprisingly, there is a
> good book that is called "Learning Perl." If you're familiar with
> programming, you could probably learn enough to whip out a script
> tonight. Then, if you have problems, post your code and the problem
> description to this group. You won't have much luck with trying to get
> people to write your code for you, though.
I am in the process but it is so much different than what I know. However
the other guy gave me a template and I'll elaborate on it. I expect to know
Perl reasonabley well within couple of weeks. Thanks -- ZAConsaltants, the
Performance Experts
Sent via Deja.com
http://www.deja.com/
------------------------------
Date: Thu, 11 Jan 2001 21:01:52 GMT
From: zatlas@juno.com
Subject: Re: Simple project
Message-Id: <93l6vo$pvo$1@nnrp1.deja.com>
In article <93l4nk$ni9$1@nnrp1.deja.com>,
rbfitzpa@my-deja.com wrote:
> Use your imagination. If you write it down it's easy:
>
> 1. List all the files in the dir
> 2. Parse each file for 'BIND MAP', 'CHANGE PRIORITY', 'CHECK TERMINAL'
> 3. Each time you find an occurance, increment a counter
> 4. At the end print out the counter totals
>
> example:
>
> $CP=0;
> $XY=0;
> fils='/dir/where/files/are'
> foreach fil (@fils)
> {
Thanks This is exactly the boost I need. I expect to know it reasonable well
within couple of weeks
--
ZAConsaltants, the Performance Experts
Sent via Deja.com
http://www.deja.com/
------------------------------
Date: Thu, 11 Jan 2001 21:08:59 GMT
From: zatlas@juno.com
Subject: Re: Simple project
Message-Id: <93l7d2$qer$1@nnrp1.deja.com>
In article <Pvo76.1456$dy.302789@news.uswest.net>,
jerome.oneil@360.com wrote:
> Perl is *a* way to go. But for the task you are attempting, basic unix tools
> like grep would more than likely be easier and faster. I believe MS
> distributes a unix tools package for Windows boxes.
You'll be surprised to learn how poor is the ordinary Windows environment in
tools like Grep, etc. There are some utilities readily available but they do
not have the rich syntax you find in Unix scripts.
--
ZAConsaltants, the Performance Experts
Sent via Deja.com
http://www.deja.com/
------------------------------
Date: Thu, 11 Jan 2001 22:08:22 GMT
From: jerome@activeindexing.com (Jerome O'Neil)
Subject: Re: Simple project
Message-Id: <q9q76.2024$dy.362948@news.uswest.net>
zatlas@juno.com elucidates:
> In article <Pvo76.1456$dy.302789@news.uswest.net>,
> jerome.oneil@360.com wrote:
>> Perl is *a* way to go. But for the task you are attempting, basic unix tools
>> like grep would more than likely be easier and faster. I believe MS
>> distributes a unix tools package for Windows boxes.
>
> You'll be surprised to learn how poor is the ordinary Windows environment in
> tools like Grep, etc.
I believe you, but it doesn't surprise me. :)
--
If men could learn from history, what lessons it might teach us! But
passion and party blind our eyes, and the light which experience gives
is a lantern on the stern, which shines only on the waves behind us.
--Samuel Taylor Coleridge, "Recollections"
------------------------------
Date: Thu, 11 Jan 2001 20:29:33 GMT
From: Uri Guttman <uri@sysarch.com>
Subject: Re: socket send to client problem
Message-Id: <x766jl4ytu.fsf@home.sysarch.com>
>>>>> "GW" == Garry Williams <garry@zvolve.com> writes:
GW> On 11 Jan 2001 09:52:19 GMT, Anno Siegel
GW> <anno4000@lublin.zrz.tu-berlin.de> wrote:
>>
>> Have you tried to check with select() (four-argument form)? It should
>> tell you if a handle is ready to accept data.
GW> Won't help.
GW> As Uri has pointed out before in this group, the socket is available
GW> only by finding out if it can be *read*. Besides, even a reliable
GW> check will lead to a race condition. Bad design. An easy fix would
GW> be to handle the "program crashes out" condition -- whatever that
GW> means. Or maybe the OP should just fork.
i am confused by the OP's problem and your comments. to test whether a
socket got connected (especially using an async connect), you test for
writeability, you don't have to write anything to it. to test for a
socket closing on you, you test for readability and if you read 0 bytes,
it closed. this assumes non-blocking reads which you should always use
on sockets.
as for how to test the socket, you can use 4 arg select (low level and
tricky), IO::Select (simple module interface but you still have to do
work), Event.pm or POE (event loop based systems with callbacks) and
other ways.
i much prefer the event loop stuff as it makes writing any client/server
system so much easier. but some folks can't wrap their brains around
events.
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: Thu, 11 Jan 2001 20:07:24 -0000
From: Chris Stith <mischief@velma.motion.net>
Subject: Re: strange end-of-line
Message-Id: <t5s4jssp4jj18c@corp.supernews.com>
Drew Krause <drkrause@mindspring.com> wrote:
> Anno Siegel wrote:
>> Drew Krause <drkrause@mindspring.com> wrote in comp.lang.perl.misc:
>> >Hello, I think this is related to using perl, so I'll ask. I have some
>> >text files that don't copy well into my unix system.
>>
>> From what other system, copied by what means? Please be specific.
> Copied from CD, which looks like it's meant to be readable on many platforms.
Probably ISO9660. Yes, ISO means more than the extension on burned
copies of CDs. It's the International Standards Organization, and
stndard #9660 refers to a standard for data CDs that should be
supported on all platforms.
>>
>>
>> > They look fine --
>> >no strange characters -- but the spaces and blank lines generate errors
>> >when read.
>>
>> What errors do they generate when read by what program? Please be
>> specific.
> I get "illegal character" flags from running csound (a sound synthesis
> program.
I'm sure you know what that means, based on what you write below.
>> Yes, there are different kinds of end-of-line markers in use on different
>> systems. Many people have heard of the problem, most of which actually
>> got bitten by it.
> In fact, a "^M" appears at the end of each line when I try "vi -b filename".
This mean you're opening a Windows file on a Unix system.
There are many messages about this archived on Deja recently.
>>If necessary, the conversion can also be done in Perl, and isn't hard
>>to do.
> Do tell!
Perl is a language with many strengths and a few major weaknesses.
Handling character substitution, including control characters,
happens to be one of its best strengths. The weaknesses, by the way,
don't matter too much since Perl has XS, Inline.pm, and has as
another of its greatest strengths being an excellent glue language.
Chris
--
Christopher E. Stith
"Get real! This is a discussion group, not a helpdesk. You post
something, we discuss its implications. If the discussion happens to
answer a question you've asked, that's incidental." -- nobull in clpm
------------------------------
Date: 11 Jan 2001 19:06:32 GMT
From: nospam@hairball.cup.hp.com (Richard J. Rauenzahn)
Subject: Re: system() method won't change directories!
Message-Id: <979239990.990436@hpvablab.cup.hp.com>
2obvious@my-deja.com writes:
>A chdir() call would take care of the first line, but I don't know of
>any way to start the second without resorting to a system() call. And
>that won't work--thanks to Wyzelli, Tony, and RH I now know that each
>system() call creates its own environment.
system() also inherits the parent environment. The problem with
chdir'ing within a system() call is that the parent environment doesn't
inherit from the child.
$ perl5
chdir("/tmp") || die "$!";
system("pwd");
chdir("/") || die "$!";
system("pwd");
__END__
/tmp
/
Rich
--
Rich Rauenzahn ----------+xrrauenza@cup.hp.comx+ Hewlett-Packard Company
Technical Consultant | I speak for me, | 19055 Pruneridge Ave.
Development Alliances Lab| *not* HP | MS 46TU2
ESPD / E-Serv. Partner Division +--------------+---- Cupertino, CA 95014
------------------------------
Date: Thu, 11 Jan 2001 15:46:05 -0600
From: Michael Carman <mjcarman@home.com>
Subject: Re: Want to create class/sub-class with no package
Message-Id: <3A5E299D.7823BBC4@home.com>
bpettit@my-deja.com wrote:
>
> I want to create a class that has subclasses. From everything
> I've read, all classes have to be in a package file. So, if
> I wanted something like a class called "Main" and then a subclass
> called "Shape" and then a subclass called "Square", I'd need to
> have 3 separate package files.
You're a little off the mark, there. A class *is* a package, so you
can't have the former without the latter. While packages are often
contained in their own file, they don't have to be. A package is just a
namespace. A single file can contain multiple packages.
> What I want to do is fairly simple.. I don't even need any methods.
Then do you really need a class? Would a hash do what you want?
[snip of sample code]
That doesn't look quite right. For classes, take a look at the perlobj
and perltoot manpages. For data structures, look at the perldsc manpage,
and also possibly perlref, perlreftut, and perllol.
-mjc
------------------------------
Date: Thu, 11 Jan 2001 22:53:03 GMT
From: egwong@netcom.com
Subject: Re: Want to create class/sub-class with no package
Message-Id: <jPq76.2448$J%.263375@news.flash.net>
bpettit@my-deja.com wrote:
> Hello,
> I'm somewhat new to perl. I want to create a class that has
> subclasses. From everything I've read, all classes have to be in a
> package file. So, if I wanted something like a class called "Main" and
> then a subclass called "Shape" and then a subclass called "Square", I'd
> need to have 3 separate package files.
> What I want to do is fairly simple.. I don't even need any methods.
> I want my own datastructure that I can define to different things. But,
> I don't want to have multiple package files. Perhaps this example will
> explain what I want to do better (I'm thinking in terms of C++ and
> trying to use PERL syntax)
While each of your classes need to be in its own class, they needn't
be in different files. For example
#!/usr/bin/perl -w
package Square;
sub new {
my $proto = shift();
my $self = { created => 3, };
bless $self, ref($proto) || $proto;
}
package Shape;
sub new {
my $proto = shift();
my $self = { created => 2, sqr => Square->new(), };
bless $self, ref($proto) || $proto;
}
package MyObj;
sub new {
my $proto = shift();
my $self = { created => 1, shp => Shape->new(), };
bless $self, ref($proto) || $proto;
}
package main;
use strict;
my $newObject = MyObj->new();
print $newObject->{shp}->{sqr}->{created}, "\n";
looks like what you were trying to do. I'm not sure I'd call this
"subclassing" though, because all that's happening is that one class
creates an instance of another. To do real inheritance, do something like
#!/usr/bin/perl -w
package Square;
sub new {
# ...
}
sub init {
my $self = shift();
$self->{square_created} = 3;
foreach ( @ISA ) {
if (my $initializer = $_->can('init')) { $self->$initializer(); }
}
}
package Shape;
@ISA = qw / Square /;
sub new {
# ...
}
sub init {
my $self = shift();
$self->{shape_created} = 2;
foreach ( @ISA ) {
if (my $initializer = $_->can('init')) { $self->$initializer(); }
}
}
package MyObj;
@ISA = qw / Shape /;
sub new {
my $proto = shift();
my $self = {};
bless $self, ref($proto) || $proto;
$self->init();
return $self;
}
sub init {
my $self = shift();
$self->{myobj_created} = 1;
foreach ( @ISA ) {
if (my $initializer = $_->can('init')) { $self->$initializer(); }
}
}
package main;
use strict;
my $newObj = MyObj->new();
print map { "$_: $$newObj{$_}\n" } keys %$newObj;
where the child classes actually inherit all of the methods and attributes
of their parents. So here, each instance of MyObj is blessed and then
calls init() in itself and all of it's ancestors (in this example it
doesn't matter, for a more complicated heirarchy, you'll probably want
to make sure that no single instance calls init() in the same parent
class twice.)
While the perlobj and perltoot manpages are very good (you certainly
can't beat the price! :), what you really want is _Object Oriented Perl_
by Damian Conway (http://www.manning.com/conway/).
-eg
------------------------------
Date: Thu, 11 Jan 2001 12:12:40 -0800
From: "Thomas C. Jones" <tjones@computer.org>
Subject: XS DLL Wrapper Code: Please Review
Message-Id: <3A5E13B8.E1D63DB6@computer.org>
Hello,
I am not a programmer, but I play one on TV. I've created an XS wrapper
for the Corelis JTAG controller and I would like to get the trickiest
interface reviewed by the experts. So if you have a few moments, please
review the code below and give me any feedback at all.
The trickiest bit of the Corelis interface involves the scan operations,
exemplified by the scan_dr() call (with permission by Corelis,
http://www.corelis.com):
__declspec(dllexport) void _stdcall scan_dr
(enum TAPS test_bus, // Which TAP to Scan
const unsigned short int *out_data, // Holds data shifted out of
card
unsigned long bit_length, // Number of bits to be shifted
unsigned short int *in_data); // Data to be shifted into card
The declspec/stdcall stuff is just MS Windows dressing to avoid having
to use a .def file for DLL linkage. The unsigned short int pointers
point to arrays holding the scan data. This is where the complexity
arises from and here is how I coded the wrapper (with permission of my
employer):
## Sample call: $result = scan_dr($test_bus, \@out_data, $length,
\@in_data); # Verify $result == 0
## $test_bus - Which TAP to reset - enumerated type I'm treating as
int
## @out_data - Holds data shifted out of card
## $length - Number of bits to be shifted
## @in_data - Data to be shifted into card
short
jtag_scan_dr(test_bus, out_data_ref, length, in_data_ref)
int test_bus
SV* out_data_ref
unsigned long length
SV* in_data_ref
PREINIT:
AV* out_data;
AV* in_data;
unsigned long i, j;
unsigned short out_data_int[SCAN_DATA_ARRAY_SIZE];
unsigned short in_data_int[SCAN_DATA_ARRAY_SIZE];
SV* GPSVP; // General purpose
int ArrayOK; // Says Array is OK.
CODE:
// Sanity-check test_bus:
if( test_bus > MAX_TAPS ) {
printf( "ERROR: scan_dr: test_bus (%d) out of range!\n",
test_bus);
RETVAL = 1;
// Sanity-check array references:
} else if(!SvROK( out_data_ref)) {
printf("out_data not OK!\n");
RETVAL = 1;
} else if(!SvROK( in_data_ref)) {
printf("in_data_ref not OK!\n");
RETVAL = 1;
} else if( SvTYPE(SvRV(out_data_ref)) != SVt_PVAV) {
printf("out_data_ref not AV!\n");
RETVAL = 1;
} else if( SvTYPE(SvRV(in_data_ref)) != SVt_PVAV) {
printf("in_data_ref not AV!\n");
RETVAL = 1;
// Sanity-check length:
} else if( length > SCAN_DATA_WIDTH ) { // Change this value in
Jtag.h for new versions of the chip
printf( "ERROR: scan_dr: length=%d > SCAN_DATA_WIDTH=%d - not
possible\n",
length, SCAN_DATA_WIDTH );
// All sanity-checks PASS:
} else {
// Dereference arrays:
out_data = (AV *) SvRV(out_data_ref);
in_data = (AV *) SvRV(in_data_ref);
// Move Perl array data to local array:
ArrayOK = 1; // Assume OK
for(i=0; i<SCAN_DATA_ARRAY_SIZE; i++ ) {
GPSVP = *av_fetch( out_data, (I32) i, 0);
if(SvIOK(GPSVP)) {
out_data_int[i] = (unsigned short int) SvIV(GPSVP);
} else {
ArrayOK = 0;
}
}
// Call target function:
if( ArrayOK ) {
scan_dr((enum TAPS) test_bus, out_data_int, length,
in_data_int);
// Convert in_data back:
for(i=0; i<SCAN_DATA_ARRAY_SIZE; i++ ) {
av_store( in_data, (I32) i,
newSViv((IV)in_data_int[i]));
}
RETVAL = 0;
} else {
RETVAL = 1;
}
}
OUTPUT:
RETVAL
I would have preferred to use more straight-line code but I could not
find a way to exit at any point and preserver the RETVAL/OUTPUT
structure. Also, I'm troubled by the av_store( newSViv ) - does
av_store() automatically free the old SVs when they're overwriitten?
Obviously, a more generic wrapper would malloc the unsigned short int
arrays based on the length prior to the real call and free them
afterward but this serves my needs and it's simple (perhaps rev 2.0...).
Any comments will be taken in the spirit they're given.
Thanks, Tom
------------------------------
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 V10 Issue 42
*************************************