[22577] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 4798 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Apr 1 06:05:58 2003

Date: Tue, 1 Apr 2003 03:05:10 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Tue, 1 Apr 2003     Volume: 10 Number: 4798

Today's topics:
        accessing hash reference data in embedded perl from C++ (Chage)
    Re: accessing hash reference data in embedded perl from <tassilo.parseval@rwth-aachen.de>
    Re: accessing hash reference data in embedded perl from (Chage)
    Re: accessing hash reference data in embedded perl from <tassilo.parseval@rwth-aachen.de>
        array or two hashes? <hannes@michalek.de>
    Re: array or two hashes? (Anno Siegel)
    Re: Beginner's Perl question - trouble with a very simp <josef.moellers@fujitsu-siemens.com>
        Beginner's Perl question - trouble with a very simple s (Martyn Rankin)
    Re: Beginner's Perl question - trouble with a very simp (Anno Siegel)
    Re: CGI.pm or roll-your-own? (Helgi Briem)
    Re: CGI::ContactForm 1.03 - Feedback request <tore@aursand.no>
    Re: CGI::ContactForm 1.03 - Feedback request <noreply@gunnar.cc>
    Re: CGI::ContactForm 1.03 - Feedback request <noreply@gunnar.cc>
    Re: Does Perl can do everything you need in unix shell  <michael+USENET@www.heiming.de>
        Function parameter of Array and Array reference. <pengtaoli@hotmail.com>
    Re: Function parameter of Array and Array reference. <tassilo.parseval@rwth-aachen.de>
    Re: implementing a threaded serial device (or any other <stacom@stacom-software.de>
    Re: modules using modules - shared objects <tassilo.parseval@rwth-aachen.de>
    Re: modules using modules - shared objects <goldbb2@earthlink.net>
    Re: modules using modules - shared objects <nwzmattXX@XXnetscape.net>
    Re: modules using modules - shared objects <tassilo.parseval@rwth-aachen.de>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 31 Mar 2003 22:30:21 -0800
From: ericfcb@hotmail.com (Chage)
Subject: accessing hash reference data in embedded perl from C++
Message-Id: <f61bf635.0303312230.2c16e860@posting.google.com>

I searched tru the net, trying to look for some embedded Perl
documentation in getting a hash reference value, I can't get it.

my question: 
1) is there any way of retrieving hash reference data? 

i know perl_get_hv can return a pointer of HV, if the hash format is
something like that:
$main::testHash{'key1'} = "value1"; 
$main::testHash{'key2'} = "value2"; 

but i can't get the hash value by simply using perl_get_hv if my hash
is a reference:
$main::referenceHash = { 'key3' => "value3", 
'key4' => "value4" 
}; 

this will return a bad hash to me. 
is there any work around to get data from Hash reference? 
im working on it now, still, hoping for some solution, meanwhile, i
wish that you can find out some answer for me too, thanks a thousand!

rgds, 
chii biao


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

Date: 1 Apr 2003 06:48:00 GMT
From: "Tassilo v. Parseval" <tassilo.parseval@rwth-aachen.de>
Subject: Re: accessing hash reference data in embedded perl from C++
Message-Id: <b6bcn0$ggi$1@nets3.rz.RWTH-Aachen.DE>

Also sprach Chage:

> I searched tru the net, trying to look for some embedded Perl
> documentation in getting a hash reference value, I can't get it.
> 
> my question: 
> 1) is there any way of retrieving hash reference data? 
> 
> i know perl_get_hv can return a pointer of HV, if the hash format is
> something like that:
> $main::testHash{'key1'} = "value1"; 
> $main::testHash{'key2'} = "value2"; 
> 
> but i can't get the hash value by simply using perl_get_hv if my hash
> is a reference:
> $main::referenceHash = { 'key3' => "value3", 
> 'key4' => "value4" 
> }; 
> 
> this will return a bad hash to me. 

$main::referenceHash is foremost a scalar and not a hash. You have to
dereference that yourself. Something along the following should work.

    SV *ref  = get_sv("main::referenceHash", FALSE);
    HV *hash = (HV*) SvRV(ref);

SvRV() is the generic way to dereference Perl-types. You simply cast the
return value accordingly.

Tassilo
-- 
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval


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

