[22330] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 4551 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Feb 11 18:06:15 2003

Date: Tue, 11 Feb 2003 15:05:12 -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, 11 Feb 2003     Volume: 10 Number: 4551

Today's topics:
    Re: [Regex] Removing lines not containing a substring <abigail@abigail.nl>
    Re: [Regex] Removing lines not containing a substring (Allan Cady)
    Re: ActivePerl distro. whatNext (samphdauto)
    Re: best Perl book for a newbi (Nataku)
    Re: building hash <tassilo.parseval@post.rwth-aachen.de>
    Re: building hash <eric.ehlers@btopenworld.com.nospam>
    Re: building hash <peakpeek@purethought.com>
        Calling multiple versions of compress in one program <spamhater47+U030211175851@grymoire.com>
    Re: Can you run linux and perl on a floppy ? <jpagnew@vcu.edu>
    Re: Can you run linux and perl on a floppy ? <jpagnew@vcu.edu>
    Re: can't locate Mail/MboxParser.pm in @INC (Randy Kobes)
    Re: Controlling order of precedence <mgjv@tradingpost.com.au>
    Re: createing a daemon (Anno Siegel)
    Re: createing a daemon <abigail@abigail.nl>
    Re: DBD::mysql not loading mysql.dll (Randy Kobes)
    Re: Does anyone know if ... <bart.lateur@pandora.be>
        Fork multiple children, read from their pipes (maybe)
    Re: Fork multiple children, read from their pipes (Anno Siegel)
    Re: Fork multiple children, read from their pipes <noreply@gunnar.cc>
    Re: Insecure Filehandle Dependencies <tassilo.parseval@post.rwth-aachen.de>
    Re: Insecure Filehandle Dependencies <flavell@mail.cern.ch>
    Re: Paging output (Anno Siegel)
    Re: Paging output <goldbb2@earthlink.net>
        PCRE stumper (PHP preg_match_all) <thisis@bogus.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 11 Feb 2003 20:38:25 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: [Regex] Removing lines not containing a substring
Message-Id: <slrnb4inq1.qnk.abigail@alexandra.abigail.nl>

Allan Cady (allancady@yahoo.com) wrote on MMMCDLI September MCMXCIII in
<URL:news:d563b154.0302102334.969afcf@posting.google.com>:
,,  The script below removes all lines from the source data that don't
,,  begin (after whitespace) with '<td'.  It takes four substitutions to
,,  accomplish this.  I'm wondering if there's a way to do this in one
,,  substitution?

    s/^(?!\s*<td).*\n//;

,,  More generally, can you match lines that _don't_ contain a specified
,,  substring?  Or even more general, can you match any string based on
,,  "anything but this substring"?

This matches strings that don't contain 'PAT':

    /^(?:(?!PAT).)*$/s;

,,                                 What I do below depends on the '<td'
,,  being the first non-whitespace in the line... what if I were looking
,,  for a substring that could be at any position in the line?


If you're looking for an exact substring, use index(). If you're
coding yourself, just use the '!~' operator. But if you can only
supply a regex, use the form I presented above.


Abigail
-- 
sub f{sprintf$_[0],$_[1],$_[2]}print f('%c%s',74,f('%c%s',117,f('%c%s',115,f(
'%c%s',116,f('%c%s',32,f('%c%s',97,f('%c%s',0x6e,f('%c%s',111,f('%c%s',116,f(
'%c%s',104,f('%c%s',0x65,f('%c%s',114,f('%c%s',32,f('%c%s',80,f('%c%s',101,f(
'%c%s',114,f('%c%s',0x6c,f('%c%s',32,f('%c%s',0x48,f('%c%s',97,f('%c%s',99,f(
'%c%s',107,f('%c%s',101,f('%c%s',114,f('%c%s',10,)))))))))))))))))))))))))


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

Date: 11 Feb 2003 13:03:48 -0800
From: allancady@yahoo.com (Allan Cady)
Subject: Re: [Regex] Removing lines not containing a substring
Message-Id: <d563b154.0302111303.6b69d0f4@posting.google.com>

Thanks folks for the answers.

I figured there are a zillion other ways to accomplish what I want
(TMTOWTDI).   index() is new to me and looks very useful.  But I was
looking for an answer using this specific approach.  Sounds like it
may not exist.

> That's the !~ operator.  It is simply a negated match.

