[19523] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 1718 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun Sep 9 11:05:27 2001

Date: Sun, 9 Sep 2001 08:05:08 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <1000047907-v10-i1718@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Sun, 9 Sep 2001     Volume: 10 Number: 1718

Today's topics:
        Alternative to Storable for passing arbitray data betwe (Stan Brown)
        Arrays, References and indexes. <sudhir@newmail.net>
    Re: Arrays, References and indexes. (Martien Verbruggen)
    Re: CR/LF <bart.lateur@skynet.be>
    Re: CR/LF <flavell@mail.cern.ch>
    Re: CR/LF (Martien Verbruggen)
    Re: Disappearing Compiler messages <goldbb2@earthlink.net>
    Re: Disappearing Compiler messages <flavell@mail.cern.ch>
    Re: Disappearing Compiler messages <goldbb2@earthlink.net>
    Re: Evaluation order of object methods <goldbb2@earthlink.net>
    Re: fore! <goldbb2@earthlink.net>
    Re: form input changed from Select to hidden, no errors <goldbb2@earthlink.net>
        free guestbook script <y_h@pi.be>
    Re: guestbook <gnarinn@hotmail.com>
    Re: guestbook <y_h@pi.be>
    Re: help with this script-database column names <goldbb2@earthlink.net>
    Re: How can I find the PID's of my children? <bernie@fantasyfarm.com>
    Re: object & fork <goldbb2@earthlink.net>
    Re: Perl objects problems (mirvine555)
        Reverse DNS lookup <Gareth@Horth.F9.co.uk>
    Re: running ActivePerl on W2k Prof <bart.lateur@skynet.be>
    Re: Simple regexp <bart.lateur@skynet.be>
    Re: Why doesn't this regex work? <bart.lateur@skynet.be>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 9 Sep 2001 08:36:49 -0400
From: stanb@panix.com (Stan Brown)
Subject: Alternative to Storable for passing arbitray data between a child and i
Message-Id: <9nfnp1$ajj$1@panix1.panix.com>

I have a perlTK script which forks a child to do large DBI queries. 
I this uses DBD::fetchall_arrayref to do the fetch, and passes the result
back to the parent uisng a pipe and Storable.

On large datsets, I am getting Storable erros about corupt Storable fiels.
I am not runing out of swap mspace, or anything nasty like that. 

Can anyone sugest an alternative way to pass arbitrary data between 2
co-operationg processes?




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

Date: Sun, 09 Sep 2001 11:59:50 GMT
From: Sudhir Krishnan <sudhir@newmail.net>
Subject: Arrays, References and indexes.
Message-Id: <3B9B5A80.9D2198E3@newmail.net>

Hi,
 
I've been using perl since a week.

I need to read in a number of files and use an appropriate
data-structure for the same.

Each file is divided into a number of sections.
Each line will be a record


 file 1    file 2   file 3
[     ]  [     ]  [     ]
[     ]  [     ]  [     ]
  ---     ---      ---
[     ]  [     ]  [     ]
[     ]  [     ]  [     ]
  ----     ----    ----
[     ]  [     ]  [     ]
[     ]  [     ]  [     ]

Something like a triple dimensional array seems to suit the purpose.


a[i][j][k];

i=file index
j=section index in file
k=line (record) index in (file, section)


While iterating over the files, I'm storing it as:

a[i][j][k] = $_;

And it seems to work fine.

Problems:
1) I don't really understand the underlying implementation 
   with references.

2) I use a[i][j][k] for getting/setting values, its
   3 loops and is cumbersume.

3) I'd like to know how I can get/set values without using indexes.

  i.e for a simple array:
instead of
   for ($i=0; $i <=$#ARR; $i++)

I'd like to use
   for $val (@ARR)

But in my case, $val will be a reference until I read the 3rd level,
right?

Sample code would be really appreciated!!

thanks!
Sudhir


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

Date: Sun, 9 Sep 2001 22:58:24 +1000
From: mgjv@tradingpost.com.au (Martien Verbruggen)
Subject: Re: Arrays, References and indexes.
Message-Id: <slrn9pmprf.b0q.mgjv@martien.heliotrope.home>

On Sun, 09 Sep 2001 11:59:50 GMT,
        Sudhir Krishnan <sudhir@newmail.net> wrote:

> i=file index
> j=section index in file

How do you distinguish between the 'sections'?

> k=line (record) index in (file, section)

> While iterating over the files, I'm storing it as:
> 
> a[i][j][k] = $_;
> 
> And it seems to work fine.
> 
> Problems:
> 1) I don't really understand the underlying implementation 
>    with references.