Date: 1 Apr 2003 02:22:16 -0800
From: ericfcb@hotmail.com (Chage)
Subject: Re: accessing hash reference data in embedded perl from C++
Message-Id: <f61bf635.0304010222.466e4c12@posting.google.com>

"Tassilo v. Parseval" <tassilo.parseval@rwth-aachen.de> wrote in message news:<b6bcn0$ggi$1@nets3.rz.RWTH-Aachen.DE>...
> Also sprach Chage:
> 
> > I searched tru the net, trying to look for some embedded Perl
> > documentation in getting a hash reference value, I can't get it.
> > 
> > my question: 
> > 1) is there any way of retrieving hash reference data? 
> > 
> > i know perl_get_hv can return a pointer of HV, if the hash format is
> > something like that:
> > $main::testHash{'key1'} = "value1"; 
> > $main::testHash{'key2'} = "value2"; 
> > 
> > but i can't get the hash value by simply using perl_get_hv if my hash
> > is a reference:
> > $main::referenceHash = { 'key3' => "value3", 
> > 'key4' => "value4" 
> > }; 
> > 
> > this will return a bad hash to me. 
> 
> $main::referenceHash is foremost a scalar and not a hash. You have to
> dereference that yourself. Something along the following should work.
> 
>     SV *ref  = get_sv("main::referenceHash", FALSE);
>     HV *hash = (HV*) SvRV(ref);
> 
> SvRV() is the generic way to dereference Perl-types. You simply cast the
> return value accordingly.
> 
> Tassilo


Tassilo,  thanks alot!
it works fine. 
I was actually looking for some method in dereferencing the hash
reference, too bad i never thought of type-casting it with SvRV, coz
SvRV stated return value as SV* in the documentation.

Thanks again for your help, truly. Cool!!


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

Date: 1 Apr 2003 10:50:01 GMT
From: "Tassilo v. Parseval" <tassilo.parseval@rwth-aachen.de>
Subject: Re: accessing hash reference data in embedded perl from C++
Message-Id: <b6bqsp$2vn$1@nets3.rz.RWTH-Aachen.DE>

Also sprach Chage:

> "Tassilo v. Parseval" <tassilo.parseval@rwth-aachen.de> wrote in message news:<b6bcn0$ggi$1@nets3.rz.RWTH-Aachen.DE>...

>> $main::referenceHash is foremost a scalar and not a hash. You have to
>> dereference that yourself. Something along the following should work.
>> 
>>     SV *ref  = get_sv("main::referenceHash", FALSE);
>>     HV *hash = (HV*) SvRV(ref);
>> 
>> SvRV() is the generic way to dereference Perl-types. You simply cast the
>> return value accordingly.
>> 
>> Tassilo
> 
> 
> Tassilo,  thanks alot!
> it works fine. 
> I was actually looking for some method in dereferencing the hash
> reference, too bad i never thought of type-casting it with SvRV, coz
> SvRV stated return value as SV* in the documentation.

That would be perlapi.pod. Some general concepts can be found in
perlguts.pod. It explains how to work with the basic types such as
scalars, arrays and hashes. It also deals with some other important
things such as reference-count and mortality.

Tassilo
-- 
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval


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

Date: Tue, 1 Apr 2003 10:16:34 +0200
From: hannes michalek <hannes@michalek.de>
Subject: array or two hashes?
Message-Id: <b6bhr4$3jqer$1@ID-111424.news.dfncis.de>

i have two sub's in my script: 
1) takes a directory and returns a list of files in that directory
2) takes a directory and returns the text of a certain line in a certain 
file in that directory.

the problem is: the script does that very often and many times for the same 
directories. so i want to scan all directories at the beginning, storing 
filenames and text and using the stored ones later in the program.

my question is, then: 
is it faster to use an array (directory,filenames,text) or two hashes 
(directory, filelist) and (directory,text)?

there are about 80 directories involved with 2-10 files (for the 
filenames-list) and 1 text-line each 


thanks!

hannes

-- 
hannes michalek (after change to gpg in 08/2002) <hannes@michalek.de>
Key fingerprint = E160 AFC5 7B0F 311D C5B4 42CB 6592 D5FA 338F 4932




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

Date: 1 Apr 2003 09:36:52 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: array or two hashes?
Message-Id: <b6bmjk$fb$1@mamenchi.zrz.TU-Berlin.DE>