But it only negates the return value (true/false), not the
"side-effects" (i.e. the substitution), if I understand it correctly. 
Not much help unless I'm processing line-by-line.

> Everything else aside, these substitutions result in a lot of
> string copying.

Yes, that's why I hoped to consolidate it into one statement.

> Just work on the file line by line.

I'm also doing some substitutions that span lines (see my previous
posts), so I already have the file loaded in as a single string. 
Hence my preference to let the regex engine do the work for me.

Anyway, thanks for the help.

Allan


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

Date: 11 Feb 2003 13:06:08 -0800
From: samj@eisa.net.au (samphdauto)
Subject: Re: ActivePerl distro. whatNext
Message-Id: <65050d4c.0302111306.5b59e8a7@posting.google.com>

helgi@decode.is (Helgi Briem) wrote in message news:<3e43987d.760704004@news.cis.dfn.de>...
> On 6 Feb 2003 10:39:39 -0800, samj@eisa.net.au (samphdauto)
> wrote:
> 
> >> If you do not know the difference then I suggest you invest in a book
> >> tailored to beginner programmers in general.
> >
> >any recommendation for this, and for Perl in specific? 
> 
> Learn how to use the Perl documentation installed
> on your own computer, probably in C:/Perl/html/index.html
> and accessible from the command line using perldoc.
> 
> perldoc -q books
> 
> Also:
> http://www.perldoc.com/perl5.8.0/pod/perlfaq2.html#Perl-Books
> 
> 
> >and oh.. is there a Perl newsgroup that is very fast in responds, 
> 
> You're treading very close to a lot of killfiles here.
> There are human beings responding to your posts,
> not robots.  Show a little patience.
> 

sorry if I gave the wrong impression. what I realy ment is my post
takes too lont to appear in google not trying to rush a response at
all. but I understand not that it could be my ISP.
thanks
Sam
> >google takes too long just to post original posts.?
> 
> Then use a real news client.  Google is an archive,
> not a news server.  I suggest Forte Free Agent.
> 
> >so the packages is programs written to be executed by perl.ext. is do
> >you recommend some sites which has some free & usfull downloads
> >instead of writing my own from scratch?
> 
> You're going to have to make up your mind. Do you want
> to learn to use Perl or not?  If not, this is the wrong 
> place for you.  I suggest http://nms-cgi.sourceforge.net/ 
> if free scripts are all you want.


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

Date: 11 Feb 2003 11:53:17 -0800
From: Crapnut566@yahoo.com (Nataku)
Subject: Re: best Perl book for a newbi
Message-Id: <7e48fc99.0302111153.78d9442f@posting.google.com>

I would definitly go for the llama ... aka Learning Perl by O'reilly. 
Programming Perl covers more, but doesnt offer as smooth of an
introduction to the language as the llama does, plus those advanced
features you dont use all that often.

ktb <xyf@nixnotes.org> wrote in message news:<slrnb4h3l6.djq.xyf@scab.nixnotes.org>...
> In article <65050d4c.0302102010.5225f1e8@posting.google.com>, samphdauto wrote:
> > Hello
> > I come from a vb backgound and want to learn Perl.. what is the latest
> > book you recommned me to start with. the version of perl I have is
> > 5.8.0 which is the latest 'I think'.
> 
> Running the command -
> perldoc -q book
> 
> Will net you a URL -
> http://www.perl.com/perl/critiques/index.html
> 
> This site has a book list as well -
> http://learn.perl.org/
> hth,
> kent


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

Date: 11 Feb 2003 19:31:17 GMT
From: "Tassilo v. Parseval" <tassilo.parseval@post.rwth-aachen.de>
Subject: Re: building hash
Message-Id: <b2bj25$e3e$1@nets3.rz.RWTH-Aachen.DE>

Also sprach Janek Schleicher:

> On Tue, 11 Feb 2003 13:37:01 +0000, Tassilo v. Parseval wrote:
> 
>> Also sprach Janek Schleicher:
>> 
>>> ...
>> 
>> The thread reminds me of an idea I had been mind-toying with lately.
>> Writing a module (with a tie() interface) that makes all Perl data-types
>> store the data zlib-compressed. It could be done in XS to gain some
>> speed but it's probably less trivial than it sounds since on a an
>> ordinary FETCH the data would need to be decompressed and could thusly
>> explode your memory.
> 
> Great idea!
> 
> Perhaps you don't need to compress everything. I would suggest to compress
> on block base, while a compressed block is assigned to some hash keys (or
> % a number). If that would be chunks of perhaps 1024 byte, the compression
> would be not perfect, but at least reducing the size by more than 50%
> (assuming large textural data). Then you only would need a normal hash or
> similar to remember what blocks are assigned to what hash keys.
>
> I'm afraid my explanation is confusing. But the main idea is simply to
> compress not all together, but in seperated blocks that can be
> distinguished by e.g. the hash value. That would have also the advantage
> that it could be used efficient in RAM as also on DISK.