Have you read the documentation, specifically perlref, perllol and
perldsc? They go into quite some detail about how perl references, and
perl's complex data structures work..

> 2) I use a[i][j][k] for getting/setting values, its
>    3 loops and is cumbersume.

You will, most likely need three loops. However, you may not need them
all explicitly. You could do something like:

Read the line into a (lexically scoped) array, splitting it. Then use an
assignment like:

$a[i][j] = \@array;

or, if the array needs to be reused, store a reference to a copy of the
array:

$a[i][j] = [@array];

Of course, you can do the same thing with the files, one level up.
You'll still be using three iterations, just not as specific anymore,
maybe.

> 3) I'd like to know how I can get/set values without using indexes.
> 
>   i.e for a simple array:
> instead of
>    for ($i=0; $i <=$#ARR; $i++)
> 
> I'd like to use
>    for $val (@ARR)
> 
> But in my case, $val will be a reference until I read the 3rd level,
> right?

I'm not entirely sure what you mean. $val, in this for loop, will always
specifically be an alias (not the same as a reference, but similar) to
the things you're iterating over. If there isn't anything yet to be an
alias to, then how can this work? I mean, you're filling the data
structure, right? So the data and the references aren't there yet...

If these things that you work with are all numbered, what's wrong with
using indexes? If you'd rather use push to add new data to the end, you
could do something like (untested pseudo-code follows):

my @files;

foreach my $file (@files)
{
    my @sections;   # all the sections in a file
    my @section;    # the records in a single section in a file

    open my $fh, $file or die $!;
    while (<$fh>)
    {
        if (line_looks_like_new_section)
        {
            push @sections, [@section];
            @section = ();
        }
        else
        {
            my @records = split;
            push @section, \@records;
        }
    }

    push @array, \@sections;
}

 ... unless I've misunderstood your structure. If I have, I'm sure you'll
be able to get the idea and adapt it to your needs.
as you can see, you can just store a reference to @sections and
@records, since they will go out of scope immediately after you've
stored them, and therefore won't be reused. @section, however, stays in
scope, and therefore, you need to make a copy of the array, and store a
reference to that, and empty the array after that.

Martien

PS. If you provide some real data next time, not much, but just enough,
I might be tempted to write some real code.
-- 
Martien Verbruggen              | Since light travels faster than
Interactive Media Division      | sound, isn't that why some people
Commercial Dynamics Pty. Ltd.   | appear bright until you hear them
NSW, Australia                  | speak?


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

Date: Sun, 09 Sep 2001 10:31:58 GMT
From: Bart Lateur <bart.lateur@skynet.be>
Subject: Re: CR/LF
Message-Id: <epgmpt4tujo756gg19idgadn149s682cdl@4ax.com>

Sean Hamilton wrote:

>I am assuming \r translates directly to CR, but does \n always translate to
>LF? Or does it translate to LF on Unix, CR on Mac, CRLF on Windows? Is it
>identical in behavior to C's \n escape?

Er... Sort of.

In perl, "\n" is always one character, representing the newline. "\r" is
the other character.

In fact, on Unix, DOS and Windows (and similar, such as OS/2), "\n" is
chr(10)="\x0A"="\012". So "\r" is "\x0D"="\015".

Only when printing such a string to a text file (no binmode), on
DOS/Windows, "\n"/"\x0A" gets converted to the"\x0A\x0D" sequence, and
vice versa when reading from a text file.

On a Mac, a plain end of line ("\n") is a bare return, "\x0D", thus "\r"
is "\x0A". So the value of "\n" and "\r" got swapped.

But you use "\n" for the same purpose on Mac and the others: as the line
end marker. You shouldn't really be using "\r" in portable code.

-- 
	Bart.


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

Date: Sun, 9 Sep 2001 14:35:10 +0200
From: "Alan J. Flavell" <flavell@mail.cern.ch>
Subject: Re: CR/LF
Message-Id: <Pine.LNX.4.30.0109091420540.13081-100000@lxplus023.cern.ch>

On Sep 9, Tim Hammerquist inscribed on the eternal scroll:

> Me parece que Sean Hamilton <sh@planetquake.com> dijo:
> > Greetings,
> >
> > I am assuming \r translates directly to CR, but does \n always translate to
> > LF? Or does it translate to LF on Unix, CR on Mac, CRLF on Windows? Is it
> > identical in behavior to C's \n escape?
>
> The perlport manpage discusses '\n' as the "logical" newline, silently
> converting to and from whatever it should be, usually intelligently
> enough.  OTOH, as perlport says, \n cannot always be trusted.
                                      ^^^^^^^^^^^^^^^^^^^^^^^^

I suggest that may have been an unfortunate choice of terms: the
string "trust" doesn't appear anywhere in the copy of perlport that
I'm looking at - though I think I see what you're getting at.  You can
perfectly well _trust_ Perl to behave to specification on this issue:
the important thing is to know what that specification says ;-)