hannes michalek  <hannes@michalek.de> wrote in comp.lang.perl.misc:
> i have two sub's in my script: 
> 1) takes a directory and returns a list of files in that directory
> 2) takes a directory and returns the text of a certain line in a certain 
> file in that directory.
> 
> the problem is: the script does that very often and many times for the same 
> directories. so i want to scan all directories at the beginning, storing 
> filenames and text and using the stored ones later in the program.
> 
> my question is, then: 
> is it faster to use an array (directory,filenames,text) or two hashes 
> (directory, filelist) and (directory,text)?

Speed isn't your problem at this point, getting it right is.  Chose
the most natural way to express what you need and worry about efficiency
later.  If you save yourself a walk through some disk directories,
you have a lot of time to burn.

> there are about 80 directories involved with 2-10 files (for the 
> filenames-list) and 1 text-line each 

Walk through the directories and collect the file names as before.
In addition, if the "certain file" exists in that directory, push
its (full) name on another list.  Let the sub return references to
both lists.

Anno


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

Date: Tue, 01 Apr 2003 12:27:48 +0200
From: Josef =?iso-8859-1?Q?M=F6llers?= <josef.moellers@fujitsu-siemens.com>
Subject: Re: Beginner's Perl question - trouble with a very simple search/pattern  matching script
Message-Id: <3E8969A4.E9C90B08@fujitsu-siemens.com>

Martyn Rankin wrote:
> =

> Hi, I'm new to perl & am having trouble with a basic file - I want to
> open a file and loop through it to search for 6 different patterns
> (shown in the array '@mv') - these patterns are listed in output.txt,
> yet when I run the file I just get the following output:
> =

> Looking for dog...failed
> Looking for cat...failed
> Looking for bird...failed
> Looking for fish...failed
> =

> Can anyone see where I'm going wrong, or suggest a better way to do
> it? Thanks for your help in advance

First of all, the output you show above does not match the code fragment
you show beneath:


> #!/usr/local/bin/perl
> open (RESULTS,"output.txt") || die "Error - cannot open results
> file\n";
> @mv =3D ("dog", "cat", "bird", "fish");
> foreach $mv (@mv)
> {
>    print "Looking for $mv...\n";
*** This will produce a newline at the end, so any subsequent output
should show up on a new line, yet in the "output" you givem these are on
the same line!
>    if (/$mv/){
>       print "mv ok\n";
>    }
>    else{
>       print "failed\n";
>    }
> }
> close (RESULTS);

You do not reference RESULTS anywhere!

Anyway, I'd do it like this:

# Note that
# 1. I have prepended "< " to the file name to open
# 2. I have used "or" rather than "||"
# 3. I have also specified "$!" so perl will tell me why it failed.

open(RESULTS, "< output.txt") or die "Error - cannot open results file:
$!\n";

while ($line =3D <RESULTS>) {
   foreach $mv (@mv) {
	print "Looking for $mv...";	# No newline
	if ($line =3D~ /$mv/) {
	    print "mv ok\n";
	} else {
	    print "failed\n";
	}
    }
}

close RESULTS;

Note that $line will have a line terminator.

HTH,

Josef
-- =

Josef M=F6llers (Pinguinpfleger bei FSC)
	If failure had no penalty success would not be a prize
						-- T.  Pratchett


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

Date: 1 Apr 2003 02:12:26 -0800
From: mgf_rankin@yahoo.com (Martyn Rankin)
Subject: Beginner's Perl question - trouble with a very simple search/pattern matching script
Message-Id: <c4147d77.0304010212.437d4033@posting.google.com>

Hi, I'm new to perl & am having trouble with a basic file - I want to
open a file and loop through it to search for 6 different patterns
(shown in the array '@mv') - these patterns are listed in output.txt,
yet when I run the file I just get the following output:

Looking for dog...failed
Looking for cat...failed
Looking for bird...failed
Looking for fish...failed

Can anyone see where I'm going wrong, or suggest a better way to do
it? Thanks for your help in advance

Martyn


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

Match.pl
********

#!/usr/local/bin/perl
open (RESULTS,"output.txt") || die "Error - cannot open results
file\n";
@mv = ("dog", "cat", "bird", "fish");
foreach $mv (@mv)
{
   print "Looking for $mv...\n";
   if (/$mv/){
      print "mv ok\n";
   }
   else{
      print "failed\n";
   }
}
close (RESULTS);

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