I think I did understand it roughly. The above would make a lot of sense
if Perl had a more sophisticated tie() interface. It is particularly
incomplete for scalars, I think. Whenever you do

    $tied_scalar .= "some string";

this results in a FETCH and STORE operation. But instead, an APPEND is
needed here (where you could use the above block structure to append to
the last block only), otherwise you temporarily need the amount of memory
required for the uncompressed data. Also, a lot of string operators
should be tieable. A tied interface really should have a SUBSTR(),
CHOP(), CHOMP() etc. method to make tying scalars more useful that it is
right now.

> Looking forward to such a module,

The more I think about it, the more I feel that such a thing is
currently not possible via tie(). :-( I see a slight chance for arrays
and hashes however, since they offer a more sophisticated interface.

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, 11 Feb 2003 20:34:18 +0000 (UTC)
From: "eric" <eric.ehlers@btopenworld.com.nospam>
Subject: Re: building hash
Message-Id: <b2bmoa$5oc$1@sparta.btinternet.com>

> If you know the format of the ID strings, you can probably get the hash
> keys to take up less memory.  For example, if the ID string is *always*
> a letter followed by 9 digits, then you can do:

??
in his sample data, all of the ID strings consist of a letter followed by 7
digits - i would imagine that the ID string is *never* a letter followed by
9 digits.
below you're packing the digits using the "N" template, which will work for
any ID string which consists of a letter followed by an unsigned long less
than 2^32.

> while(<IN>) {
>    $string{$a.pack 'N', $2} = $3 if /(.)(.*?)\t(.*)/;
> }

i think you mean $1 in place of the $a above.

-eric




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

Date: Tue, 11 Feb 2003 22:45:19 +0000
From: Sharon Grant <peakpeek@purethought.com>
Subject: Re: building hash
Message-Id: <h2vi4vo56dml58h25ob9brma05im4uoll5@4ax.com>

On Tue, 11 Feb 2003 12:43:16 +0100, in comp.lang.perl.misc, "Janek Schleicher" <bigj@kamelfreund.de> wrote:

>Another is to use the quick Berkeley DB via the Perl module
>Tie::DB_File

What does that do?

The DB_File module comes with Perl and already has
an interface for tie-ing a Berkeley/DB file to a
Perl hash
-- 
Sharon


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

Date: 11 Feb 2003 22:37:34 GMT
From: Bruce Barnett <spamhater47+U030211175851@grymoire.com>
Subject: Calling multiple versions of compress in one program
Message-Id: <b2btve$jeo$0@208.20.133.66>


I'm comparing different compression algorithms in one program.
Example code:
---------------------
#!/local/bin/perl -w

use strict;
use Compress::Zlib qw(compress);
use Compress::LZF;

sub C1 {
    my ($arg) =@_;
    return Compress::Zlib::compress($arg,"Z_BEST_COMPRESSION");
}
sub C2 {
    my ($arg) =@_;
    return Compress::LZF::compress($arg);
}

my $a = "aaaaaaaaaaaaaaaaaaaaaaaaa";
printf("%d %d %d\n",
          length($a),
          length(C1($a)),
          length(C2($a)));
--------------------------------

When I run this, I get:

Subroutine main::compress redefined at /local/lib/perl5/5.8.0/Exporter.pm line 59.
 at /tmp/com.pl line 5
Prototype mismatch: sub main::compress ($;$) vs ($) at /local/lib/perl5/5.8.0/Exporter.pm line 59.
 at /tmp/com.pl line 5
25 36 10


Anyway to eliminate the errors? Thanks.
I can use eval, I suppose...

-- 
Sending unsolicited commercial e-mail to this account incurs a fee of 
$500 per message, and acknowledges the legality of this contract.



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

Date: Tue, 11 Feb 2003 16:47:44 -0500
From: Jim Agnew <jpagnew@vcu.edu>
Subject: Re: Can you run linux and perl on a floppy ?
Message-Id: <3E496F80.97FA9F59@vcu.edu>

well...... you can find and run the LOAF... Linux On A Floppy.

I *don't* think you can get perl on a floppy, or either on the second
floppy... ;-)