Older versions of Perl documentation showed unfortunate signs in this
area of having been composed by unix bigots.  But recent versions are
more even-handed in this regard.  Unix is quite able to demonstrate
its superiority without any need for bigotry ;-))  - and since Perl
rightly stresses benefits of portability, its documentation clearly
needs to reflect that fact.

> If the
> actual value of \n is important or even in question, it's possible you
> should specify explicitly what you want. ("\012" for newline, "\015" for
> carriage-return).

Indeed.  And as a general rule, if you're working with \n and \r then
you want to open in text mode, whereas if you're working with \012 and
\015 then you want to open with binmode().  However, it's better to
understand what one's doing and why (based for example on that perldoc
perlport page), in preference to applying "as a general rule"
recipes by rote.  IMHO, of course.

good luck



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

Date: Sun, 9 Sep 2001 23:07:34 +1000
From: mgjv@tradingpost.com.au (Martien Verbruggen)
Subject: Re: CR/LF
Message-Id: <slrn9pmqcm.b0q.mgjv@martien.heliotrope.home>

On Sun, 09 Sep 2001 10:31:58 GMT,
	Bart Lateur <bart.lateur@skynet.be> wrote:
> 
> But you use "\n" for the same purpose on Mac and the others: as the line
> end marker. You shouldn't really be using "\r" in portable code.

That is, if you are writing files. If you want to write portable code
that implements many of the RFC protocols, for example, then you
should never use \n, but always specifically the characters that are
proscribed by the protocol. In many cases that is \015\012 for an
"end-of-line". Not \r\n or simply \n, although both might work correctly
on some (different) platforms.

Unix ttys also have some special dealing with these things.

Martien
-- 
Martien Verbruggen              | 
Interactive Media Division      | Useful Statistic: 75% of the people
Commercial Dynamics Pty. Ltd.   | make up 3/4 of the population.
NSW, Australia                  | 


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

Date: Sun, 09 Sep 2001 07:31:54 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Disappearing Compiler messages
Message-Id: <3B9B532A.5B2BB11A@earthlink.net>

David F Dafoe wrote:
> 
> A few things changed recently in my .PL files hosted on an NT server
> at a large corporation.
> 
> You're supposed to put this line:
> 
> print "Content-type:text/html\n\n";
> 
> at the beginning of each file.  This used to work and still works on
> other servers.  Recently, this line started to cause my scripts to
> stop running, so I had to comment them out.
> 
> Also, I no longer get compiler error messages.  If there is any syntax
> error I get a message saying "script produced no output."  I have to
> laboriously remove all new code, and put it back in line by line until
> I find the offending code.
[snip]

Try putting this somewhere near the top of your script:
	use CGI::Carp qw(fatalsToBrowser);

-- 
"I think not," said Descartes, and promptly disappeared.


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

Date: Sun, 9 Sep 2001 14:37:57 +0200
From: "Alan J. Flavell" <flavell@mail.cern.ch>
Subject: Re: Disappearing Compiler messages
Message-Id: <Pine.LNX.4.30.0109091436390.13081-100000@lxplus023.cern.ch>

On Sep 9, Benjamin Goldberg inscribed on the eternal scroll:

> David F Dafoe wrote:
[...]
> > Also, I no longer get compiler error messages.  If there is any syntax
> > error I get a message saying "script produced no output."  I have to
> > laboriously remove all new code, and put it back in line by line until
> > I find the offending code.
>
> Try putting this somewhere near the top of your script:
> 	use CGI::Carp qw(fatalsToBrowser);

Quite how is this supposed to help with scripts that don't compile?




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

Date: Sun, 09 Sep 2001 09:49:32 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Disappearing Compiler messages
Message-Id: <3B9B736C.B1671CB5@earthlink.net>