Output.txt
**********

dog
cow
cat
sheep
bird
horse
fish


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

Date: 1 Apr 2003 10:33:51 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Beginner's Perl question - trouble with a very simple search/pattern matching script
Message-Id: <b6bpuf$485$1@mamenchi.zrz.TU-Berlin.DE>

Martyn Rankin <mgf_rankin@yahoo.com> wrote in comp.lang.perl.misc:
> Hi, I'm new to perl & am having trouble with a basic file - I want to
> open a file and loop through it to search for 6 different patterns
> (shown in the array '@mv') - these patterns are listed in output.txt,
> yet when I run the file I just get the following output:
> 
> Looking for dog...failed
> Looking for cat...failed
> Looking for bird...failed
> Looking for fish...failed
> 
> Can anyone see where I'm going wrong, or suggest a better way to do
> it? Thanks for your help in advance

First, look at the faq that deals with (part of) the problem:
"perldoc -q 'match many'" has something to say about this problem.

> ----------------
> 
> Match.pl
> ********
> 
> #!/usr/local/bin/perl

No use strict.  No warnings.  Bad.

> open (RESULTS,"output.txt") || die "Error - cannot open results
                 ^^^^^^

That's a strange name for an input file.

> file\n";
> @mv = ("dog", "cat", "bird", "fish");
> foreach $mv (@mv)
> {
>    print "Looking for $mv...\n";
>    if (/$mv/){

Here, /$mv/ matches against the default $_.  But you have never assigned
anything to $_, so there's nothing to match.  Under warnings, Perl would
have told you so.

>       print "mv ok\n";
>    }
>    else{
>       print "failed\n";
>    }
> }
> close (RESULTS);

You have opened your input file, but you never read from it.  Put another
loop around the foreach loop to load each line from the file into $_

    while ( <RESULTS> ) {
        foreach my $mv ( @mv ) {
            # ....
        }
    }

Anno


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

Date: Tue, 01 Apr 2003 09:49:58 GMT
From: helgi@decode.is (Helgi Briem)
Subject: Re: CGI.pm or roll-your-own?
Message-Id: <3e89606f.277270714@news.cis.dfn.de>

On 31 Mar 2003 16:53:05 GMT, Greg Raven
<usrsa@racquettech.com> wrote:

>> (See http://www.perldoc.com/perl5.8.0/lib/CGI.html)  And 10 lines later,
>> there's the use of param().  This is the very first thing after the table
>> of contents.  It's even before the abstract.  How much easier can it get?
>
>I was trying to read everything before doing anything. Also, the first 
>example didn't address the task I was attempting to accomplish, so I 
>hoped that by reading more of the documentation I might understand how 
>to approach my task optimally.

Never, ever do that!  ;-)  The Perl docs are arranged so
that 90% of the time, what you need is in the first 20
lines or so.  Not like the typical Unix man pages where
the most common thing to do is buried 73 pages down.
-- 
Regards, Helgi Briem
helgi DOT briem AT decode DOT is


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

Date: Tue, 01 Apr 2003 07:27:18 +0200
From: "Tore Aursand" <tore@aursand.no>
Subject: Re: CGI::ContactForm 1.03 - Feedback request
Message-Id: <pan.2003.04.01.04.32.48.689261@aursand.no>

On Tue, 01 Apr 2003 04:14:46 +0200, Gunnar Hjalmarsson wrote:
>> As your module is today, you must most certainly be a Perl programmer
>> to change any of the text.

> No. All the text can be changed via arguments in the calling script.

There is no HTML code?


-- 
Tore Aursand


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

Date: Tue, 01 Apr 2003 11:57:01 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: CGI::ContactForm 1.03 - Feedback request
Message-Id: <b6bnuc$3k24l$1@ID-184292.news.dfncis.de>

Tore Aursand wrote:
> On Tue, 01 Apr 2003 04:14:46 +0200, Gunnar Hjalmarsson wrote:
> 
>>> As your module is today, you must most certainly be a Perl
>>> programmer to change any of the text.
> 
>> No. All the text can be changed via arguments in the calling
>> script.
> 
> There is no HTML code?