I'm afraid you're SOL here...

Jim

Jay Tilton wrote:
> 
> "Andrew Rich" <andrew.rich@bigpond.com> wrote:
> 
> :  Can you run linux and perl on a floppy ?
> 
> Anybody else smell an X-Y problem?
> 
> Why would you want to do this?


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

Date: Tue, 11 Feb 2003 16:49:06 -0500
From: Jim Agnew <jpagnew@vcu.edu>
Subject: Re: Can you run linux and perl on a floppy ?
Message-Id: <3E496FD2.33E7D70E@vcu.edu>

hhmm.. I stand corrected!!!! 8-D

j.

Ingo Wichmann wrote:
> 
> Andrew Rich schrieb:
> >  Can you run linux and perl on a floppy ?
> 
> http://trinux.sourceforge.net/
> 
> maybe.
> 
> Ingo


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

Date: 11 Feb 2003 19:07:43 GMT
From: randy@theoryx5.uwinnipeg.ca (Randy Kobes)
Subject: Re: can't locate Mail/MboxParser.pm in @INC
Message-Id: <slrnb4ii38.q93.randy@theoryx5.uwinnipeg.ca>

On Sun, 9 Feb 2003 23:34:08 +0100, David Bryson <dbryson@gmx.de> wrote:
[ .. ]
>> Tassilo v. Parseval" <tassilo.parseval@post.rwth-aachen.de> wrote in
>> message news:b26idl$cjf$1@nets3.rz.RWTH-Aachen.DE...
[ .. ]
>> >
>> > Simply reinstall from CPAN and it should work again. The module isn't
>> > overly complex so it should all work seamlessly.
>
>Thanks for ur post. I tried and got the  errors mentioned.

When installing from CPAN.pm, and assuming you're following
the prerequisite recommendations, did the tests for the
prerequisites pass? If they don't, the package won't
get installed - you would have to do a 'force install'.

-- 
best regards,
randy kobes


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

Date: Tue, 11 Feb 2003 22:10:46 GMT
From: Martien Verbruggen <mgjv@tradingpost.com.au>
Subject: Re: Controlling order of precedence
Message-Id: <slrnb4it75.se5.mgjv@verbruggen.comdyn.com.au>

On Tue, 11 Feb 2003 11:00:04 GMT,
	Bart Lateur <bart.lateur@pandora.be> wrote:
> Martien Verbruggen wrote:
> 
>>$ perl -MO=Terse,exec -e '$result =  (($a+$b) + (($c+$d)))'
>>OP (0x80fc088) enter
>>COP (0x8103000) nextstate
>>SVOP (0x80f9308) gvsv  GV (0x80ffeac) *a
>>SVOP (0x80f9328) gvsv  GV (0x80ffe94) *b
>>BINOP (0x8102d38) add [1]
>>SVOP (0x8102480) gvsv  GV (0x80fff00) *c
>>SVOP (0x8172bf8) gvsv  GV (0x80ffec4) *d
>>BINOP (0x8102ca0) add [2]
>>BINOP (0x80f9378) add [3]
>>SVOP (0x8102d60) gvsv  GV (0x80fff0c) *result
>>BINOP (0x80f92e0) sassign
>>LISTOP (0x80fc038) leave [1]
>>-e syntax OK

> I'm new to this... I am familir with how FORTH works, so your
> explanation is what I could have given blindfolded. What I don't get is
> what those digits between brackets are on the lines that contain the
> "add" opcode. A real stack wouldn't need them. So, what are they? Is
> this actually a stack, or are those "register" numbers?

The documentation for the whole B:: tree is rather limited, and so is
the documentation for what ops do and are in Perl [1]. What gets
printed is the return value of B::OP::targ, which returns the op_targ
member of an OP struct, which is defined in Perl's op.h, and is of
type PADOFFSET.