Alan J. Flavell wrote:
> 
> On Sep 9, Benjamin Goldberg inscribed on the eternal scroll:
> 
> > David F Dafoe wrote:
> [...]
> > > Also, I no longer get compiler error messages.  If there is any
> > > syntax error I get a message saying "script produced no output." 
> > > I have to laboriously remove all new code, and put it back in line
> > > by line until I find the offending code.
> >
> > Try putting this somewhere near the top of your script:
> >       use CGI::Carp qw(fatalsToBrowser);
> 
> Quite how is this supposed to help with scripts that don't compile?

Read up on the difference between 'use' and 'require', and you'll see.

As long as this use statement occurs before the error, it should work.

-- 
"I think not," said Descartes, and promptly disappeared.


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

Date: Sun, 09 Sep 2001 10:26:15 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Evaluation order of object methods
Message-Id: <3B9B7C07.2D0DDE52@earthlink.net>

Mr. Sunblade wrote:
> 
> <snip>
> 
> > In my example, everything was in the "ARRAY" class, so a line like
> > the above worked perfectly ok, since an anonymous array is always an
> > instance of type ARRAY, even without blessing it.  Your code,
> > however, is in class Set::Array, not ARRAY.  So you need to bless
> > the thing before you return it.
> 
> Lost this thread for a while. :(
> 
> Ah, I see now Benjamin.  Well, uh, unfortunately I was actually
> thinking of trying to make something publishable and I'm afraid that
> if I put this in the package ARRAY I'll be hunted down and stoned to
> death by the perl community since I would be redefining the behavior
> of their arrays,

<grin>  Yeah, that was kinda evil of me to give such a thing as an
example.

> possibly without their knowledge (if they didn't read the docs or
> didn't understand). Or perhaps I'm confused again. :|

No you had it right.

> After tinkering some more, I made a command decision - have
> *everything* return a blessed reference.  This makes life easier at
> the expense of two minor annoyances.  First, I'll have to dereference
> everything, including scalars.  Second, I'll have to use the reftype
> method (vs. ref) to get the underlying type (unless I *want* to the
> class name).  I also decided to just allow '1' be passed to a method
> in order to not modify the object.

Much of perl tries to DWIM.  It's better, IMHO, to look at the context,
and guess what the user wants to do.  The reason this is reasonable is
that when you want to get around this dweomer, you can:
	($a, $b) = (1, scalar foo()); # force scalar context
or
	$x = (() = foo()); # force list context
or
	($x) = foo(); # another way to force list context
or
	do{foo();1} # force void context.

> e.g.
> my $len = $sao->unique()->length(); # Modifies object
> my $len = $sao->unique(1)->length(); # Doesn't modify object

I would do it as, in non-void context, always makes a new object, and in
scalar context, always returns a reference.

my $sao2 = $sao->unique; # makes a copy
my $has_uniques = $sao->length == $sao2->length;
my $has_uniques = $sao->length == $sao->unique->length;
$sao->unique; # changes $sao.

This is because getting a modified copy is, I believe, a more common
operation than changing the object in place.

Oh, and as to the underlying type... remmeber that unlike in C, perl
arrays do not have to be all one type.  You can have an array consisting
of one string, one glob, one blessed CGI object, etc.  What would your
reftype method return for this case?

-- 
"I think not," said Descartes, and promptly disappeared.


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

Date: Sun, 09 Sep 2001 07:22:13 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: fore!
Message-Id: <3B9B50E5.ADF6FF1B@earthlink.net>

Steven M. O'Neill wrote:
> 
> I would like assistance in learning to translate the following to a
> one-liner.  Any code and/or references will be appreciated.
> 
> Steve
> 
> -~-~-
> [dis.pl:]
> 
> open IN, "dis" or die "nope";
> while (<IN>) {
>     chomp;
>     $grepwithme = substr($_, 3);
>     @output = `grep ^${grepwithme}\$ /usr/share/dict/words`;
>     print "$grepwithme " unless (@output);
> }

perl -lne 'BEGIN{ $x{substr <>, 3}=$. until eof }
           delete $x{$_}
           END{ print "@{[sort {$x{$a}<=>$x{$b}}, keys %x]}" }' \
           dis /usr/share/dict/words

Sorry that this isn't *quite* a one-liner, but it's shorter than yours,
without being too obfuscated, and it doesn't run an external process in
doing it.  If you don't care too much about order, you can eliminate the
comparison block to the sort, or even remove the sort entirely.

NB: this is untested.

> [head dis:]
> disability
> disable
> disabled
> disablement
> disabusal
> disabuse
> disacceptance
> disaccharide
> disaccharose
> disaccommodate
> 
> [output:]
> abled ablement abusal accharide accharose

-- 
"I think not," said Descartes, and promptly disappeared.


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

Date: Sun, 09 Sep 2001 10:09:03 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: form input changed from Select to hidden, no errors and no search results
Message-Id: <3B9B77FF.9CBDDFFA@earthlink.net>

JerryGarciuh wrote:
[snip]
>     <input type="hidden" name="case" value="Insensitive ">
[snip]
>             if ($FORM{'case'} eq 'Insensitive') {

In one place you have "Insensitive ", with a space on the end, and in
the other place you have 'Insensitive', with no space on the end.

The two are not equal.

-- 
"I think not," said Descartes, and promptly disappeared.


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

Date: Sun, 9 Sep 2001 14:13:50 +0200
From: "stewie" <y_h@pi.be>
Subject: free guestbook script
Message-Id: <9nfm1l$pp$1@news.planetinternet.be>

Hi,

Does anyone know where i can find a free and easy to install guestbook
script on the net.

The owner of the guestbook should be able to reply to the messages that are
posted, so that his response appears below the posted message.

I hope this isn't a stupid question in this newsgroup, but I'm just not a
programmer.
It is too difficult for me to write this myself or adapt any script.

Thanks for any help

Stewie




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

Date: Sun, 9 Sep 2001 09:41:39 +0000
From: gnari <gnarinn@hotmail.com>
Subject: Re: guestbook
Message-Id: <1000028499.829896772280335.gnarinn@hotmail.com>

In article <9ndnf6$tap$1@news.planetinternet.be>, stewie <y_h@pi.be> wrote:
>I've downloaded the perl code for a guestbook
>
>It works fine, but it needs one more thing:
>The owner of the guestbook should be able to give a reaction on the
>guestboard true the admin tool.
>
>But i don't have a clue how to fix that.
>
>Is there someone who can help me out?
>I've attached the scipts in a ZIP-file

(zip file snipped)

some friendly advice:

this is a not the best way to get help from this newsgroup.
this group is for discussions of perl, not a help desk.
this is not a binary newsgroup
many readers here are not PC users, and may have problems
unpacking a ZIP-file
you have not shown that you have made any attempts yourself
to solve your problem

you do not say where you downloaded this guestbook.
have you contacted its authors? read its documentation?
if you had said what package this is, maybe some
reader familiar with it could give some opinion or point at some
information. or pointed at some other package that did what you want

it is better to supply an url when you want to refer to
a whole package like this.
it is even better if you you described what parts you have
problems with, told us what you have tried to do, and how the results
differed from what you expected. then i am sure you would get help.

now, try again, and hope you have not been killfiled by those
who might be able to help you.

gnari


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

Date: Sun, 9 Sep 2001 14:03:44 +0200
From: "stewie" <y_h@pi.be>
Subject: Re: guestbook
Message-Id: <9nflen$dh$1@news.planetinternet.be>

I have tried to study the code, but it is too complex for me.
I found out that it is possible to add text in the data-file manually.
But to built in a function in the admin tool is too complex for me.

The URL where I downloaded is http://www.cougasoft.org/guestbook/index.html
It is called guestbook 4.8.

The guestbook is free to use, as long as one leaves the copyright notice on
top.

If you can help me, I'd like that.
If you won't or can not help me, that is OK too.

Or: if you know where i can find a script on the net for a guestbook in
which the guestbook
owner can reply, i would be happy if you told me.

Thanks
Stewie


"gnari" <gnarinn@hotmail.com> wrote in message
news:1000028499.829896772280335.gnarinn@hotmail.com...
> In article <9ndnf6$tap$1@news.planetinternet.be>, stewie <y_h@pi.be>
wrote:
> >I've downloaded the perl code for a guestbook
> >
> >It works fine, but it needs one more thing:
> >The owner of the guestbook should be able to give a reaction on the
> >guestboard true the admin tool.
> >
> >But i don't have a clue how to fix that.
> >
> >Is there someone who can help me out?
> >I've attached the scipts in a ZIP-file
>
> (zip file snipped)
>
> some friendly advice:
>
> this is a not the best way to get help from this newsgroup.
> this group is for discussions of perl, not a help desk.
> this is not a binary newsgroup
> many readers here are not PC users, and may have problems
> unpacking a ZIP-file
> you have not shown that you have made any attempts yourself
> to solve your problem
>
> you do not say where you downloaded this guestbook.
> have you contacted its authors? read its documentation?
> if you had said what package this is, maybe some
> reader familiar with it could give some opinion or point at some
> information. or pointed at some other package that did what you want
>
> it is better to supply an url when you want to refer to
> a whole package like this.
> it is even better if you you described what parts you have
> problems with, told us what you have tried to do, and how the results
> differed from what you expected. then i am sure you would get help.
>
> now, try again, and hope you have not been killfiled by those
> who might be able to help you.
>
> gnari




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

Date: Sun, 09 Sep 2001 06:15:49 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: help with this script-database column names
Message-Id: <3B9B4155.E63BDA71@earthlink.net>

gnarasi wrote:
> 
> Please help out with this script. It works fine , all columns are
> printed, but not the column names(headers). How are the column names
> retrieved and printed? I am learning this and any help is welcome.
> Thanks
> 
> ++++++++++
> #!/usr/bin/perl
>  print "Content-type: text/html\n\n";
> use DBI;
> use CGI;

Use warnings, use strict.  Don't use CGI unless you need it.

#!/usr/bin/perl -w
use strict;
use DBI ();

print "Content-type: text/html\n\n";

> 
> { $dbh = DBI->connect("dbi:mysql:gldb", 'root', '' ,
> { RaiseError => 1 });

You don't need to put this in a block... so lose first "{"

my $dbh = DBI->connect( "dbi:mysql:gldb", "root", "",
	{ RaiseError => 1 } );

> 
> $sth = $dbh->prepare("SELECT * FROM cia");

insert a "my" for compliance with strict:
my $sth = $dbh->prepare("SELECT * FROM cia");

> 
> $sth->execute();
> 
> @dbRows = $sth->fetchall_arrayref();

First, fetchall_arrayref returns not an array, but a single arrayref,
which contains all the rows.

Second, it will be more memory efficient, to use fetchrow to get each
row as you need it, than to fetchall and store them.  So eliminate that
line and change to the code way down below.

> {

Here's another brace that could be eliminated.

>         print qq!
>         <HTML>
>         <HEAD>
>         <TITLE>SQL Statement
>                      Results</TITLE>
>         </HEAD>
>         <BODY BGCOLOR = "#FFFFFF"
>                  TEXT = "#000000">
>         <CENTER>
>         <TABLE BORDER = "1">!;

Here's where you want to put in the column names, I suppose.

	print "<TR>";
	print "<TH>", $_, "</TD>" for @{$sth->{NAME};
	print "</TR>";

Using alternate quotes is good when you have ' or " in the data, but
normal quotes are often just as readable, or more readable.

> foreach $rowReference (@dbRows)
>             {
>             foreach $columnReference
>                     (@$rowReference)
>                 {
>                 print qq!<TR>!;
>                 foreach $column
>                         (@$columnReference)
>                     {

You would only need two levels of loop, not three, if you'd realized
what the return value of fetchall_arrayref was.

>                     print qq!<TD>
>                              $column
>                              </TD>\n!;
>                     }
>                 print qq!</TR>!;
>                 }
>             }

	while( my $row = $sth->fetchrow_arrayref ) {
		print "<TR>";
		print "<TD>", $_, "</TD>" for @$row;
		print "</TR>";
	}


>         print qq!
> 
>         </TABLE>
>         </CENTER>
>         </BODY>
>         </HTML>!;
>         exit;
> 
>         }
> }

These two braces match up with the ones I suggested for you to get rid
of.  If you get rid of them there, then you must get rid of them here.

-- 
"I think not," said Descartes, and promptly disappeared.


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

Date: Sun, 09 Sep 2001 07:44:28 -0400
From: Bernie Cosell <bernie@fantasyfarm.com>
Subject: Re: How can I find the PID's of my children?
Message-Id: <a2lmptoicjmkoqe5h77drasgsfsbslavlu@news.supernews.net>

stanb@panix.com (Stan Brown) wrote:

} However, I'm going to leave the user the option of canceling from an "exit"
} button on the main window. So I need to be able to send a kill() signal to
} my children.
} 
} Short of making these PID's global variables, when they are spwned, how can
} I detrmine what children, my runing perl process is the parent of?