Let me explain how I'm thinking. In the initial message in this thread 
I wrote:
     "Its purpose is to make it *very* easy to create an unlimited
     number of web contact forms. It's not a general tool for
     creating forms; there are already quite a few of those out
     there. It's just doing one thing, but it's doing it with a
     minimum of user effort."

You can:
- change any text string via arguments to the contactform()
   function, and
- edit the enclosed CSS file (or create your own),
but you can't change the markup code without editing the module.

The thought is, at least so far, that if somebody is particular about 
the markup, or wants additional fields, s/he'd better use a general 
tool for generating web forms.

I want to keep it simple. *Very* simple.

/ Gunnar

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl



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

Date: Tue, 01 Apr 2003 11:57:04 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: CGI::ContactForm 1.03 - Feedback request
Message-Id: <b6bnuf$3k24l$2@ID-184292.news.dfncis.de>

Tore Aursand wrote:
> On Tue, 01 Apr 2003 01:57:00 +0200, Gunnar Hjalmarsson wrote:
> 
>> sub htmlize {
>>     [...]
>> }
>> 
>> I found that CGI.pm includes a function for it, so I simply
>> replaced the above subroutine with the escapeHTML() function.
> 
> Great!  Now, with the templating in mind you could have used that
> function for _every_ value inserted into the HTML template.  That
> would've resulted in perfectly valid HTML no matter what characters
> the users of your module have been trying to use.

Or, with the current design of the module, you can simply extend the 
use of the escapeHTML() function to the arguments, and not just use it 
for the submitted form input. Think I'll do that.

Thanks!

/ Gunnar

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl



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

Date: Tue, 1 Apr 2003 08:37:45 +0200
From: Michael Heiming <michael+USENET@www.heiming.de>
Subject: Re: Does Perl can do everything you need in unix shell scripting?what about PHP?
Message-Id: <p3cb6b.9f5.ln@news.heiming.de>

In comp.unix.shell edu@rd c <ecleofe7@yahoo.com> wrote:
> hi Guys,
>         Need your advice.This could be helpfull for those newbie.I
> want to learn shell scripting.I just want to know that if you master
> the Perl you can overcome all the shell scripting needed in unix,on
> w/c you dont need to study those csh,bsh,bash and so on.But what about
> PHP,since they are just the same function with perl and this is much
> easier to learn?

Forget about PHP, perl is great to know and comes in handy for many 
purposes, but you should first learn the shell (sh/bash/ksh) and
probably awk is nice to know, you'll find it on almost every *nix,
perl might not even be installed per default on many *nix. So it
won't help you much.

-- 
Michael Heiming

Remove +SIGNS and www. if you expect an answer, sorry for 
inconvenience, but I get tons of SPAM


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

Date: Tue, 1 Apr 2003 17:04:43 +0800
From: "Franklin Lee" <pengtaoli@hotmail.com>
Subject: Function parameter of Array and Array reference.
Message-Id: <b6bkpa$em6@netnews.proxy.lucent.com>

Hello All,

I want to use one function defined in a module.
Module file format like below:
*************************************************
package Mypackage;
 ....
@EXPORT = qw( abc );

sub abc($;\@)
{
 ...
}

1;
 ...
*************************************************
In my own program, I will call function abc .
if I write abc($a,\@b), it will give Warning message:
 ...must be array ( not reference constructor) at ...

Then I write abc($a,@b), it will no Warning message
and work well.

But I don't know why this happen. Up to function defination,
it should fill array reference.

Who can tell me the reason?

Thank you in advance!

Franklin





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

Date: 1 Apr 2003 09:37:12 GMT
From: "Tassilo v. Parseval" <tassilo.parseval@rwth-aachen.de>
Subject: Re: Function parameter of Array and Array reference.
Message-Id: <b6bmk8$qao$1@nets3.rz.RWTH-Aachen.DE>

Also sprach Franklin Lee:

> I want to use one function defined in a module.
> Module file format like below:
> *************************************************
> package Mypackage;
> ....
> @EXPORT = qw( abc );
> 
> sub abc($;\@)
> {
> ...
> }
> 
> 1;
> ...
> *************************************************
> In my own program, I will call function abc .
> if I write abc($a,\@b), it will give Warning message:
> ...must be array ( not reference constructor) at ...
> 
> Then I write abc($a,@b), it will no Warning message
> and work well.
> 
> But I don't know why this happen. Up to function defination,
> it should fill array reference.

