[22724] in Perl-Users-Digest
Perl-Users Digest, Issue: 4945 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue May 6 14:52:21 2003
Date: Tue, 6 May 2003 11:05:09 -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, 6 May 2003 Volume: 10 Number: 4945
Today's topics:
Re: basic doubts.. <nobull@mail.com>
Re: Can't write a LOG file, please help (Gino Vives)
Re: Can't write a LOG file, please help <john.thetenant-s@moving-picture.com>
Re: Convert hex to dec <vp@NoSpamPLZ.net>
Re: Design Opinions - Dealing with Constants <koos_pol@NO.nl.JUNK.compuware.MAIL.com>
Re: Design Opinions - Dealing with Constants <temp1_NO_SPAM_@williamc.com>
Re: Design Opinions - Dealing with Constants (Sara)
Extracting part of the contents of one var into another <bigus AT creationfactor DOT net>
Re: Extracting part of the contents of one var into ano <mbudash@sonic.net>
Re: Extracting part of the contents of one var into ano <abigail@abigail.nl>
Re: Forcing an rvalue context on a scalar expression <abigail@abigail.nl>
Re: Forcing an rvalue context on a scalar expression <nobull@mail.com>
How do you create a biniary file in Perl. <wallabyedward@hunterlink.net.au>
Re: How do you create a biniary file in Perl. <tassilo.parseval@rwth-aachen.de>
Re: How do you create a biniary file in Perl. <wallabyedward@hunterlink.net.au>
Re: How do you create a biniary file in Perl. <tassilo.parseval@rwth-aachen.de>
Re: How to fork and PID - revisited ctcgag@hotmail.com
Re: Length of line <jurgenex@hotmail.com>
Re: Length of line <jurgenex@hotmail.com>
Re: Length of line <no@spam.for.me.invalid>
Re: Length of line <bigj@kamelfreund.de>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 06 May 2003 18:34:50 +0100
From: Brian McCauley <nobull@mail.com>
Subject: Re: basic doubts..
Message-Id: <u9ptmw5685.fsf@wcl-l.bham.ac.uk>
"Jürgen Exner" <jurgenex@hotmail.com> writes:
> Rajesh M A wrote:
> > My doubt is simple...what is a dispatch table? i searched the internet
> > a lot, but failed to find any promising information.
>
> A dispatch table is a generic term for a mapping of some sort.
> y => "Yes"
> n => "No"
> m => "Maybe"
>
> Then instead of having a long list of "if ... then print ..." you would
> have one single statement "print dispatchtable{$answer}".
I thought the term dispatchtable generally implied that the RHS of the
mapping was some sort of pointer to executable code. In the case of
Perl this would, in practice, be a CODE reference.
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: 6 May 2003 09:17:56 -0700
From: gino_vives@yahoo.com (Gino Vives)
Subject: Re: Can't write a LOG file, please help
Message-Id: <61f4ab48.0305060817.766e0f30@posting.google.com>
Steve Grazzini <grazz@pobox.com> wrote in message
> Presumably the open() failed. If you add some error-checking
> you'll at least know why:
>
> open LOG, '>> ../logfile' or die "open: ../logfile: $!";
>
> And an absolute path to the logfile would be safer.
>
I'm gonna change it to absolute path...
(just 4 testing because it's gonna run under differents users, then
differents paths)
> > Neither of theses solutions wrote into a file, but the first one
> > (if I just esecute "programe") does write in the screen (STDOUT???)
>
> Didn't you get a shell error when you redirect the first one
> to a file?
>
no, I got the text... something like:
jvives@carufin:~$ programe
(text shown here)
>
> You forgot to "use strict;" and you might want some error-checking
> here as well. (And an absolute path to /bin/date.)
>
The script is working, no matter how baddly was written. My problem is
with the log file, I CAN'T do it!!!!
------------------------------
Date: Tue, 6 May 2003 18:00:02 +0100
From: John Strauss <john.thetenant-s@moving-picture.com>
Subject: Re: Can't write a LOG file, please help
Message-Id: <20030506180002.557d168c.john.thetenant-s@moving-picture.com>
On 6 May 2003 09:17:56 -0700
gino_vives@yahoo.com (Gino Vives) wrote:
>
> Steve Grazzini <grazz@pobox.com> wrote in message
>
> > Presumably the open() failed. If you add some error-checking
> > you'll at least know why:
> >
> > open LOG, '>> ../logfile' or die "open: ../logfile: $!";
> >
> > And an absolute path to the logfile would be safer.
> >
>
> I'm gonna change it to absolute path...
> (just 4 testing because it's gonna run under differents users, then
> differents paths)
> > > Neither of theses solutions wrote into a file, but the first one
> > > (if I just esecute "programe") does write in the screen (STDOUT???)
> >
> > Didn't you get a shell error when you redirect the first one
> > to a file?
> >
>
> no, I got the text... something like:
> jvives@carufin:~$ programe
> (text shown here)
>
>
> >
> > You forgot to "use strict;" and you might want some error-checking
> > here as well. (And an absolute path to /bin/date.)
> >
>
> The script is working, no matter how baddly was written. My problem is
> with the log file, I CAN'T do it!!!!
add these after your interpreter line:
use strict;
use warnings;
then fix all the errors and warnings. the most common of these will be due to not scoping your variables when you declare them. use "my" wherever you declare, eg replace
@fecha = `date`;
with
my @fecha = `date`;
the fact that you print to LOG, but the text appears on your screen indicates that your LOG filehandle wasn't opened properly.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
drop the .thetenant to get me via mail
------------------------------
Date: Tue, 06 May 2003 14:06:05 GMT
From: "VP" <vp@NoSpamPLZ.net>
Subject: Re: Convert hex to dec
Message-Id: <hjPta.20274$yv2.262042@weber.videotron.net>
On Tue, 29 Apr 2003 16:57:46 +0200, kderaedt wrote:
> Hi,
>
> How can I convert a heximal number into a decimal number, is there a
> function available or do I have to calculate this.
Surprise, surprise... 'perldoc -f hex'
--
.signature: No such file or directory
------------------------------
Date: Tue, 06 May 2003 17:18:47 +0200
From: Koos Pol <koos_pol@NO.nl.JUNK.compuware.MAIL.com>
Subject: Re: Design Opinions - Dealing with Constants
Message-Id: <newscache$b71heh$lcb$1@news.emea.compuware.com>
Kevin Vaughn wrote (Tuesday 06 May 2003 15:38):
> I've created a large number of scripts that reference a single file
> containing all of my constants. I named the file environment.pl. The
> file
> mainly contains paths. I include environment.pl using a "required"
> statement.
>
> Is this good or bad design? I'm particularly interested in the downsides.
Well, without going into the pro/con discussion, I have a similar situation.
And I have solved this using my own My_App::Constants package and access the
values using $My_App::Constants::some_var. And that suits me very well.
--
KP
------------------------------
Date: Tue, 06 May 2003 17:47:42 GMT
From: PropART <temp1_NO_SPAM_@williamc.com>
Subject: Re: Design Opinions - Dealing with Constants
Message-Id: <3EB7F4B3.74FD93AA@williamc.com>
Kevin Vaughn wrote:
>
> I've created a large number of scripts that reference a single file
> containing all of my constants. I named the file environment.pl. The file
> mainly contains paths. I include environment.pl using a "required"
> statement.
>
> Is this good or bad design? I'm particularly interested in the downsides.
>
> Now a specific question - How do I get perl to stop giving me the error
> "Global symbol $constant_from_environment requires explicit package name at
> $line_number" when I have strict turned on?
>
> I'm trying to get my head around the lexical vs. package scope thing, but
> it's not coming easy for some reason. Do I have to go through creating a
> package, or can I use "our"?
>
> -Kevin
See thread above from last night, "Including files in Perl". It covers
almost exactly the same problem.
IMO it's good design, because you have all your constants in one place.
I'm no Perl expert, though.
--williamc
------------------------------
Date: 6 May 2003 10:59:59 -0700
From: genericax@hotmail.com (Sara)
Subject: Re: Design Opinions - Dealing with Constants
Message-Id: <776e0325.0305060959.37682877@posting.google.com>
"Kevin Vaughn" <kevin.vaughn@ttu.edu> wrote in message news:<vbfemoj1e9hba1@corp.supernews.com>...
> I've created a large number of scripts that reference a single file
> containing all of my constants. I named the file environment.pl. The file
> mainly contains paths. I include environment.pl using a "required"
> statement.
>
> Is this good or bad design? I'm particularly interested in the downsides.
>
> Now a specific question - How do I get perl to stop giving me the error
> "Global symbol $constant_from_environment requires explicit package name at
> $line_number" when I have strict turned on?
>
> I'm trying to get my head around the lexical vs. package scope thing, but
> it's not coming easy for some reason. Do I have to go through creating a
> package, or can I use "our"?
>
> -Kevin
I'm not sure of the OO design implications, as I'm no OO purist, but I
"sort of" do this kind of thing as well. For example, I have a single
package that contains all of my "error objects and methods". If there
is an error string to be added or changed, I dont have to slog though
the 400 some odd package- I know they are all in there.
In your case I see something similar- if its a constant you know its
in the constant package. Seems like a good idea.
I guess the downside might be seperating the constants from their
"point of use", assuming some have a single point. However, in the
case of error objects and in your case, constants, its more common to
find multiple uses of the object. Therefore, it deserves a more global
locale, ergo its own package.
I'm not sure why you're saying you have to "go through creating a
package" like its some laborious effort. Its really not that dramatic-
add
package constantPak;
at the top, put
use constantPak;
in your code, oh and if you're using Perl n for any n<6, then of
course you'll need a TRUE value returned, hence a 1; at the end of the
package.
So there really isn't much to "go though". It's relatively painless-
certainly much easier than programming a PDP-8 from the frontpanel
binary switches! ha...
The downsides- well as I said in some cases the constant will be
removed from its point of use which may make it a bit more difficult
to maintain in some respects. But in our case I think things are MORE
mainatinable this way since everyone on the design team knows that all
errors are in the error package.
-Gx
------------------------------
Date: Tue, 6 May 2003 16:31:28 +0100
From: "Bigus" <bigus AT creationfactor DOT net>
Subject: Extracting part of the contents of one var into another
Message-Id: <b98khu$udk@newton.cc.rl.ac.uk>
When you have a string, such as:
$sentence = "my name is bob"
and you want to extract say the persons name into another variable, then I
would normally do something like this:
$name = $sentence;
$name =~ s/^.* //;
or
@temp = $sentence =~ /(\S+)$/;
$name = $temp[0];
There's a one-line way of doing this, isn't there - but can you tell me what
it is?
Bigus
------------------------------
Date: Tue, 06 May 2003 15:42:49 GMT
From: Michael Budash <mbudash@sonic.net>
Subject: Re: Extracting part of the contents of one var into another
Message-Id: <mbudash-1E426C.08424906052003@typhoon.sonic.net>
In article <b98khu$udk@newton.cc.rl.ac.uk>,
"Bigus" <bigus AT creationfactor DOT net> wrote:
> When you have a string, such as:
>
> $sentence = "my name is bob"
>
> and you want to extract say the persons name into another variable, then I
> would normally do something like this:
>
> $name = $sentence;
> $name =~ s/^.* //;
($name = $sentence) =~ s/^.* //;
>
> or
>
> @temp = $sentence =~ /(\S+)$/;
> $name = $temp[0];
($name) = $sentence =~ /(\S+)$/;
>
> There's a one-line way of doing this, isn't there - but can you tell me what
> it is?
hth-
--
Michael Budash
------------------------------
Date: 06 May 2003 15:58:28 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: Extracting part of the contents of one var into another
Message-Id: <slrnbbfmt4.tib.abigail@alexandra.abigail.nl>
Bigus () wrote on MMMDXXXV September MCMXCIII in
<URL:news:b98khu$udk@newton.cc.rl.ac.uk>:
`' When you have a string, such as:
`'
`' $sentence = "my name is bob"
`'
`' and you want to extract say the persons name into another variable, then I
`' would normally do something like this:
`'
`' $name = $sentence;
`' $name =~ s/^.* //;
`'
`' or
`'
`' @temp = $sentence =~ /(\S+)$/;
`' $name = $temp[0];
`'
`' There's a one-line way of doing this, isn't there - but can you tell me what
`' it is?
($name) = $sentence =~ /(\S+)$/;
Or:
($name = $sentence) =~ s/.* //;
Or:
$name = substr $sentence => 1 + rindex $sentence => " ";
Abigail
--
sub f{sprintf'%c%s',$_[0],$_[1]}print f(74,f(117,f(115,f(116,f(32,f(97,
f(110,f(111,f(116,f(104,f(0x65,f(114,f(32,f(80,f(101,f(114,f(0x6c,f(32,
f(0x48,f(97,f(99,f(107,f(101,f(114,f(10,q ff)))))))))))))))))))))))))
------------------------------
Date: 06 May 2003 15:54:40 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: Forcing an rvalue context on a scalar expression
Message-Id: <slrnbbfmm0.tib.abigail@alexandra.abigail.nl>
Brian McCauley (nobull@mail.com) wrote on MMMDXXXV September MCMXCIII in
<URL:news:u98ytk6yc9.fsf@wcl-l.bham.ac.uk>:
:} What is the simplest way to force an rvalue context on a scalar
:} expression?
:}
:} For example, consider:
:}
:} my @q;
:} foo($q[-1]);
:}
:} This generates "Modification of non-creatable array value attempted,
:} subscript -1" because it's trying to evaluate $q[-1] as an lvalue but
:} I just wanted foo's $_[0]=undef.
The problem is your use of -1 as index on an empty array. An empty array
has no elements, so it certainly doesn't have a last element. Due to Perl
using 'call by implicite reference' when calling functions with arguments,
it must know which element you mean, just in case foo assigns to $_ [0].
Consider this:
sub foo {$_ [0] = 1}
my @q;
foo $q [2];
print scalar @q;
This prints 3 because you created $q [2]. But suppose you had called
'foo $q [-1]', how large should @q be afterwards? Which element was
created?
:}
:} I can easily, but not very elegantly, fix this by saying:
:}
:} my $last_q = $q[-1];
:} foo($last_q);
:}
:} Or even...
:}
:} foo(my $last_q = $q[-1]);
:}
:} Or, without a varaiable, but more perversely...
:}
:} foo(&{sub { $q[-1] }});
:}
:} Anyone got any less ugly solutions?
I still don't know what you really want to do. Call foo with just one
argument, that argument being undefined? Just do:
foo (undef)
Or if the argument should be modifyable, do:
foo (my $foo)
Maybe you want @q to be something after foo is called, but I can't
deduce that from your post.
Abigail
--
sub camel (^#87=i@J&&&#]u'^^s]#'#={123{#}7890t[0.9]9@+*`"'***}A&&&}n2o}00}t324i;
h[{e **###{r{+P={**{e^^^#'#i@{r'^=^{l+{#}H***i[0.9]&@a5`"':&^;&^,*&^$43##@@####;
c}^^^&&&k}&&&}#=e*****[]}'r####'`=437*{#};::'1[0.9]2@43`"'*#==[[.{{],,,1278@#@);
print+((($llama=prototype'camel')=~y|+{#}$=^*&[0-9]i@:;`"',.| |d)&&$llama."\n");
------------------------------
Date: 06 May 2003 17:52:03 +0100
From: Brian McCauley <nobull@mail.com>
Subject: Re: Forcing an rvalue context on a scalar expression
Message-Id: <u9znm0587g.fsf@wcl-l.bham.ac.uk>
anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) writes:
> Brian McCauley <nobull@mail.com> wrote in comp.lang.perl.misc:
> > What is the simplest way to force an rvalue context on a scalar
> > expression?
> >
> > For example, consider:
> >
> > my @q;
> > foo($q[-1]);
> >
> > This generates "Modification of non-creatable array value attempted,
> > subscript -1" because it's trying to evaluate $q[-1] as an lvalue but
>
> Isn't that a bug? I mean, you're not assigning anything, are you?
No, see Abigail's explaination of what's going on.
> > foo(&{sub { $q[-1] }});
>
> Along the same line as sub {...}:
>
> foo( do { $q[-1] } );
>
> and, found coincidentally, and not quite understood:
>
> do( scalar $q[ -1]);
Strange! By rights _neither_ of those should work. Both do{} and
scalar() still return lvalues. If using $q[-1] in an lvalue context
where @q=() is wrong then it should always be wrong.
Hmmm... actually it's probably good that do{} works this way, perhaps
do{} should be documented as behaving this way then we could rely on
it.
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: Tue, 6 May 2003 15:33:00 +1000
From: "Anthony" <wallabyedward@hunterlink.net.au>
Subject: How do you create a biniary file in Perl.
Message-Id: <b98rik$ghj80$1@ID-82947.news.dfncis.de>
I have little information on the subject in the two texts I have.
Thanks In Advance
Anthony
------------------------------
Date: 6 May 2003 17:39:26 GMT
From: "Tassilo v. Parseval" <tassilo.parseval@rwth-aachen.de>
Subject: Re: How do you create a biniary file in Perl.
Message-Id: <b98s0e$gqb$1@nets3.rz.RWTH-Aachen.DE>
Also sprach Anthony:
> I have little information on the subject in the two texts I have.
This is actually very easy:
open NEW, ">file.bin" or die $!;
binmode NEW; # needed on some platforms
print NEW <<EODATA;
\000\002...
...
EODATA
close NEW;
I think you don't even need binmode() if you are writing the characters
implicitely by providing their octal or hexadecimal value. But then you
wont get a literal "\n" on Windows for example when you write one. It
would get translated into "\r\n".
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, 6 May 2003 15:53:33 +1000
From: "Anthony" <wallabyedward@hunterlink.net.au>
Subject: Re: How do you create a biniary file in Perl.
Message-Id: <b98sp0$gkfu9$1@ID-82947.news.dfncis.de>
Tassilo v. Parseval <tassilo.parseval@rwth-aachen.de> wrote in message
news:b98s0e$gqb$1@nets3.rz.RWTH-Aachen.DE...
: Also sprach Anthony:
:
: > I have little information on the subject in the two texts I have.
:
: This is actually very easy:
:
: open NEW, ">file.bin" or die $!;
: binmode NEW; # needed on some platforms
: print NEW <<EODATA;
: \000\002...
: ...
: EODATA
: close NEW;
:
Thanks, seems simple enough. How about reading and overwriting ? Do you need
something like C's equivalnet of fseek() ??
Anthony
: I think you don't even need binmode() if you are writing the characters
: implicitely by providing their octal or hexadecimal value. But then you
: wont get a literal "\n" on Windows for example when you write one. It
: would get translated into "\r\n".
:
: Tassilo
: --
:
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
:
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
:
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
------------------------------
Date: 6 May 2003 18:13:35 GMT
From: "Tassilo v. Parseval" <tassilo.parseval@rwth-aachen.de>
Subject: Re: How do you create a biniary file in Perl.
Message-Id: <b98u0f$kn7$1@nets3.rz.RWTH-Aachen.DE>
Also sprach Anthony:
> Tassilo v. Parseval <tassilo.parseval@rwth-aachen.de> wrote in message
> news:b98s0e$gqb$1@nets3.rz.RWTH-Aachen.DE...
>: Also sprach Anthony:
>:
>: > I have little information on the subject in the two texts I have.
>:
>: This is actually very easy:
>:
>: open NEW, ">file.bin" or die $!;
>: binmode NEW; # needed on some platforms
>: print NEW <<EODATA;
>: \000\002...
>: ...
>: EODATA
>: close NEW;
>:
> Thanks, seems simple enough. How about reading and overwriting ? Do you need
> something like C's equivalnet of fseek() ??
Yes, you will. This demonstrates most of the needed operations:
use Fcntl qw/:seek/; # import seek-constants
open F, "+<file.bin" or die $!; # open read/write
read F, my $buf, 4096; # 4096 bytes into $buf
print F, "..."; # "..." goes into F
seek F, SEEK_SET, 20; # jump to 20th byte
seek F, SEEK_CUR, 20; # 20 bytes ahead from the current pos
seek F, SEEK_END, -20; # 20 bytes before EOF
my $pos = tell F; # where am I right now?
Theoretically there is still the angle operator (<>):
while (<F>) { ... }
It is less useful on binary files but you could set $/ to a reference to
a number to read that many bytes each time (and thus make it useful
again):
local $/ = \4096;
while (<F>) {
# $_ contains the next 4096 bytes
}
This is the same as:
while (read F, local $_, 4096) {
...
}
Check the documention for each of the above functions and some more.
From your shell:
perldoc -f open
seek
tell
read
sysread
syswrite
binmode
sysopen
eof
perldoc perlop # for the I/O operators such as <>
perldoc perlvar # for the special variable $/
perldoc perlopentut # all you can do with open()
perldoc Fcntl # this module exports some constants
I think there is also a module available from the CPAN. File::Binary or
so, but it's only for reading binary files as I recall.
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: 06 May 2003 15:09:49 GMT
From: ctcgag@hotmail.com
Subject: Re: How to fork and PID - revisited
Message-Id: <20030506110949.812$2V@newsreader.com>
"Tony" <anthony@no_spam.movielink.net.au> wrote:
>
> START: open (FILE, $file) or die "Could not open: $file\n";
> foreach my $schedule(<FILE>)
> {
> my $local_time_day = day_time(); # code not shown for this
> my $time_day = (extracted from $file); # code not shown for this
> my $data = (extracted from $file); # code not shown for this
> if ("$local_time_day eq $time_day") # Compare for a match...
> {
> process_data($data);
> }
> }
>
> goto START;
>
> The above continuesly scans $file for a day/time match and passes
> a list of files to the sub "process_data" when a match happens.
>
> The sub "process_data" then sends the list of files, one a a time,
> to another sub "process_file". This sub will process the file,
> return for the next file and so on until all have been processed.
>
> All runs fine before adding any "fork" functionality.
>
> I tried the following:
>
> if ("$local_time_day eq $time_day") # Compare for a match...
> {
> if (fork() == 0)
> {
> my $child_pid = $$; # Get child PID
> close SCHEDULE; # Close previously opened file handle
> process_data($data); # Send data to be processesed by sub
> exit; # Exit child if processing completed
> }
> }
> }
Your lexical variable $child_pid is useless. It isn't used inside of it's
scope, and it doesn't exist outside of it's scope. Also, it's not
magically tied to any variable in the parent process, which I think is what
you are hoping.
You're closing a filehandle named SCHEDULE, but you show opening one called
FILE.
> That does work (Start a new process) but various docs talk about signals,
> kids, $$ for PID etc. which has confused me.
>
> The "process_file" program is something like:
>
> sub process_file
> {
> ...
> my $external = "/some/path/program";
> my $file = $_[1];
> my $params = "abc";
> system "$external $params $file";
> }
>
> It would seem that the external file processor should
> be started something like:
>
> sub process_file
> {
> ...
> if (fork() == 0)
> {
> $pocess_file_pid = $$; # Get players PID before EXEC
> exec "$external $params $file"; or die "Could not exec $!\n";
> }
> }
Why do you want two different forks in the same program? If you're
having problems with fork, try to get away with as few forks as possible.
>
> I see a small problem here - this snippet will return as soon as
> it has strated the external program, resulting in an attempt to
> process the next file - this will "blow up" as the current file
> is still being processed.
Uh, if you want to process stuff serially, process it serially.
> I somehow need to have the "process_data"
> sub check for the process running and when it is finished, send
> the next file.
Why are you explicitly forking in the first place then?
There is already an excellent way to start a process and then
wait until it's done--the system command.
> Could i use "waitpid" for that by checking the $process_pid ?
No. Because you don't seem to have a variable $process_pid. Or at
least if you do, you don't seem to set it to the childs pid.
> What is the right way to store the pid's and where? I have defined
> a $process_file_id at the beginning and then substituting its
> value with $$ after fork.
The only (good) way for the parent to know the child's pid is to capture it
from the fork command itself: my $child_pid=fork;
Once you are in the child, the child can use $$ to get the child pid, but
can't easily pass that up to the parent.
If the child needs the parent's pid, the parent should: my $parent_pid=$$;
before the fork, and the child can read it from $parent_pid.
> I want to be able to store the $process_file_pid and $child_pid somewhere
> so that I can kill both processes if a new event is trigered by my
> main loop at the beginning. This will enable me to run a new
> set of files.
There is no natural way for a process to get it's grandchild's pid.
You could use IPC from the child to the parent to report the grandchild's
pid, or you could just have the process signal it's child, and have the
child catch that signal and in turn signal the grandchild. But I think
you're best bet is to do away with the multiple generations.
>
> It is a bit of a moutfull, am I on the right path?
I don't know to where you are trying to go.
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service New Rate! $9.95/Month 50GB
------------------------------
Date: Tue, 06 May 2003 14:23:53 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: Length of line
Message-Id: <ZzPta.44831$B61.24699@nwrddc01.gnilink.net>
Tyro wrote:
> How can I determine the length of line I get from file?
> length($_) isn't good enough - it counts any white space (\r\t\n\f) as
> 1.
> But what if my tab (\t) actually takes four (4) spaces.
You have a misconception here.
The tab character is one single character. If your display device expands
that into 4 characters or 24 characters is a matter of the display device.
Neither the tab character itself nor Perl would know anything about that.
> The purpose of this is to check during compilation that my source
> files lines not longer than 80 (some of platforms can't stand it
> longer).
Then your only option is to find out what how your display device displays
those characters and simulate that behaviour in your Perl scripts.
But please be warned that as soon as someone uses a different display device
or uses the same display device with different settings (e.g. many people
set tab for 2 characters because 4 appears to be too long for code
indentation; other people set it to 8 characters because that is the old
standard from mechanical typewriters) your calculations will be wrong.
The right solution is not use tabs but to use real spaces instead.
jue
------------------------------
Date: Tue, 06 May 2003 14:28:20 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: Length of line
Message-Id: <8EPta.44847$B61.36156@nwrddc01.gnilink.net>
Nils Petter Vaskinn wrote:
> On Tue, 06 May 2003 04:09:57 -0700, Tyro wrote:
>
>> Hi all!
> [snip counting characters]
>> The purpose of this is to check during compilation that my source
>> files lines not longer than 80 (some of platforms can't stand it
>> longer).
>
> I think you have got the answer to your question how to count with the
> with of a tab character. But I suspect you're trying to solve the
> wrong problem.
>
> If your compiler chokes on 80 characters it's likly that a tab
> character counts as one character. Try to create a line with less
> than 80 characters (tabs counting as one) but a length more than 80
> when tabs are expanded. If that compiles on all platforms you can use
> length($_) without worry.
Of course the better answer is: DDT
"Don't Do That"
Tabs in source code are a nuicance for anyone who needs to read/maintain the
code later and uses a different expansion then the original author because
it screws up any indentation.
jue
------------------------------
Date: Tue, 06 May 2003 15:03:49 GMT
From: "Nils Petter Vaskinn" <no@spam.for.me.invalid>
Subject: Re: Length of line
Message-Id: <pan.2003.05.06.15.03.49.406011@spam.for.me.invalid>
On Tue, 06 May 2003 14:28:20 +0000, Jürgen Exner wrote:
>
> Of course the better answer is: DDT
> "Don't Do That"
>
> Tabs in source code are a nuicance for anyone who needs to read/maintain the
> code later and uses a different expansion then the original author because
> it screws up any indentation.
As logs it is consistent use of tabs and not mixed with spaces it's not
that bad.
And since the OP has problems with compilers that chokes on log lines tabs
seems a better solution for this case at least.
regards
NP
------------------------------
Date: Tue, 06 May 2003 18:02:39 +0200
From: "Janek Schleicher" <bigj@kamelfreund.de>
Subject: Re: Length of line
Message-Id: <pan.2003.05.06.16.00.38.15675@kamelfreund.de>
Tyro wrote at Tue, 06 May 2003 04:09:57 -0700:
> I'm a new in Perl so sorry if it trivial.
>
> How can I determine the length of line I get from file?
> length($_) isn't good enough - it counts any white space (\r\t\n\f) as
> 1.
> But what if my tab (\t) actually takes four (4) spaces.
>
> The purpose of this is to check during compilation that my source
> files lines not longer than 80 (some of platforms can't stand it
> longer).
I would try something like, assuming
$_ = "bla bla\tblabla\n";
use Text::Tabs; # The module doing the dirty job
$Text::Tabs::tabstop = 4;
chomp( my $line = $_ ); # copy line and remove trailing end of line
my ($expanded_line) = expand($line);
print length($expanded_line);
# prints 14
Greetings,
Janek
------------------------------
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 4945
***************************************