Well, ignoring matters of whether this is the right way to have organized
the program, when I needed to do this I used /proc.  /proc isn't portable
to all OSs, but if your OS _has_ /proc, then *this* part of it should be
pretty standard:

# Given a proc no, return a list of all processes which have
## that proc as the parent.  [that is, find all children of the given proc]
sub children
{
    my $parent = $_[0] ;
    my @children ;
    opendir(P, "/proc") or return () ;
    while (defined($_ = readdir P))
    {
        next if /\D/ ;
        my $ppid = parent($_) ;
        push @children, $_ if $ppid && $ppid == $parent ;
    }
    closedir P ;
    return @children ;
}

# Return the parent pid of a process
sub parent
{
    my $pid = $_[0] ;
    open (S, "/proc/$pid/stat") or return undef ;
    my $ppid = (split / /, <S>)[3] ;
    close S ;
    return $ppid ;
}

[as you can tell, in my case I didn't need to find *MY* processes children,
but I was looking for the children of some other process]

  /Bernie\
-- 
Bernie Cosell                     Fantasy Farm Fibers
bernie@fantasyfarm.com            Pearisburg, VA
    -->  Too many people, too few sheep  <--          


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

Date: Sun, 09 Sep 2001 08:01:18 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: object & fork
Message-Id: <3B9B5A0E.D66AEEDB@earthlink.net>