The prototype does two things: First it ensures that the passed
parameters are in fact valid and, in the case of \@, it turns the second
one into a reference automatically. That is needed in order to write
functions similar to those of Perl's push():

    sub mypush(\@@) {
        my ($dest, @src) = @_;
        push @$dest, @src;
    }

    mypush @array, @another_array;

Without a prototype automatically transforming the first array into a ref,
this would not be possible since Perl flattens the arguments into one
large list so that it is later impossible to distinguish between the
first and the second array.

In short: having a prototype like \$, \% etc. will result in taking
references to the arguments and pass those. If you wanted to pass a
reference yourself, your prototype would have to look like:

    sub abc($;$);

because a reference is a scalar.

Tassilo
-- 
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval


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

Date: Tue, 01 Apr 2003 10:30:59 +0200
From: Alexander Eisenhuth <stacom@stacom-software.de>
Subject: Re: implementing a threaded serial device (or any other parallel way)
Message-Id: <b6bio1$3ljgr$1@ID-155280.news.dfncis.de>


>    my $comobj = bless {}, $packagename;
>    &threads::shared::share $comobj;
>    bless $comobj, $packagename; # extra blessing for good luck
>    # now, *AFTER* sharing, fill in the data.
>    %$comobj = (
>       inputBuffer=>"",
>       running=>1,
>       watchThread=>0,
>       internalCountr=>0,
>       serialPort=>0,
>       osDeviceName => $osDeviceName,
>    );
>    $comobj->{serialPort} = ...;
>    my $watch_thread = threads->create( \&watch_device, $comobj, $c );

Thank you for you reply. blessing that way don't work, because it seems to perl
as a unblessed reference, if you call that object later on. To clarify the
situation here the scripts

---------------- main.pm ----------------------------
use developThreadInPerlWork;

# -- setup a device
$comDev = new CComClass();
print "main: comDev:",$comDev,"\n";

# -- wait for return
$a = <STDIN>;

# -- signal, that we want to shutdown
$comDev->exitThread();

# -- wait
sleep 2;

---------------- developThreadInPerlWork.pm -----------
use threads;
use threads::shared; # to share data amoung threads
package CComClass;
# global variable
my $threadRunning=1;
threads::shared::share ($threadRunning);

sub new {                # Allocator and Initializer
	my ($packageName) = @_;
	my $comObj = bless {}, $packageName;
	threads::shared::share $comObj;
	# bless  $comObj, $packageName; # blessing here is wrong because we can't call
exitThread later on
	my $comObj = {
		inputBuffer=>"",
		running=>1,
		watchThread=>0,
		internalCounter=>0
	};
	bless  $comObj, $packageName;
	print "CComClass::new - 1 :",$comObj,"\n";
	$comObj->{$watchThread} = create threads \&watchDevice, $comObj;
	print "CComClass::new - 2 :",$comObj,"\n";
	return $comObj;
}

sub exitThread {
	my ($obj) = @_;
	$obj->{running} = 0;
	$threadRunning = 0;
}

sub watchDevice {
	my ($comObj) = @_;
	print "threadLoop comObj >>>",$comObj, "<<<\n";
	while ($threadRunning) {
		print "threadLoop(",$threadRunning,")","\n";
		sleep 1;
	};
	print "thread finished(",$comObj->{running},")\n";
}

----------------------------------------------------------
output:
CComClass::new - 1 :CComClass=HASH(0x813b218)
threadLoop comObj >>>CComClass=HASH(0x825e174)<<<
threadLoop(1)
CComClass::new - 2 :CComClass=HASH(0x813b218)
main: comDev:CComClass=HASH(0x813b218)
threadLoop(1)
threadLoop(1)

The thread seems to copy the blessed comObj. In the doc of threads::shared there
is written amoung BUGS:
[...]
bless is not supported on shared references. In the current version, bless will
only bless the thread local reference and the blessing will not propagate to the
other threads. This is expected to be implemented in a future version of Perl.
[..]

That sounds to me, that threads, sharing "objects" won't work in perl :-(, and
so I'm also expecting problems using Device::SerialPort.

Regards
Alexander

Regards




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

