[24418] in Perl-Users-Digest
Perl-Users Digest, Issue: 6606 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue May 25 11:05:48 2004
Date: Tue, 25 May 2004 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)
Perl-Users Digest Tue, 25 May 2004 Volume: 10 Number: 6606
Today's topics:
Re: ActiveState Perl mangles text files <mothra@mothra.com>
Re: ActiveState Perl mangles text files <mothra@mothra.com>
Re: ActiveState Perl mangles text files <1usa@llenroc.ude>
Re: ActiveState Perl mangles text files <rick.cross@btopenworld.com>
Re: ActiveState Perl mangles text files <rick.cross@btopenworld.com>
Re: ActiveState Perl mangles text files <usenet@morrow.me.uk>
Re: ActiveState Perl mangles text files <usenet@morrow.me.uk>
Am I a programmer or a scripter? <mothra@mothra.com>
Re: Am I a programmer or a scripter? <peter@semantico.com>
Re: Am I a programmer or a scripter? <ittyspam@yahoo.com>
Re: Getting an IP address (Paul Lalli)
Getting the Attribute Bold <GeorgeKinleyxxx@hotmail.com>
how to pass a hash from c <www.@o>
Re: Map or Regex and Sorting <uri@stemsystems.com>
Re: Netcat frontend - please help <mgjv@tradingpost.com.au>
Newbie Question <laurence_breeze@yahoo.co.uk>
Newbie Question <laurence_breeze@yahoo.co.uk>
Re: Newbie Question <jurgenex@hotmail.com>
Re: Newbie Question <peter@semantico.com>
Re: Newbie Question <nospam@bigpond.com>
Re: Newbie Question <tadmc@augustmail.com>
Re: Perl inplace editing (Sundaram Ramasamy)
Re: Perl work? <mothra@mothra.com>
query on fork in perl (debraj)
Re: query on fork in perl <salvafg@terra.es>
Re: query on fork in perl (Greg Bacon)
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Tue, 25 May 2004 11:43:53 GMT
From: Mothra <mothra@mothra.com>
Subject: Re: ActiveState Perl mangles text files
Message-Id: <ZjGsc.16201017$Of.2702554@news.easynews.com>
>>Hmm... I always thought of it the other way round - IO::File did come
>>later after all. Using IO:File can be useful where you actually want
>>your file object to be restricted by scope (filehandles are
>>automatically global). So I've just gotten into the habit of always
>>using it.
>
>
> Really? I'm surprised...
>
> The point about lexical FHs is that they *are* scoped. In my code:
>
> my @lines = do {
> open my $IN, '<', 'file' or die ...;
> <$IN>
> };
>
> the FH is closed at the end of the scope.
>
> Ben
>
Then why would the first example (below) write to the file, but the
second won't?
This will write to the file "foobar.txt"
-----------------
use strict;
if ( 1 == 1 ) {
open(FILE, ">foobar.txt");
}
print FILE "Foo";
-----------------
....whereas this will fail with: Global symbol "$file" requires explicit
package name at opening.pl line 8. Execution of opening.pl aborted due
to compilation errors.
-----------------
use strict;
use IO::File;
if ( 1 == 1 ) {
my $file = IO::File->new('foobar.txt', 'w');
}
print $file "Foo";
-----------------
------------------------------
Date: Tue, 25 May 2004 11:55:58 GMT
From: Mothra <mothra@mothra.com>
Subject: Re: ActiveState Perl mangles text files
Message-Id: <ivGsc.16108671$Id.2673422@news.easynews.com>
Jim Keenan wrote:
> Mothra <mothra@mothra.com> wrote in message news:<VWisc.16121270$Of.2688947@news.easynews.com>...
>
>>I woudn't normally use this, but it seemed the easiest way to process my
>>"Blocked Senders" list in Outlook.
>>
>>The code below, should be straightforward, but look at the output it
>>produces when you run it on a windows text file (created in notepad).
>>It seems to put blank spaces (or unprintable characters) in between each
>>original character. If I write it back out to a text file, it's even worse.
>>
>>Anyone know (or care) why? I used to write AS Perl scripts a couple of
>>years ago, and I'm sure it never did this.
>>
>>---------code---------
>>
>>#!/perl
>
>
> Unless something has changed lately, I don't think the shebang line is
> relevant in AS Perl. I have the perl executable in my path and call
> 'perl iofile.pl' from the command line.
>
>
>>use strict;
>>use IO::File;
>>
>>my $input=IO::File->new('blocked_senders.txt', 'r');
>>my @blocked_senders=<$input>;
>>$input->close;
>>
>>foreach(@blocked_senders){
>> print $_ . "\n";
>>}
>>
>
> Since you didn't supply 'blocked_senders.txt' I had to make some
> assumptions as to how it looked. Assumptions: (1) Single '\n' at end
> of line rather than two; (2) All lines flush against left margin
> rather than starting with 2 wordspaces; (3) Eliminate '?_' garbage on
> first e-mail address; (4) eliminate all wordspaces. In other words:
>
I think that was actually the problem. The text file was created in
"Windows Notepad" by MS Outlook 2003 in a particular format, which is
where all the garbage is coming from. If I pasted the same text into a
file on a Unix machine, it all worked fine. I should have opened the
file and re-saved it in the correct format. Bloody awkward Notepad :-(
------------------------------
Date: 25 May 2004 12:03:18 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude>
Subject: Re: ActiveState Perl mangles text files
Message-Id: <Xns94F451F16B151asu1cornelledu@132.236.56.8>
Mothra <mothra@mothra.com> wrote in news:ZjGsc.16201017$Of.2702554
@news.easynews.com:
>>>Hmm... I always thought of it the other way round - IO::File did come
>>>later after all. Using IO:File can be useful where you actually want
>>>your file object to be restricted by scope (filehandles are
>>>automatically global).
...
>> The point about lexical FHs is that they *are* scoped. In my code:
>>
>> my @lines = do {
>> open my $IN, '<', 'file' or die ...;
>> <$IN>
>> };
>>
>> the FH is closed at the end of the scope.
...
> Then why would the first example (below) write to the file, but the
> second won't?
Mothra, you are missing the crucial part of Ben's post about _lexical_
filehandles. Did you see the my $IN part after open in the line above?
> This will write to the file "foobar.txt"
> -----------------
> use strict;
> if ( 1 == 1 ) {
> open(FILE, ">foobar.txt");
> }
>
> print FILE "Foo";
> -----------------
Try this:
use strict;
if (1) {
open my $FILE, '>', 'foobar.txt' or die $!;
}
print $file "Foo";
--
A. Sinan Unur
1usa@llenroc.ude (reverse each component for email address)
------------------------------
Date: Tue, 25 May 2004 12:38:56 GMT
From: Mothra <rick.cross@btopenworld.com>
Subject: Re: ActiveState Perl mangles text files
Message-Id: <A7Hsc.5254146$iA2.613945@news.easynews.com>
A. Sinan Unur wrote:
> Mothra, you are missing the crucial part of Ben's post about _lexical_
> filehandles. Did you see the my $IN part after open in the line above?
>
I did but didn't understand the significance of it. My bad.
------------------------------
Date: Tue, 25 May 2004 12:50:34 GMT
From: Mothra <rick.cross@btopenworld.com>
Subject: Re: ActiveState Perl mangles text files
Message-Id: <uiHsc.5254760$iA2.614405@news.easynews.com>
Ben Morrow wrote:
> Quoth Mothra <mothra@mothra.com>:
>
>>Ben Morrow wrote:
>>
>>
>>>Quoth Mothra <mothra@mothra.com>:
>>>
>>>
>>>>Being awkward now, but is there a way I do this with IO::File?
>>>
>>>
>>>Why? Perl's lexical filehandles (the 'open my $IN' in my example) make
>>>IO::File obsolete, AFAICS.
>>>
>>
>>Hmm... I always thought of it the other way round - IO::File did come
>>later after all. Using IO:File can be useful where you actually want
>>your file object to be restricted by scope (filehandles are
>>automatically global). So I've just gotten into the habit of always
>>using it.
>
>
> Really? I'm surprised...
>
> The point about lexical FHs is that they *are* scoped. In my code:
>
> my @lines = do {
> open my $IN, '<', 'file' or die ...;
> <$IN>
> };
>
> the FH is closed at the end of the scope.
>
> Ben
>
Sorry I must have misunderstood your first response. I was taught on a
training course a couple of years ago that using IO::File->new was the
best method for opening files. I was given a few reasons, one of which
was the fact that you can scope your file objects. I honestly can't
remmeber any other reasons. I was shown other methods such as the
bareword method, which was what I thought you were referring to until I
read your post more carefully. Wasn't shown the lexical filehandles,
which I find surprising, as the course was a pretty good one.
From what you've written, it appears that I was given misleading
information there?
------------------------------
Date: Tue, 25 May 2004 13:25:53 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: ActiveState Perl mangles text files
Message-Id: <c8vhh1$jla$1@wisteria.csv.warwick.ac.uk>
Quoth Mothra <mothra@mothra.com>:
> >>Hmm... I always thought of it the other way round - IO::File did come
> >>later after all. Using IO:File can be useful where you actually want
> >>your file object to be restricted by scope (filehandles are
> >>automatically global). So I've just gotten into the habit of always
> >>using it.
> >
> >
> > Really? I'm surprised...
> >
> > The point about lexical FHs is that they *are* scoped. In my code:
> >
> > my @lines = do {
> > open my $IN, '<', 'file' or die ...;
> > <$IN>
> > };
> >
> > the FH is closed at the end of the scope.
>
> Then why would the first example (below) write to the file, but the
> second won't?
>
> This will write to the file "foobar.txt"
> -----------------
> use strict;
> if ( 1 == 1 ) {
> open(FILE, ">foobar.txt");
> }
>
> print FILE "Foo";
I said '*lexical* FHs'. Not global bareword FHs. They are rather
different beasts. Try:
use strict;
{
open my $FILE, '>', 'foobar.txt' or die $!;
}
print $FILE 'Foo';
Ben
--
Although few may originate a policy, we are all able to judge it.
- Pericles of Athens, c.430 B.C.
ben@morrow.me.uk
------------------------------
Date: Tue, 25 May 2004 13:29:08 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: ActiveState Perl mangles text files
Message-Id: <c8vhn4$jla$2@wisteria.csv.warwick.ac.uk>
Quoth Mothra <rick.cross@btopenworld.com>:
>
> Sorry I must have misunderstood your first response. I was taught on a
> training course a couple of years ago that using IO::File->new was the
> best method for opening files. I was given a few reasons, one of which
> was the fact that you can scope your file objects. I honestly can't
> remmeber any other reasons. I was shown other methods such as the
> bareword method, which was what I thought you were referring to until I
> read your post more carefully. Wasn't shown the lexical filehandles,
> which I find surprising, as the course was a pretty good one.
>
> From what you've written, it appears that I was given misleading
> information there?
One of my reasons for the 'I'm surprised' is that I am fairly sure that
lexical FHs are considerably newer than IO::Handle. It is entirely
likely that they weren't invented when you took the course.
Checking perl56delta confirms my suspicion that lexical FHs were new in
5.6.0...
Ben
--
I've seen things you people wouldn't believe: attack ships on fire off
the shoulder of Orion; I watched C-beams glitter in the dark near the
Tannhauser Gate. All these moments will be lost, in time, like tears in rain.
Time to die. ben@morrow.me.uk
------------------------------
Date: Tue, 25 May 2004 12:09:20 GMT
From: Mothra <mothra@mothra.com>
Subject: Am I a programmer or a scripter?
Message-Id: <QHGsc.16109363$Id.2673543@news.easynews.com>
Semantic question here: what's the difference between a script and a
program? At what stage can you call your Perl script a program for
example? And at what stage can you call yourself a Perl programmer?
I encounter some snobbery from time to time with programmers telling me
that what I'm doing is "just scripting", whereas the stuff they write
(in Java, ColdFusion, C++ etc) is "real programming".
Well yes, I'm a lowly Sys Admin, and I've only been using Perl for about
3 years, but I've written some big and complex "scripts" in Perl to
solve Sys Admin-type problems.
So is there a real difference, or is it just semantic snobbery?
------------------------------
Date: Tue, 25 May 2004 13:55:20 +0100
From: Peter Hickman <peter@semantico.com>
Subject: Re: Am I a programmer or a scripter?
Message-Id: <40b34238$0$2114$afc38c87@news.easynet.co.uk>
Mothra wrote:
> So is there a real difference, or is it just semantic snobbery?
Snobbery, no doubt about it.
Besides *real* programmers use assembler none of this high level language stuff that is just for the babies who can't cope with hex ;-)
I once knew a guy who programmed intel assembly in debug, not just DOS stuff but windows drivers and the like.
------------------------------
Date: Tue, 25 May 2004 10:01:40 -0400
From: Paul Lalli <ittyspam@yahoo.com>
Subject: Re: Am I a programmer or a scripter?
Message-Id: <20040525100107.S3804@dishwasher.cs.rpi.edu>
On Tue, 25 May 2004, Mothra wrote:
> Semantic question here: what's the difference between a script and a
> program? At what stage can you call your Perl script a program for
> example? And at what stage can you call yourself a Perl programmer?
>
> I encounter some snobbery from time to time with programmers telling me
> that what I'm doing is "just scripting", whereas the stuff they write
> (in Java, ColdFusion, C++ etc) is "real programming".
>
> Well yes, I'm a lowly Sys Admin, and I've only been using Perl for about
> 3 years, but I've written some big and complex "scripts" in Perl to
> solve Sys Admin-type problems.
>
> So is there a real difference, or is it just semantic snobbery?
The FAQ covers this rather extensively:
perldoc -q script
Paul Lalli
------------------------------
Date: 25 May 2004 05:38:21 -0700
From: ittyspam@yahoo.com (Paul Lalli)
Subject: Re: Getting an IP address
Message-Id: <bf6ba38c.0405250438.6b7429a8@posting.google.com>
[please post your reply below the original message. See the posting
guidelines]
"Sean Berry" <sean_berry@cox.net> wrote:
> "Paul Lalli" <ittyspam@yahoo.com> wrote in message
> news:20040524190845.R338@dishwasher.cs.rpi.edu...
> > However... if you're already using htaccess, might it not be a better idea
> > to use that to limit the IP addresses, as that functionality is already
> > built in? Just a suggestion, it may or may not apply to your situation.
> >
> > Paul Lalli
>
> I was thinking about that but I already have programs in the directory that
> shoud be able to be accessed from anywhere.
>
> How does it work if I have an .htaccess in ./domain/private and another in
> ./domain/private/restricted
>
> Are both passwords going to be required to access
> ./domain/private/restricted?
>
> Many thanks. I know this has turned into a non-perl question, but hate to
> start another post on another newsgroup for a simple question like this.
You should probably read the Apache documentation on the subject.
(http://httpd.apache.org/docs/mod/core.html#files might prove useful).
There are ways to limit .htaccess to apply to only certain
files/directories.
Paul Lalli
------------------------------
Date: Tue, 25 May 2004 14:10:25 GMT
From: George Kinley <GeorgeKinleyxxx@hotmail.com>
Subject: Getting the Attribute Bold
Message-Id: <Xns94F4AF2E3C6FAGeorgeKinleyxxxhotma@131.228.6.98>
HI,
I am trying to print in color to command window in WIN2k, I am running my
script on cmd.exe , but when I use Term:ANSIColor I am not able to have
color display, I am not sure why it happenes, any way I worked other way
round and used Wins32::Console, Now I can get colors , my quesction is how
can I make the Attribute BOLD using Wins32::Console or any other module
which I am not aware of
-G
------------------------------
Date: Tue, 25 May 2004 16:20:45 +0200
From: wwwww <www.@o>
Subject: how to pass a hash from c
Message-Id: <40b35640$1@e-post.inode.at>
hi !
i would like to use perl as a script-language for my c/c++ written programm.
i can perfectly call function with scalars.
but how can i pass hashes (HV*) ?
thanks
wolfgang reder
------------------------------
Date: Tue, 25 May 2004 13:20:59 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Map or Regex and Sorting
Message-Id: <x7fz9omymc.fsf@mail.sysarch.com>
>>>>> "HH" == Herr Hardy <schaumfestiger@gmx.de> writes:
HH> On Tue, 25 May 2004 08:31:02 +0200, Herr Hardy <schaumfestiger@gmx.de>
HH> wrote:
HH> Taking your funny line
>>> push @{ /OPEN/ ? \@openlist : \@clsdlist }, $_ for
HH> without mapping
HH> push @{ /OPEN/ ? \@openlist : \@clsdlist }, $_ for
HH> sort {
HH> my($x) = ($a=~/(\d{1,3})/);
HH> my($y) = ($b=~/(\d{1,3})/);
HH> $x <=> $y;
HH> }
HH> <DATA>;
read http://sysarch.com/perl/sort_paper.html to find out why the
map/sort/map is usually much faster.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
------------------------------
Date: Tue, 25 May 2004 22:51:52 +1000
From: Martien Verbruggen <mgjv@tradingpost.com.au>
Subject: Re: Netcat frontend - please help
Message-Id: <slrncb6gb8.nqd.mgjv@martien.heliotrope.home>
On 24 May 2004 05:17:21 -0700,
Piotrek <red.guy@gazeta.pl> wrote:
>
>
> Hello,
>
> I have a task to write a ftp client using a compiled version of linux
> netcat. I figures this would be a good way to learn perl ;)
The full FTP protocol can't be implemented with a single netcat program.
Part of the FTP protocol requires a separate data connection from the
control connection (although the data connection does not have to be
there all the time).
If you want to get a working FTP client, and write it in Perl, the best
bet you have is the Net::FTP module. If you don't want to use that, for
whatever reasons, at least study it to see how it does its things. You
should at least get the relevant RFCs for the FTP protocol, and study
them first (959, and maybe 2228, 2640, 2773).
> What I need to do is to take the default input and output from a
> launched netcat prog. and make a frontend.
Maybe the Expect module can help, if you insist on using netcat. It's
created to do this sort of thing.
Martien
--
|
Martien Verbruggen | If it isn't broken, it doesn't have enough
| features yet.
|
------------------------------
Date: Tue, 25 May 2004 11:00:59 +0100
From: Laurence Breeze <laurence_breeze@yahoo.co.uk>
Subject: Newbie Question
Message-Id: <40B3195B.3050707@yahoo.co.uk>
I have a reference to a 2 dimensional array and I'd like to print out
the contents of the array. However, the number of elements will vary
and I can't see a way of doing this.
Any help or references would be appreciated.
Laurence
------------------------------
Date: Tue, 25 May 2004 11:18:48 +0100
From: Laurence Breeze <laurence_breeze@yahoo.co.uk>
Subject: Newbie Question
Message-Id: <40B31D88.9090301@yahoo.co.uk>
I have a reference to a 2 dimensional array and I'd like to print out
the contents of the array. However, the number of elements will vary
and I can't see a way of doing this.
Any help or references would be appreciated.
Laurence
------------------------------
Date: Tue, 25 May 2004 10:23:53 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: Newbie Question
Message-Id: <Z8Fsc.24306$ZQ.12456@nwrddc03.gnilink.net>
Laurence Breeze wrote:
> I have a reference to a 2 dimensional array and I'd like to print out
> the contents of the array. However, the number of elements will vary
> and I can't see a way of doing this.
>
> Any help or references would be appreciated.
Do you have problems writing the nested loop or do you have problems
dereferencing the AOA (array of arrays)?
jue
------------------------------
Date: Tue, 25 May 2004 11:42:16 +0100
From: Peter Hickman <peter@semantico.com>
Subject: Re: Newbie Question
Message-Id: <40b32308$0$1864$afc38c87@news.easynet.co.uk>
Laurence Breeze wrote:
> I have a reference to a 2 dimensional array and I'd like to print out
> the contents of the array. However, the number of elements will vary
> and I can't see a way of doing this.
>
> Any help or references would be appreciated.
>
> Laurence
>
If this is just for debugging then look at Data::Dumper.
use Data::Dumper;
print Dumper( $var );
all yours.
------------------------------
Date: Tue, 25 May 2004 22:21:31 +1000
From: Gregory Toomey <nospam@bigpond.com>
Subject: Re: Newbie Question
Message-Id: <11558458.GumVJxqfWq@GMT-hosting-and-pickle-farming>
Laurence Breeze wrote:
> I have a reference to a 2 dimensional array and I'd like to print out
> the contents of the array. However, the number of elements will vary
> and I can't see a way of doing this.
>
> Any help or references would be appreciated.
>
> Laurence
Havr a look at the 'array of arrays' in the data structures cookbook
http://www.perldoc.com/perl5.8.0/pod/perldsc.html
gtoomey
------------------------------
Date: Tue, 25 May 2004 07:55:06 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Newbie Question
Message-Id: <slrncb6gha.teu.tadmc@magna.augustmail.com>
Laurence Breeze <laurence_breeze@yahoo.co.uk> wrote:
> Subject: Newbie Question
Please put the subject of your article in the Subject of your article.
> I have a reference to a 2 dimensional array and I'd like to print out
> the contents of the array.
use Data::Dumper;
print Dumper @array;
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: 25 May 2004 05:56:48 -0700
From: sundaram@sfg.homeunix.com (Sundaram Ramasamy)
Subject: Re: Perl inplace editing
Message-Id: <b029225f.0405250456.4c7b513c@posting.google.com>
"John W. Krahn" <krahnj@acm.org> wrote in message news:<40B2A3D2.64BC6542@acm.org>...
> Sundaram Ramasamy wrote:
> >
> > I want to check in file line start with HOSTNAME, then I want to
> > replcae HOSTNAME value to linux.com, if line is not there I want add
> > new line HOSTNAME=linux.com
> >
> > Using inplace editing I was not able to add new line.
> >
> > Here is my one liner inplace editing script
> >
> > perl -i.old -ne '$ne=0; if( /^\s*HOSTNAME\s*=/ ) {
> > s/=.*$/=linux.com/; $nx++; print $_; }else { print $_; } END { if( $nx
> > ==0 ){ $_="HOSTNAME=linux.com\n"; print $_; } }' network
> >
> > Requirment:
> >
> > 1) orginal file:
> > NMAE=myname
> > IP=234.56.43.23
> > HOSTNAME=abcde.com
> >
> > I need out put:
> > NMAE=myname
> > IP=234.56.43.23
> > HOSTNAME=linux.com
> >
> > 2) orginal file:
> > NMAE=myname
> > IP=234.56.43.23
> >
> > I need out put:
> > NMAE=myname
> > IP=234.56.43.23
> > HOSTNAME=linux.com
> >
> > Any tips for this
>
>
> perl -i.old -0pe'
> s/(?<=HOSTNAME)(\s*=\s*.+)/=linux.com/
> ||
> s/\z/HOSTNAME=linux.com\n/
> ' network
>
>
>
> John
Thanks for all,
John can you explain the meaning of your script.
SR
------------------------------
Date: Tue, 25 May 2004 12:21:50 GMT
From: Mothra <mothra@mothra.com>
Subject: Re: Perl work?
Message-Id: <yTGsc.16110014$Id.2673656@news.easynews.com>
krakle wrote:
> Charlton Wilbur <cwilbur@mithril.chromatico.net> wrote in message news:<87lljj24hm.fsf@mithril.chromatico.net>...
>
>>They're also people in foreign countries where $2/hour is a very good
>>wage. Someone who lives in a region where $2000/year is a comfortable
>>income is probably doing quite well to be making $3 or $4 an hour.
>
>
> ermmm.. I would have to disagree. I don't think someone who makes
> $2,000 a year able to live off from $3 an hour even has a computer
> with internet access and a programming education background to do the
> work. I would of thought common sense would of ruled that out...
Whatever you might think, it happens. That's why so many IT jobs in the
UK are being outsourced to the far East. Not sure if this is true in
the USA as well?
------------------------------
Date: 25 May 2004 05:04:20 -0700
From: debhatta@hotmail.com (debraj)
Subject: query on fork in perl
Message-Id: <f9f243e.0405250404.72ce2f09@posting.google.com>
Hi
I am trying out a new thing to me ie. use fork(). Now my requirement
is like this :
I have a file which has quite a few numbers such as :
22,23,34 etc.
Now I have a sub-routine say sub create_page, which when passed the
above numbers does quite a few things and creates html pages in their
name, such as 22.html etc.
So instead of passing one at a time, I wanted to pass it to fork'ed
processes so that they happen all at a time. This is where I am
getting stuck. Can anyone help out please ? basically how to call the
same sub with different parameters and on different processes ? and
how to control them ?
My code :
open LIST , "file";
FORKER: while (<LIST>) {
my $newpid = fork();
if ( not defined $newpid )
{
# if return value of fork() is undef, something went wrong
die "fork didn't work: $!\n";
}
elsif ( $newpid == 0 )
{
# if return value is 0, this is the child process
$parent = $pid; # which has a parent called $pid
$pid = $$; # and which will have a process ID of its very
own
@kids = (); # the child doesn't want this baggage from the
parent
last FORKER; # and we don't want the child making babies
either
}
else
{
# the parent process is returned the PID of the newborn by
fork()
print "$$ spawned $newpid \n";
push @kids, $newpid;
$i= $#kids;
$i ++;
}
}
if ( $parent ) # if I have a parent, i.e. if I'm the child process
{
print "I am process number $pid\n";
&create_page("$kids[$i]");
print "Creating $kids[$i]\n";
exit( 0 );
}
else
{
# parent process needs to preside over the death of its kids
while ( my $kid = shift @kids )
{
print "Parent waiting for $kid to die\n";
my $reaped = waitpid( $kid, 0 );
unless ( $reaped == $kid )
{
print "Something's up: $?\n";
}
}
}
------------------------------
Date: Tue, 25 May 2004 14:13:43 GMT
From: =?ISO-8859-1?Q?Salvador_Fandi=F1o?= <salvafg@terra.es>
Subject: Re: query on fork in perl
Message-Id: <rwIsc.568802$A6.2193051@telenews.teleline.es>
debraj wrote:
> Hi
>
> I am trying out a new thing to me ie. use fork(). Now my requirement
> is like this :
>
> I have a file which has quite a few numbers such as :
>
> 22,23,34 etc.
> Now I have a sub-routine say sub create_page, which when passed the
> above numbers does quite a few things and creates html pages in their
> name, such as 22.html etc.
> So instead of passing one at a time, I wanted to pass it to fork'ed
> processes so that they happen all at a time. This is where I am
> getting stuck. Can anyone help out please ? basically how to call the
> same sub with different parameters and on different processes ? and
> how to control them ?
>
> My code :
> open LIST , "file";
> FORKER: while (<LIST>) {
> my $newpid = fork();
> if ( not defined $newpid )
> {
> # if return value of fork() is undef, something went wrong
> die "fork didn't work: $!\n";
> }
> elsif ( $newpid == 0 )
> {
> # if return value is 0, this is the child process
> $parent = $pid; # which has a parent called $pid
> $pid = $$; # and which will have a process ID of its very
> own
> @kids = (); # the child doesn't want this baggage from the
> parent
> last FORKER; # and we don't want the child making babies
> either
> }
> else
> {
> # the parent process is returned the PID of the newborn by
> fork()
> print "$$ spawned $newpid \n";
> push @kids, $newpid;
> $i= $#kids;
> $i ++;
> }
>
> }
>
> if ( $parent ) # if I have a parent, i.e. if I'm the child process
> {
> print "I am process number $pid\n";
>
> &create_page("$kids[$i]");
> print "Creating $kids[$i]\n";
> exit( 0 );
> }
> else
> {
> # parent process needs to preside over the death of its kids
> while ( my $kid = shift @kids )
> {
> print "Parent waiting for $kid to die\n";
> my $reaped = waitpid( $kid, 0 );
> unless ( $reaped == $kid )
> {
> print "Something's up: $?\n";
> }
> }
> }
ufff!
I advise you to use Parallel::ForkManager or Proc::Queue from
CPAN to limit the number of concurrent processes running:
# limit number of child processes to 6
use Proc::Queue size => 6, qw(run_back);
use POSIX ":sys_wait_h";
sub create_page {...}
while(<>) {
my $number=$_;
chomp $number;
# fork and call create_page sub
run_back {
create_page($number)
};
# read childs
1 while waitpid(-1, WNOHANG)>0;
}
# wait for all childs to exit
1 while wait != -1;
Bye,
- Salva
------------------------------
Date: Tue, 25 May 2004 14:58:05 -0000
From: gbacon@hiwaay.net (Greg Bacon)
Subject: Re: query on fork in perl
Message-Id: <10b6nnttg20b5cf@corp.supernews.com>
In article <f9f243e.0405250404.72ce2f09@posting.google.com>,
debraj <debhatta@hotmail.com> wrote:
: I have a file which has quite a few numbers such as :
:
: 22,23,34 etc.
: Now I have a sub-routine say sub create_page, which when passed the
: above numbers does quite a few things and creates html pages in their
: name, such as 22.html etc.
: So instead of passing one at a time, I wanted to pass it to fork'ed
: processes so that they happen all at a time. This is where I am
: getting stuck. Can anyone help out please ? basically how to call the
: same sub with different parameters and on different processes ? and
: how to control them ?
See below:
#! /usr/local/bin/perl
use warnings;
use strict;
use POSIX ":sys_wait_h";
sub do_some_work {
my $param = shift;
open my $fh, ">", "$param.html"
or die "$0: open >$param.html: $!";
print $fh map "$_\n",
"<html>",
"<head><title>Happy $param Page</title></head>",
"<body>",
"<h1>Happy $param Page</h1>",
"</body>",
"</html>";
}
## main
my %kid;
for (qw/ 22 23 24 25 /) {
my $pid = fork;
if (not defined $pid) {
warn "$0: fork (item $_): $!";
next;
}
elsif ($pid == 0) {
# child
do_some_work $_;
exit 0;
}
else {
# parent
$kid{$pid} = 1;
}
}
my $errors = 0;
while (scalar keys %kid > 0) {
my $pid = wait;
if ($pid == -1) {
warn "$0: unreaped: " . join(", " => keys %kid);
exit;
}
warn "$0: unknown kid pid $pid"
unless exists $kid{$pid};
++$errors if $? != 0;
delete $kid{$pid};
}
print "Done.\n";
exit $errors;
Hope this helps,
Greg
--
The Constitution is not an instrument for the government to restrain
the people, it is an instrument for the people to restrain the
government -- lest it come to dominate our lives and interests.
-- Patrick Henry
------------------------------
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.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
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 6606
***************************************