Steffen Bachmann wrote:
[snip]
> Thanks for help, I'm using open() to establish the interprocess
> communication and
> it's working fine:
> 
> package myProcess;
> 
> sub new{
>         my $class = shift;
>         my $self = {};
>         $self->{'result'} = '?';
>         bless($self, $class);
>         };

There's no reason to store anything into $self->{result}, since it never
gets used before we call method result.

> 
> sub start{
>         my ($self,$cmd) = @_;
>         $SIG{'CHLD'} = sub{wait;};
>         $self->{'pid'} = open("PIPE_$self","$cmd |") || "Can't fork:
> $!\n";
>         return($self->{'pid'});
>         };

Eww, yuck!  Using string filehandles is *so* perl4, though the way you
use a stringified object reference to create a unique filehandle name is
kinda cool.

If you have perl 5.6, then code it like this:
	sub start {
		my ($self, $cmd) = @_;
		my $pid = open(my ($fh), "-|", $cmd);
		die "Can't fork: $!" if !$pid;
		@$self{qw(cmd pid fh)} = ($cmd, $pid, $fh);
	}

If you only have perl 5.005, then something like the following works:

	sub start {
		my ($self, $cmd) = @_;
		my $pid = open(local (*fh), "-|", $cmd);
		die "Can't fork: $!" if !$pid;
		@$self{qw(cmd pid fh)} = ($cmd, $pid, \*fh);
	}