Date: 1 Apr 2003 05:58:47 GMT
From: "Tassilo v. Parseval" <tassilo.parseval@rwth-aachen.de>
Subject: Re: modules using modules - shared objects
Message-Id: <b6b9qn$elc$1@nets3.rz.RWTH-Aachen.DE>

Also sprach Benjamin Goldberg:

> Matt wrote:

>> Thanks Tassilo. I'm starting to get a handle on it. I've been reading
>> up on inheritance a little. What I want to do now is to (bless? tie?)
> 
> bless.
> 
> tieing is something different.
> 
>> an object of my class to a DBI object. In other words, just extend the
>> methods of the DBI object to include some custom methods. I've tried
>> various things, but I'm not sure what I'm doing and nothing is
>> working. Can you(or anyone else) give me an example of how this might
>> be possible, or point me in the direction of one.
> 
> This will *not* work with DBI, but the *ordinary* semantic for
> inheriting from another class would be something like this:

I am not myself an expert with DBI but I lately did what, according to
you, should not work. Namely extending DBI by simply inheriting from it.
However, I couldn't directly subclass DBI but instead had to inherit
from DBI::db:

    package Santiago::Db;

    use strict;
    use DBI;
    use base qw(DBI::db);

    sub new {
        my ($class, $database, $username, $password) = @_;
        my $dsn = "DBI:mysql:database=$database;host=localhost";
        my $dbh = DBI->connect($dsn, $username, $password)
            or die $DBI::errstr;
        
        my $self = bless $dbh => $class;
        ...                 
        $self;
    }

Overriding certain functions did not make problems either:

    sub disconnect {
        my $self = shift;
        # finishing some statement handles
        $self->ac_insert_finish;
        $self->ac_exists_finish;
        $self->ac_update_finish;
        $self->cp_update_finish;
        $self->SUPER::disconnect;
    }

Since I have heard it frequently that DBI is special with respect to
inheritance, I wonder what the shortcomings of my approach would be
(other than poking around a little with the internals by subclassing
DBI::db). I stored new attributes (a couple of statement handlers
basically) with the help of closures so that I would not cause any
clashes with the internal structure of a DBI::db object (I am not even
sure whether it's a blessed hash, it could be a pointer to a C-struct,
too, in which case I couldn't store additional attributes in it).

Tassilo
-- 
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval


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

Date: Tue, 01 Apr 2003 02:50:07 -0500
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: modules using modules - shared objects
Message-Id: <3E8944AF.26D31640@earthlink.net>

"Tassilo v. Parseval" wrote:
[snip question about subclassing DBI]
> > This will *not* work with DBI, but the *ordinary* semantic for
> > inheriting from another class would be something like this:
> 
> I am not myself an expert with DBI but I lately did what, according to
> you, should not work. Namely extending DBI by simply inheriting from
> it. However, I couldn't directly subclass DBI but instead had to
> inherit from DBI::db:
[snip]

Directly subclassing DBI was, in fact, what I was saying "would not
work".

Subclassing DBI::db is, in fact, precisely *one* of the things I was
referring to when I said you needed to do some other special things if
you want to subclass DBI.

Read the "Subclassing the DBI" part of DBI.pm's documentation.

Here's a minimal class for allowing you to write
   my $dbh = SomeClass->connect( ... );
And have it act *just* like DBI->connect( ... );.

   package SomeClass;
   use base 'DBI';
   @SomeClass::db::ISA = "DBI::db";
   @SomeClass::st::ISA = "DBI::st";
   1;
   __END__

Each $dbh object will be an instance of SomeClass::db, and each
statement handle returned by $dbh->prepare will be a SomeClass::st
object.

Tasillo -- the problem with your approach, is that if someone *merely*
wanted to add methods, they still have to overwrite ->connect, or
provide a method which calls ->connect.

With "the right way", if they don't need to add anything at creation
time, they don't need to define a connect sub, and can just use the
parent class's connect method through inheritance.

If you want to provide extra methods on statement handles, with your
approach, you'd have to override ->prepare, whereas with this "correct"
approach, statement handles are automatically blessed to the right
class.

And the biggest advantage is that it looks cleaner.

-- 
$a=24;split//,240513;s/\B/ => /for@@=qw(ac ab bc ba cb ca
);{push(@b,$a),($a-=6)^=1 for 2..$a/6x--$|;print "$@[$a%6
]\n";((6<=($a-=6))?$a+=$_[$a%6]-$a%6:($a=pop @b))&&redo;}


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