The only "documentation" of what this flag is supposed to mean (at
leats, as far as I can find it) is in perlintern.pod (the sources
themselves are remarkably quiet on the meaning of things), which says:

    Only my/our variable (SVs_PADMY/SVs_PADOUR) slots get valid names.
    The rest are op targets/GVs/constants which are statically
    allocated or resolved at compile time.  These don't have names by
    which they can be looked up from Perl code at run time through
    eval"" like my/our variables can be.  Since they can't be looked
    up by "name" but only by their index allocated at compile time
    (which is usually in PL_op->op_targ), wasting a name SV for them
    doesn't make sense.

So I guess, that it really is just a "unique" name for that particular
op. Judging from the output of B::Terse, it seems that it is simply a
monotonically increasing number that is assigned to ops, according to
execution order although, since it's assigned at compile time, this is
not necessarily correct.

If, instead of B::Terse, you use B::Debug, you get lots more
information, even though, generally, most of that is of little
interest.

Martien

[1] There simply isn't much consolidated information about how perl
works, internally. It is spread out over the sources, and various
documents in the standard documentation set, and working out how to
read them, and in which order is a bit challenging :). My
understanding is getting better, although it is still far from
complete, and started when I read /Extending and Embedding Perl/ by
Tim Jenness and Simon Cozens. At least now I can read most of
perlguts, perlintern, perlhack and parts of the sources without
getting a migraine. :)
-- 
                        | 
Martien Verbruggen      | Little girls, like butterflies, need no
Trading Post Australia  | excuse - Lazarus Long
                        | 


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

Date: 11 Feb 2003 19:56:41 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: createing a daemon
Message-Id: <b2bkhp$k81$2@mamenchi.zrz.TU-Berlin.DE>

ryan  <ryan@dctnet.net> wrote in comp.lang.perl.misc:
> Hi, 
> 	I was wondering if anyone can point me to some information about
> creating a daemon with perl.  I want it to run from the init.d and be