Actually, the above *might* even work under perl4.  I'm not sure,
though.

> 
> sub result{
>         my $self = shift;
>         my $pipe ='PIPE_'.$self;
>         my $r = '';
>         while(<$pipe>){$r = $r.$_;};
>         $self->{'result'} = $r;
>         return($self->{'result'});
>         };

There's a reason we have $/, so we can slurp in a bunch of data:
	sub result {
		my $self = shift;
		my $fh = $self->{fh};
		return $self->{result} if exists $self->{result};
		$self->{result} = do { local $/; <$fh> };
		if( waitpid $self->{pid}, 0 ) {
			my ($sig, $ret) = ($?%256, $?/256);
			my $cmd = $self->{cmd};
			die "$cmd died from signal $sig" if $sig;
			die "$cmd exited with code $ret" if $ret;
		} else {
			warn "waitpid: $!";
		}
		close $fh;
		delete $self->{qw(pid fh)};
		return $self->{result};
	}

Note that I *don't* have a $SIG{CHLD} thing in start().  I don't need it
because I have waitpid in result().

> 
> sub cleanup{
>         my $self = shift;
>         my $r = $self->{'pid'};
>         close("PIPE_$self");
>         kill('SIGTERM',$self->{'pid'});
>         delete $self->{'pid'};
>         return($r);
>         };

Some changes, including a name change:

	sub DESTROY {
		my $self = shift;
		return if !keys %$self;
		close $self->{fh};
		my $pid = $self->{pid};
		my $cmd = $self->{cmd};
		%$self = ();
		return if !$pid;
		kill TERM => $pid;
		if( waitpid $pid, 0 ) {
			my ($sig, $ret) = ($?%256, $?/256);
			die "$cmd died from signal $sig" if $sig;
			die "$cmd exited with code $ret" if $ret;
		} else {
			warn "waitpid: $!";
		}
	}

Due to the name change, you don't *have* to call the cleanup function
explicitly, you can just undef each variable which has a reference to
the object.  However, due to test on the second line, there's no harm if
you call DESTROY yourself.

> 
> 1;

Well, this is one line of your code which doesn't have to change :)

-- 
"I think not," said Descartes, and promptly disappeared.


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

Date: 9 Sep 2001 06:42:32 -0700
From: mirvine@compsoc.com (mirvine555)
Subject: Re: Perl objects problems
Message-Id: <9ef8bd6b.0109090542.1f5f53f6@posting.google.com>