Date: Tue, 01 Apr 2003 08:26:40 GMT
From: Matt <nwzmattXX@XXnetscape.net>
Subject: Re: modules using modules - shared objects
Message-Id: <uhe9i6b4h.fsf@XXnetscape.net>

Benjamin Goldberg <goldbb2@earthlink.net> writes:
<snip> 
> What you would have to do instead would be:
> 
>    package MyDBI;
>    use DBI ();
>    sub connect {
>       my $thisclass = shift;
>       my $self = DBI->connect( @_ );
>       my $old_class = ref $self;
>       my $new_class = $old_class . "::$thisclass Mixin";
>       @{ $new_class . "::ISA" } = ( $thisclass, $old_class );
>       return bless $self, $new_class;
>    }
>    sub selectSomething {
>       my $dbh = shift;
>       my ($a) = $dbh->selectrow_array("select something from nothing");
>       return $a;
>    }
>    1;
>    __END__
>    [and the main script as already described]
> 
> [untested]
> 
> Even this might not work.  I think it should.

It does. Thanks, this is helping me a lot. Can you explain what
`Mixin' is/does. Whats going on with line 8? It translates into:

        @{ "DBI::db::MyDBI Mixin::ISA" } = ( "MyDBI", "DBI::db" );

TIA,

Matt
-- 
Remove the X's to reply directly.


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

Date: 1 Apr 2003 09:10:28 GMT
From: "Tassilo v. Parseval" <tassilo.parseval@rwth-aachen.de>
Subject: Re: modules using modules - shared objects
Message-Id: <b6bl24$op6$1@nets3.rz.RWTH-Aachen.DE>

Also sprach Benjamin Goldberg:

> "Tassilo v. Parseval" wrote:

>> I am not myself an expert with DBI but I lately did what, according to
>> you, should not work. Namely extending DBI by simply inheriting from
>> it. However, I couldn't directly subclass DBI but instead had to
>> inherit from DBI::db:
> [snip]
> 
> Directly subclassing DBI was, in fact, what I was saying "would not
> work".
> Subclassing DBI::db is, in fact, precisely *one* of the things I was
> referring to when I said you needed to do some other special things if
> you want to subclass DBI.
> 
> Read the "Subclassing the DBI" part of DBI.pm's documentation.

Oh, never read this one yet (then I could have saved my posting, I guess). 
That I would have to use DBI::db became obvious through empirism. 

> Here's a minimal class for allowing you to write
>    my $dbh = SomeClass->connect( ... );
> And have it act *just* like DBI->connect( ... );.
> 
>    package SomeClass;
>    use base 'DBI';
>    @SomeClass::db::ISA = "DBI::db";
>    @SomeClass::st::ISA = "DBI::st";
>    1;
>    __END__
> 
> Each $dbh object will be an instance of SomeClass::db, and each
> statement handle returned by $dbh->prepare will be a SomeClass::st
> object.
> 
> Tasillo -- the problem with your approach, is that if someone *merely*
  ^^^^^^^
    !! ;-)
    
> wanted to add methods, they still have to overwrite ->connect, or
> provide a method which calls ->connect.

Which happens in my case with the constructor new(). I never thought
about changing DBI::connect() so that did not occur to me.

> With "the right way", if they don't need to add anything at creation
> time, they don't need to define a connect sub, and can just use the
> parent class's connect method through inheritance.
> 
> If you want to provide extra methods on statement handles, with your
> approach, you'd have to override ->prepare, whereas with this "correct"
> approach, statement handles are automatically blessed to the right
> class.

I did the subclassing mainly to reduce the clutter a little. All I
needed were a few extra methods added to $dbh. Without subclassing I
would have needed to store $dbh in my own object which would lead to
such ugly things as

    $self->{dbh}->do(...);

@ISAing DBI::db along with a little re-bless turned out to be the
cheapest solution (even so cheap that it did not require reading any
perldocs;-).

> And the biggest advantage is that it looks cleaner.

They aren't even equivalent. I think I would have needed quite a few
contortions in my class if I had wanted to subclass statement-handlers
as well.

Tassilo
-- 
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval


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

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

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.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 4798
***************************************


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