Make sure the volume with perl on it is mounted.  (It often is, but
init processes can be tricky that way.

[...]

Anno


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

Date: 11 Feb 2003 20:24:51 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: createing a daemon
Message-Id: <slrnb4in0j.qnk.abigail@alexandra.abigail.nl>

ryan (ryan@dctnet.net) wrote on MMMCDLI September MCMXCIII in
<URL:news:pan.2003.02.11.13.19.09.119902.20427@dctnet.net>:
--  Hi, 
--  	I was wondering if anyone can point me to some information about
--  creating a daemon with perl.  I want it to run from the init.d and be
--  able to use the start|stop|restart options.  It will just proccess a
--  queue file, so it won't be all that complex. Any help would be greatly
--  appreciated.  I don't really want to have it run as a cron job, as it
--  will not be as robust as I would like it to be.  Thanks again.


perlipc would be your first source of information. After that,
I strongly suggest "Advanced Programming in the UNIX Environment".

Often, daemons started from init.d leave a "pid file" behind,
storing the PID of the daemon. This can be used to avoid having
multiple instances of the same daemon running, but also when 
shutting down. Typically, a daemon is shut down by sending it
a signal. The restart option is sometimes implemented by sending
the daemon a signal (SIGHUP for instance, or SIGUSR1), and 
sometimes by just stopping and starting it.

Nothing of the above is typical Perl, you'd do similar steps for
non-Perl daemons.


Abigail
-- 
perl -e '* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
         / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / 
         % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %;
         BEGIN {% % = ($ _ = " " => print "Just Another Perl Hacker\n")}'


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

Date: 11 Feb 2003 19:16:00 GMT
From: randy@theoryx5.uwinnipeg.ca (Randy Kobes)
Subject: Re: DBD::mysql not loading mysql.dll
Message-Id: <slrnb4iiip.q93.randy@theoryx5.uwinnipeg.ca>

On 11 Feb 2003 09:23:17 -0800, Allen <umayxa3@yahoo.com> wrote:
>On a Microsoft box, I've got ActivePerl installed and I've installed
>DBD::mysql through ppm.
>I installed DBD::mysql through this command:
>ppm> install http://theoryx5.uwinnipeg.ca/ppms/DBD-mysql.ppd
>
>Now I am getting the error below. 
>Can someone point me in a direction on how to have the server
>recognize this DLL? What's the problem with this?
[ .. ]
>Can't load 'C:/Perl/site/lib/auto/DBD/mysql/mysql.dll' for module
>DBD::mysql: load_file:
[ .. ]
Try putting the directory containing your mysql dlls
(eg, C:/mysql/lib/opt/, where libmySQL.dll lives) into
your PATH environment variable. Alternatively, copy
libmySQL.dll to somewhere in your PATH (eg, C:/Perl/bin/), or
else to C:/Perl/site/lib/auto/DBD/mysql/.

-- 
best regards,
randy kobes


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

Date: Tue, 11 Feb 2003 21:15:45 GMT
From: Bart Lateur <bart.lateur@pandora.be>
Subject: Re: Does anyone know if ...
Message-Id: <arpi4v8kc5hahg9b2scb8rfiptqu3sqg86@4ax.com>

Basil Skordinski wrote:

>Does anyone know if there are any Perl modules out there somewhere that
>could make it possible and easy to program a desired interaction with a
>remote web site?  I am looking to do this on the Windows NT platform.  If
>the modules need to work through a browser -- that's fine to.

LWP. No, you don't need to go through the browser. On Windows, it's
likely that these modules (it's a suite) are included with Perl. Look
into the document "lwpcook" (fro m"the LWP cookbook") first.

>More specifically, this is what I'm looking to do.  Monitor a real-time
>display of 10 to 50 numbers and when a certain combination of numbers is
>displayed, establish a link with a remote web site (different from the one
>that's supplying the real-time display) and automatically fill out a form on
>that web site and submit it.

Oh, you'll need HTML::Parser, or one of its descendants
(HTML::TokeParser, HTML::TokeParser::Simple) as well.

-- 
	Bart.


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

Date: Tue, 11 Feb 2003 20:48:11 -0000
From: Jacqui or (maybe) Pete <porjes@spamcop.net>
Subject: Fork multiple children, read from their pipes
Message-Id: <MPG.18b371d1639904629897db@news.CIS.DFN.DE>

I've been trying to spawn some child processes and read from their 
various STDOUT's via pipes.  I've read faq's, docs, & books until my 
eyes have crossed (maybe that's the problem).  Any clues / pointers to 
resources very much welcomed.

My current (no doubt severely duff) code is:

#!/usr/bin/perl 
use warnings;
use strict;

$SIG{CHLD} = 'IGNORE';

use constant NKIDS => 4;
my ($to_parent, %files, $I, $fh);

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

for ($I = 0; $I < NKIDS; $I++) {
    if ( open($fh,"-|"))  {
        $files{$i} = $fh;
    } else {
        print STDOUT "xx" . $i . " Stuff from the child xx \n";
        exit;
    }
}
for ($i = 0; $i < NKIDS; $i++) {
    $fh = $files{$i};
    $to_parent .= "(".$i.")" . ":";
    my $in;
    while ($in = <$fh>) {
        $to_parent .= $in;
    }
    close($fh) or print "Failed to close " . $i;
}
print "From parent (" . $to_parent . ")\n";

# Output is:
# Failed to close 0Failed to close 1Failed to close 2Failed to close 
# 3From  parent ((0):xx3 Stuff from the child xx 
# (1):(2):(3):)

-- 
Your site's most important visitors are completely blind, 
totally deaf, and use browsers you've never seen. 


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

Date: 11 Feb 2003 21:57:37 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Fork multiple children, read from their pipes
Message-Id: <b2brkh$p8v$1@mamenchi.zrz.TU-Berlin.DE>

Jacqui or (maybe) Pete  <porjes@spamcop.net> wrote in comp.lang.perl.misc:
> I've been trying to spawn some child processes and read from their 
> various STDOUT's via pipes.  I've read faq's, docs, & books until my 
> eyes have crossed (maybe that's the problem).  Any clues / pointers to 
> resources very much welcomed.
> 
> My current (no doubt severely duff) code is:

No, it isn't.  Please use copy/paste to transfer code into an article.
You have some unnecessary typos below.

> #!/usr/bin/perl 
> use warnings;
> use strict;

Good.

> $SIG{CHLD} = 'IGNORE';
> 
> use constant NKIDS => 4;
> my ($to_parent, %files, $I, $fh);

It is generally a good idea to declare variables with the smallest scope
they need.  The corollary is, don't declare them all at the top.  This is
sometimes just an issue of style, but here adherence to the rule would
have saved you trouble.

> print "Content-type:text/plain\n\n";
> 
> for ($I = 0; $I < NKIDS; $I++) {
>     if ( open($fh,"-|"))  {
                ^^^
Here is your problem.  Because the my() declaration of $fh is outside
the loop, $fh keeps its value from one round to the next, so the
second time through $fh contains a valid filehandle.  open()ing it again
first closes it, then opens it with the new file (well, process).

Take the declaration of $fh out of the top my() and rewrite open() as
"open( my $fh, '-|')".  That will resolve the issue.  While you're
at it, move all my() declarations to the first use of the variable.

>         $files{$i} = $fh;
                  ^
That's an undeclared variable (case matters).  It wouldn't compile
under strict, so it can't be your actual code.

What you try to do here is squirrel away the filehandle, but since
the *same* filehandle is still in $fh, it will be closed later.  Not
good.

>     } else {
>         print STDOUT "xx" . $i . " Stuff from the child xx \n";
>         exit;
>     }
> }
> for ($i = 0; $i < NKIDS; $i++) {

Same $I/$i problem as above.

>     $fh = $files{$i};
>     $to_parent .= "(".$i.")" . ":";
>     my $in;
>     while ($in = <$fh>) {
>         $to_parent .= $in;
>     }
>     close($fh) or print "Failed to close " . $i;
> }
> print "From parent (" . $to_parent . ")\n";
> 
> # Output is:
> # Failed to close 0Failed to close 1Failed to close 2Failed to close 
> # 3From  parent ((0):xx3 Stuff from the child xx 
> # (1):(2):(3):)

The "Failed to close..." messages are because you are trying to close
already closed handles.

Anno


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

Date: Tue, 11 Feb 2003 22:49:01 GMT
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Fork multiple children, read from their pipes
Message-Id: <x5f2a.10654$LY2.631528@newsc.telia.net>

Jacqui or (maybe) Pete wrote:
> I've been trying to spawn some child processes ... Any clues / pointers to 
> resources very much welcomed.

I have found the CPAN module Parallel::ForkManager useful.

/ Gunnar

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



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

Date: 11 Feb 2003 19:06:19 GMT
From: "Tassilo v. Parseval" <tassilo.parseval@post.rwth-aachen.de>
Subject: Re: Insecure Filehandle Dependencies
Message-Id: <b2bhjb$c3r$1@nets3.rz.RWTH-Aachen.DE>

Also sprach Alan J. Flavell:

> On Feb 11, Tassilo v. Parseval inscribed on the eternal scroll:
> 
>> > #!/bin/perl -wT
>>                ^^
>> This is good.
> 
> Yup...
> 
>> -T is actually the source of your problem.
> 
> Pardon me, but the underlying source of the "problem" could still be
> there in the absence of -T, just that it would not be noticed until it
> was much too late!

Well, naturally. With problem I was referring to the error-message that
turned out to be unresolvable for the OP and not in the least implying
that -T should be dropped. That was not the content of my suggestion
later on.

> To put it another way, -T is not the "source of the problem", but
> merely the cause of the potential problem coming to light.
> 
> Sorry if I seem to be nit picking, but this could be an important
> point. I've seen too many cases where users have "solved the problem"
> by simply removing -T, or strict, or whatever it was that was
> provoking the diagnostic that they were getting, without taking any
> thought to investigate the underlying cause.

No, it's ok. I am pretty sure you are targeting this rather towards the
OP than to me (since I, with all modesty, know the above already:-).

>> > if ($rawfilename !~ /\W/) {
>> >    $filename = $rawfilename;
>> > }

[...]

>> You have to apply a capturing pattern-match
>> to untaint the data:
> ...
>>         ($filename) = $rawfilename =~ /(.*)/;
> ...
> 
> Am I being too much of a perfectionist to suggest it's unwise to
> encourage that paradigm?
> 
> OK, it _has_ been encapsulated here in some code which, in fact,
> renders it safe, but wouldn't it be better general advice to untaint
> $filename with an appropriate regexp?  (a slight re-casting of the
> other statements seems to be all that it needs).

I don't like this capture-all idiom I used above very much either. But
it seemed to be the most obvious solution in this case. The problem with
the above is that it is treacherous: Someone might see it as a means to
shut up tainting error messages (which it does arguably) but forgetting
about the deeper motives behind untainting.

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, 11 Feb 2003 21:44:07 +0100
From: "Alan J. Flavell" <flavell@mail.cern.ch>
Subject: Re: Insecure Filehandle Dependencies
Message-Id: <Pine.LNX.4.53.0302112142260.9560@lxplus077.cern.ch>

On Feb 11, Tassilo v. Parseval inscribed on the eternal scroll:

> No, it's ok. I am pretty sure you are targeting this rather towards the
> OP

Well, towards J. Random Lurker, if you will...

> than to me (since I, with all modesty, know the above already:-).

Of course!

-- 
           Manchmal denke ich, ich brauche die ganzen schicken, neuen
           Brauser nur, um mich besser gegen den Autor zu wehren...
                                          - Sybille Kahl on dciwam



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

Date: 11 Feb 2003 19:45:27 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Paging output
Message-Id: <b2bjsn$k81$1@mamenchi.zrz.TU-Berlin.DE>

Benjamin Goldberg  <goldbb2@earthlink.net> wrote in comp.lang.perl.misc:
> Anirban Banerjee wrote:
> > 
> > How do I do it inside the perl script? I mean everything, even
> > <tab><tab> ( I am using ReadLine ) should give me paged output, and
> > this should be transparent to the individual functions.
> 
> To page perl's output, you can do:
> 
>    if( -t STDOUT ) {
>       my @pagers = qw(less more pg);
>       unshift @pagers, $ENV{MORE} if exists $ENV{MORE};
>       for my $pager ( @pagers ) {
>          open( STDOUT, "|-", $pager ) and last;
>       }
>       warn "Couldn't page STDOUT: $!" unless -p STDOUT;
>    } else {
>       # STDOUT is already being redirected to a pager or
>       # to a file or to some other program.
>    }

I tried that too, but found that the forked process doesn't have a
terminal to read the paging commands from.  I guess you'd need IO::Pty
plus Open2 (or, perhaps Expect) to manage this.

Anno


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

Date: Tue, 11 Feb 2003 15:14:59 -0500
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Paging output
Message-Id: <3E4959C3.7EC88627@earthlink.net>

Anno Siegel wrote:
> 
> Benjamin Goldberg  wrote:
> > Anirban Banerjee wrote:
> > >
> > > How do I do it inside the perl script? I mean everything, even
> > > <tab><tab> ( I am using ReadLine ) should give me paged output,
> > > and this should be transparent to the individual functions.
> >
> > To page perl's output, you can do:
> >
> >    if( -t STDOUT ) {
> >       my @pagers = qw(less more pg);
> >       unshift @pagers, $ENV{MORE} if exists $ENV{MORE};
> >       for my $pager ( @pagers ) {
> >          open( STDOUT, "|-", $pager ) and last;
> >       }
> >       warn "Couldn't page STDOUT: $!" unless -p STDOUT;
> >    } else {
> >       # STDOUT is already being redirected to a pager or
> >       # to a file or to some other program.
> >    }
> 
> I tried that too, but found that the forked process doesn't have a
> terminal to read the paging commands from.

The forked process should inherit it's parent's terminal -- it ought to
be able to open "/dev/tty" and get keys from the user.

Dissociating a forked process from it's parent's terminal is an extra
step... a step which I don't do here, so that shouldn't happen.

> I guess you'd need IO::Pty plus Open2
> (or, perhaps Expect) to manage this.

And how would you manage this?  The pager needs to differentiate between
the user's input and the data being paged.  If you give 'more' a
terminal as it's stdin, where's it supposed to look to read the data
from?

-- 
"So, who beat the clueless idiot today?"
"Well, we flipped for it, but when Kuno
 landed, he wasn't in any shape to fight."
"Next time, try flipping a *coin.*"


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

Date: Tue, 11 Feb 2003 15:12:03 -0500
From: "Duke" <thisis@bogus.com>
Subject: PCRE stumper (PHP preg_match_all)
Message-Id: <v4im8lhjp77p4f@news.supernews.com>

In PHP, I have the following:

   preg_match_all("/begin [0-7]{3,4} (.+)\r?\n(.+)\r?\nend/Us", $data,
$matches);

this scans for UUEncoded attachments in the message body of an email and it
works great.  My problem is with optimizing it - the above will return the
following:

$matches[0] = one element for each UUEncoded part, full part
$matches[1] = one element for each UUEncoded part, filename
$matches[2] = one element for each UUEncoded part, full part with headers
stripped

the problem is that $matches[0] is pretty much the same as $matches[2], and
it uses twice the memory for no practical purpose.  Is it possible to have
it _not_ return the default 'matched' match ($matches[0])?  I'm thinking
that for large attachments (say, 8MB) this would be a significant waste of
memory.




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

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


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