Quoting Tassilo von Parseval <tassilo.parseval@post.rwth-aachen.de>:

>
> Yes, you could do this. Moreover, you could completely erase
> Insect->new. Since Critter is the baseclass, you could put the
> constructor into this class. It would automatically call Insect->_init
> then.
> And following this, Critter does not need an _init class.
> 
> In the current implementation it is a little weird. You call:
> Insect->new   # calls
> Critter->new  # calls
> Insect->_init 
> 
> Just remove Insect->new. Inheritance will take care that Critter->new
> is
> automatically called when you invoke Insect->new. That's because Perl
> cannot find a new method in the Insect-class and hence starts to scan
> the available base-classes. In this case Critter.
> 
> So the scheme is the following:
> 
> base-class Critter:
>     implements new()
> 
> class Insect is a subclass of Critter:
>     implements _init()
>     ...
>     
> class Spider is a subclass of Critter:
>     implements _init()
>     ...
> 
> ...
> 
> Thus you can build a whole class-hierarchy with only one constructor.
> An
> easy rule: One constructor for all and each sub-class has its own
> init-method whereas the base-class does not need one since it (often)
> is
> abstract and hence should not be instantiated.
> 

It's all making a little more sense now, and it sounds good, but what
about the 'empty subclass' test. Shouldn't I be able to do:

(Full Insect.pm module shown)
package Insect;

use strict;
use vars qw(@ISA);
use lib qw(./);
use Critter;
@ISA=('Critter');

1;


And create Insects with:
my $insect=Insect->new;

I won't have the Insect->name method, but the perldocs say to do it
that way, and then add any extra methods as required. I started out
like this, added the Insect->name method, then tried to initialize it,
which is when I got lost.

from perltoot
       Setting up an empty class like this is called the "empty
       subclass test"; that is, making a derived class that does
       nothing but inherit from a base class.  If the original
       base class has been designed properly, then the new
       derived class can be used as a drop-in replacement for the
       old one...

Thanks,
Mark


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

Date: Sun, 9 Sep 2001 11:46:22 +0100
From: "Gareth Horth" <Gareth@Horth.F9.co.uk>
Subject: Reverse DNS lookup
Message-Id: <gQHm7.15352$NE.1056793@wards>

Hi,

How can I do a reverse DNS lookup from a perl script?
If I do 'whois 1.2.3.4' on the CGI server it doesn't find a match - but when
I do the same on my computer it works.
Is there another way to do the lookup?

Thanks in advance,

Gareth




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

Date: Sun, 09 Sep 2001 10:35:38 GMT
From: Bart Lateur <bart.lateur@skynet.be>
Subject: Re: running ActivePerl on W2k Prof
Message-Id: <nahmptk8gnvk394a35j338rtdh51bm2j4j@4ax.com>

The Executioner wrote:

>I'm trying to run a perl script on my computer.
>I have got Windows 2000 Prof.
>I have installed ActivePerl-5.6.1.628-MSWin32-x86-multi-thread.msi
>
>Where is the cgi-bin, or where can I define the cgi-bin?

You seem to want to run a CGI script, not just a perl script, so you
need a web server as well.

If you had gotten IndigoPerl (from <http://www.indigostar.com>) instead,
it would have installed an Apache webserver at the same time, complete
with cgi-bin. IndigoPerl is compatible with ActivePerl.

-- 
	Bart.


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

Date: Sun, 09 Sep 2001 10:22:32 GMT
From: Bart Lateur <bart.lateur@skynet.be>
Subject: Re: Simple regexp
Message-Id: <8lgmptc5ab8dsvcdhrnmcv26bjgtetrdi0@4ax.com>

Scott wrote:

>we have the following output piped in from a unix program:
>12312      /home/this
>3234        /home/that
>12213     /var/horses
>
>#I need an expression here that will make $_  just the first number
>field

If you task is that simple, I'd just split on whitespace. (See the
perlfunc entry for split())

-- 
	Bart.


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

Date: Sun, 09 Sep 2001 10:20:30 GMT
From: Bart Lateur <bart.lateur@skynet.be>
Subject: Re: Why doesn't this regex work?
Message-Id: <rcgmpts6pd0ai55l7tm9oeu6opsb1li9v7@4ax.com>

Martien Verbruggen wrote:

>$data = pack "a20", $data;

	$data = unpack "a20", $data;

This one does the right thing with shorter strings, too.

-- 
	Bart.


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

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 1718
***************************************


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