[8891] in Perl-Users-Digest
Perl-Users Digest, Issue: 2508 Volume: 8
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue May 5 18:17:44 1998
Date: Tue, 5 May 98 15:01:53 -0700
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, 5 May 1998 Volume: 8 Number: 2508
Today's topics:
Re: How to delete an element in an array? <lr@hpl.hp.com>
Re: How to delete an element in an array? <lr@hpl.hp.com>
Re: I/O to serial device (Jonathan Stowe)
Re: implemented by XS functions really faster? (Juergen Heinzl)
Re: implemented by XS functions really faster? <rootbeer@teleport.com>
Re: Installation problems <rootbeer@teleport.com>
Re: is it possible to parameterize a class name? <jkry3025@comenius.ms.mff.cuni.cz>
Re: is it possible to parameterize a class name? <tchrist@mox.perl.com>
Re: Looking for Perl parser for 'C' language input file <grinch@whoville.com>
Newbie Q: access file handles in a subroutine? <news@REMOVETHISPART.jgoldberg.prestel.co.uk>
Re: Newbie Q: access file handles in a subroutine? <rootbeer@teleport.com>
Re: Open to suggestions <rootbeer@teleport.com>
out of memory problem <brian_wilson@om.cv.hp.com>
Re: out of memory problem <rootbeer@teleport.com>
Re: passing a copy of data refered to via a hard ref <rootbeer@teleport.com>
Re: perl script to trans smtp mail text to html (Earl Hood)
Re: perlio.h error using cc?? (Jonathan Stowe)
Re: pod2texinfo? (Jan Dubois)
Re: Running script as background program. (Jonathan Stowe)
Re: Scheduling a perl script under NT, makes stat() ret <jkry3025@comenius.ms.mff.cuni.cz>
Re: Scheduling a perl script under NT, makes stat() ret <rootbeer@teleport.com>
Re: Scheduling a perl script under NT, makes stat() ret (Jonathan Stowe)
Re: Split problem Form <jkry3025@comenius.ms.mff.cuni.cz>
Re: Split problem Form <rootbeer@teleport.com>
Re: Using executable pathname to define include path <glew@cs.wisc.edu>
Re: Using executable pathname to define include path <glew@cs.wisc.edu>
Re: Using executable pathname to define include path <tchrist@mox.perl.com>
Digest Administrivia (Last modified: 8 Mar 97) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Tue, 5 May 1998 13:49:33 -0700
From: "Larry Rosler" <lr@hpl.hp.com>
Subject: Re: How to delete an element in an array?
Message-Id: <6inu0u$qmn@hplntx.hpl.hp.com>
[posted and mailed]
Rich Tsui wrote in message <354F3CD1.18FC648@cbmi.upmc.edu>...
>Hi Perl guru,
>
> Could any one out there help me to find a way to delete an element
in an
>array?
>What I know for the deleted item is the content instead of the index in
the
>array.
>For example, I have an array @foo=('Jan', 'Feb', 'Dec', 'Bar', 'Mar')
and I
>would
>like to delete item 'Bar' in array @foo without knowing the index of
the item
>'Bar'.
>
>Any suggestion?
>
>Rich Tsui
>University of Pittsburgh
>
Pretty basic.
@foo = grep $_ ne 'Bar', @foo;
I'd recommend "Learning Perl' from O'Reilly & Associated.
--
Larry Rosler
Hewlett-Packard Laboratories
lr@hpl.hp.com
------------------------------
Date: Tue, 5 May 1998 14:21:52 -0700
From: "Larry Rosler" <lr@hpl.hp.com>
Subject: Re: How to delete an element in an array?
Message-Id: <6invtl$rio@hplntx.hpl.hp.com>
[posted and mailed]
Mark-Jason Dominus <mjd@op.net> wrote in message
<6ink5q$k5u$1@monet.op.net>...
> In article <354F3CD1.18FC648@cbmi.upmc.edu>,
> Rich Tsui <tsui@cbmi.upmc.edu> wrote:
> >For example, I have an array @foo=('Jan', 'Feb', 'Dec', 'Bar', 'Mar')
and I
> >would
> >like to delete item 'Bar' in array @foo without knowing the index of
the item
> >'Bar'.
> >
> >Any suggestion?
>
> You should be using a hash instead.
>
> %foo = map {($_ => 1)} @foo;
>
> # Now forget about @foo, and never use it again.
> undef @foo;
>
> $foo{new_item} = 1; # Insert new item into list
> delete $foo{Bar}; # Delete item from list
>
> foreach $item (keys %foo) {
> # Do something for each item in the list
> }
I posted my response to this question (and got a nice thank-you for the
one-liner) before I read your response. I can't decide if you were
being facetious (if so, why?) or instructive. If the latter, why not
teach him hash slices too:
@foo{@foo} = (1) x @foo; # 1 as values
or
@foo{@foo} = @foo; # keys as values
I'm sure that would mystify him even more than the "map" way, and it's
*lots* faster. (I am indebted to Uri Guttman for this insight.)
By the way, what if he liked the order of the items in the array (such
as the names of months)? How would you get it back to him?
--
Larry Rosler
Hewlett-Packard Laboratories
lr@hpl.hp.com
------------------------------
Date: Tue, 05 May 1998 16:57:18 GMT
From: Gellyfish@btinternet.com (Jonathan Stowe)
Subject: Re: I/O to serial device
Message-Id: <354f3c30.13559334@news.btinternet.com>
On Mon, 04 May 1998 17:31:58 -0400, Gary Trachier wrote :
>Hi All,
>
>I am working on a UNIX system. I need to do I/O to a serial device;
>specifically a modem that is connected as /dev/ttyb. I am using the
>following program which uses pieces from the Perl FAQ. I find though
>that the open() always fails. I know that the modem is not in use. Any
>suggestions? Thanks very much.
>
>use IO::Handle;
>MAIN: {
> my $modem = '/dev/ttyb'; # the modem device
> my $response; # response from the modem
> local *MODEM;
>
> open (MODEM, "<+ $modem") || die ("Can't open $modem."); # open the
>modem
>
You should print $! for a better idea as to why you cant open the
device.
> print MODEM "ATZ\n"; # send something to it
> MODEM->autoflush(1);
>
> sleep 4;
> print "Done sleeping.\n";
>
> $response = <MODEM>; # read the response
>
> print "\$response = $response";
>
> close (MODEM); # close the device
>}
>
I would check the permissions on your device. Many programs that deal
with the serial ports run setuid and/or change the permissions on
them.
/J\
Jonathan Stowe
Some of your questions answered:
<URL:http://www.btinternet.com/~gellyfish/resources/wwwfaq.htm>
------------------------------
Date: 5 May 1998 20:26:59 GMT
From: juergen@unicorn.noris.de (Juergen Heinzl)
Subject: Re: implemented by XS functions really faster?
Message-Id: <slrn6kuth6.e8.juergen@unicorn.noris.de>
In article <354F2531.80A0FF00@interactive.ibm.com>, John Call wrote:
>I was browsing through a readme file and it said that this particular
>code "is implemented by XS functions. This makes it about 20 times
>faster than the old implementation as pure Perl."
>
>Is this an accurate statement? If so why is it so much faster and why
>isn't more code written that uses the XS implementation?
You probably want to read perlxs. It's an interface to access C library
functions and so it really depends on your requirements and if you
need it you might be interested in swig too.
Bye, Juergen
--
\ Real name : Juergen Heinzl \ no flames /
\ EMail Private : unicorn@noris.de \ send money instead /
\ Phone Private : +49 911-4501186 \ /
------------------------------
Date: Tue, 05 May 1998 21:22:38 GMT
From: Tom Phoenix <rootbeer@teleport.com>
To: John Call <johnc@interactive.ibm.com>
Subject: Re: implemented by XS functions really faster?
Message-Id: <Pine.GSO.3.96.980505142038.13101V-100000@user2.teleport.com>
On Tue, 5 May 1998, John Call wrote:
> I was browsing through a readme file and it said that this particular
> code "is implemented by XS functions. This makes it about 20 times
> faster than the old implementation as pure Perl."
>
> Is this an accurate statement?
I hope that the author benchmarked it before making the claim, but I see
no special reason to doubt it. XS code can be 200 times faster than the
corresponding Perl, especially if it uses C's strengths and Perl's
weaknesses.
> If so why is it so much faster and why isn't more code written that uses
> the XS implementation?
Because it's harder to write in C than in Perl, and the result is less
portable. Hope this helps!
--
Tom Phoenix Perl Training and Hacking Esperanto
Randal Schwartz Case: http://www.rahul.net/jeffrey/ovs/
------------------------------
Date: Tue, 05 May 1998 20:32:24 GMT
From: Tom Phoenix <rootbeer@teleport.com>
To: Srinivas MV <Srinivas.MV@Analog.Com>
Subject: Re: Installation problems
Message-Id: <Pine.GSO.3.96.980505132845.13101N-100000@user2.teleport.com>
On Tue, 5 May 1998, Srinivas MV wrote:
> Can someone tell me where I can find info on probalble
> workarounds/pointers to problems encountered during perl installation?
The INSTALL document which comes with the distribution should cover this.
Hope this helps!
--
Tom Phoenix Perl Training and Hacking Esperanto
Randal Schwartz Case: http://www.rahul.net/jeffrey/ovs/
------------------------------
Date: Tue, 05 May 1998 22:02:09 -0700
From: Jan Krynicky <jkry3025@comenius.ms.mff.cuni.cz>
Subject: Re: is it possible to parameterize a class name?
Message-Id: <354FEED1.4393@comenius.ms.mff.cuni.cz>
Ed.Q.Bridges wrote:
>
> >This has been asked a bunch of times recently. Maybe it ought to be
> >added to the FAQ list... Search DejaNews for the subject line
> >"*Really* dynamic function loaing?" There's a thorough answer in that
> >thread. You should also
>
> a search on dejanews using this subject line (both with and without
> typo) returned no results. do you remember any of the author's names?
>
> >
> > C:\> perldoc -f use
> >
> >which will explain to you that use Foo; is a synonym for
> >
> > BEGIN { require Foo; import Foo }
> >
>
> thanks, i knew that (and tried it) already. the problem is that
> a BEGIN forces the require at compile time, not run time.
> i'm trying to insert a value to be required at runtime.
>
> when i code this:
> my($foo) = shift;
> BEGIN { require $foo; import $foo; }
> i get this:
> Null filename used at ./main.pl line 9.
> BEGIN failed--compilation aborted at ./main.pl line 9.
>
> TIA
> --e--
If you want the module to be "used" in compile time, you have to do all
the necessary
tests to find out what module to use also in compile time, that is in a
BEGIN{}
block, textualy before the BEGIN{ require $foo; import $foo; }.
If you do not insist in using it in compile time, omit the BEGIN{} at
all.
Read man perlfunc to find out what BEGIN{} means.
HTH, Jenda
------------------------------
Date: 5 May 1998 21:03:00 GMT
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: Re: is it possible to parameterize a class name?
Message-Id: <6inuq4$ik8$1@csnews.cs.colorado.edu>
[courtesy cc of this posting sent to cited author via email]
In comp.lang.perl.misc,
Jan Krynicky <jkry3025@comenius.ms.mff.cuni.cz> writes:
:If you want the module to be "used" in compile time, you have to do all
:the necessary
:tests to find out what module to use also in compile time, that is in a
:BEGIN{}
:block, textualy before the BEGIN{ require $foo; import $foo; }.
(You need to fix your line wrapping. It's in MSFMH mode.)
The code you have there doesn't work. Here's an excerpt from the
upcoming Perl Cookbook which explains what the problem with yours is,
shows what you really have to do, and tells you why.
--tom
Delaying use Until Run-Time
PROBLEM: You have a module that you don't need to load each time the
program runs, or whose inclusion you wish to delay until after your
the program starts up.
SOLUTION: Either break up the `use' into its separate `require'
and `import' components, or else employ the `use autouse' pragma.
DISCUSSION
Programs that check their arguments and abort with a usage message
on error have no reason to load modules they'll never use. This just
delays the inevitable and annoys their users. But since those `use'
statements happen during compilation not execution, much like C's
`#include' directives.
An effective strategy in this case is to place argument checking in a
BEGIN block before loading the modules. Here's the start of a program
that checks to make sure it was called with exactly two arguments,
which must be whole numbers, before going on to load the modules it
will need.
BEGIN {
unless (@ARGV == 2 && grep(/^\d+$/, @ARGV) == 2) {
die "usage: $0 num1 num2\n";
}
}
use Some::Module;
use More::Modules;
A related situation arises in programs that don't always use the same
set of modules every time they're run. For example, the *factors*
program from Chapter 2 only needs the infinite precision arithmetic
library if the -b command-line flag was supplied. A `use' statement
would do no good within a conditional, because it's evaluated at
compile time, long before the `if' can be checked. So we'll use a
`require' instead.
if ($opt_b) {
require Math::BigInt;
}
Because Math::BigInt is an object-oriented module instead of a
traditional one, no import was needed. If you have an import list,
specify it with a `qw//' construct as you would with `use'. For
example, rather than this:
use Fcntl qw(O_EXCL O_CREAT O_RDWR);
You might say this instead:
require Fcntl;
Fcntl->import(qw(O_EXCL O_CREAT O_RDWR));
However, delaying the import until run-time does mean that the rest of
your program will not be subject to any imported semantic changes that
the compiler would have seen if you'd used a `use'. In particular,
subroutine prototypes, operator overloading, and overriding of
built-in functions will not be seen in time.
You might want to encapsulate this delayed loading in a
subroutine. The following approach, deceptively simple,
does not work:
sub load_module {
require $_[0];
import $_[0];
}
The reasons for this failure are somewhat subtle. Imagine calling
`require' with "Math::BigFloat". If that's a bareword, the double
colon gets converted into your operating system's path separator,
and a trailing `.pm' added. But as a simple variable, it's a literal
filename. The second problem is even worse. Perl doesn't have a
built-in `import' function. Instead, there's a class method named
`import'. So a more proper implementation would look more like this:
load_module('Fcntl', qw(O_EXCL O_CREAT O_RDWR));
sub load_module {
eval "require $_[0]";
die if $@;
$_[0]->import(@_[1 .. $#_]);
}
But this still isn't perfectly correct in the general case. It really
shouldn't import those symbols into its own package. It should put
them into its callers package. While we could account for this,
the whole procedure just gets increasingly messy.
An convenient alternative is the `use autouse' pragma. New as of the
5.004 release of Perl, this directive can save time on infrequently
loaded functions by delaying their load until they're actually used.
use autouse Fcntl => qw( O_EXCL() O_CREAT() O_RDWR() );
Make sure to check the online documentation for this pragma to learn
its various caveats and provisos.
--
Tom Christiansen tchrist@jhereg.perl.com
If I don't document something, it's usually either for a good reason,
or a bad reason. In this case it's a good reason. :-)
--Larry Wall in <1992Jan17.005405.16806@netlabs.com>
------------------------------
Date: Tue, 5 May 1998 17:00:16 -0400
From: "Grinch" <grinch@whoville.com>
Subject: Re: Looking for Perl parser for 'C' language input files
Message-Id: <6intm5$f6e@fridge.shore.net>
James O. Payne, Jr. wrote in message <354F60FA.6F7F@us.oracle.com>...
>I have a requirement to write a utility to do some not-so-simple
>search, modify, and replace type work on a large base of 'C' code.
...snip...
You may want to have a look at the Parse::RecDescent module at CPAN. <URL:
http://www.perl.com>
HTH!
-grinch
--
-----
"It's not real coffee unless the sugar cubes float." - me
Sherm Pendley
grinch@whoville.com
http://www.whoville.com
------------------------------
Date: Tue, 5 May 1998 22:13:34 +0100
From: "Jeremy Goldberg" <news@REMOVETHISPART.jgoldberg.prestel.co.uk>
Subject: Newbie Q: access file handles in a subroutine?
Message-Id: <6inven$68n$1@plug.news.pipex.net>
How do you get something like the code below to work? I'm trying to make a
wrapper for the open() function that makes a few attempts before giving up
(in case the file in question is being written to etc.).
The call would be AttemptOpen( FILEHANDLE, filename ), and the subroutine
(which doesn't work :) reads as:
sub AttemptOpen
{
# Attempt to open a file 5 times, then fail
local( $index );
for( $index = 5; $index; $index-- )
{
if( open( *_1, $_2 ) )
{ return( 1 ); }
sleep( 1 ); # Pause a little first
}
return( 0 );
}
Presumably this IS possible in PERL? Or is there a better way of doing what
I'm looking for than just polling the file like this?
- Jeremy Goldberg
------------------------------
Date: Tue, 05 May 1998 21:47:50 GMT
From: Tom Phoenix <rootbeer@teleport.com>
To: Jeremy Goldberg <news@REMOVETHISPART.jgoldberg.prestel.co.uk>
Subject: Re: Newbie Q: access file handles in a subroutine?
Message-Id: <Pine.GSO.3.96.980505144432.13101b-100000@user2.teleport.com>
On Tue, 5 May 1998, Jeremy Goldberg wrote:
> local( $index );
You probably should be using my() variables instead of local.
> for( $index = 5; $index; $index-- )
There's nothing _wrong_ with that, but the more perlish way would be
something like this:
for $index (1..5) { ... }
> if( open( *_1, $_2 ) )
I think you want to know how to pass and return filehandles (and other
values) to your subroutine. The perlsub manpage explains that.
Hope this helps!
--
Tom Phoenix Perl Training and Hacking Esperanto
Randal Schwartz Case: http://www.rahul.net/jeffrey/ovs/
------------------------------
Date: Tue, 05 May 1998 20:18:41 GMT
From: Tom Phoenix <rootbeer@teleport.com>
To: Curtis Hoffmann <curtish@gte.net>
Subject: Re: Open to suggestions
Message-Id: <Pine.GSO.3.96.980505131332.13101M-100000@user2.teleport.com>
On Tue, 5 May 1998, Curtis Hoffmann wrote:
> Subject: Open to suggestions
Please check out this helpful information on choosing good subject
lines. It will be a big help to you in making it more likely that your
requests will be answered.
http://www.perl.com/CPAN/authors/Dean_Roehrich/subjects.post
> What I'm asking for is pointers to a Perl script that can take a
> block of text data and store it to a particular subdirectory on the
> server.
That's easy to do with Perl's file operations, of course.
> The file names should be sequential, (i.e. - File0001.txt, File0002.txt,
> File0003.txt, etc.)
That's easy to do, although there is a worry about concurrency. You may
need to use flock.
> And, if there are any security holes here, it'd be nice to know about
> them.
There are always security holes! Maybe your system administrator just
posted the root password to alt.2600. But such security holes are beyond
the scope of a Perl newsgroup. Perl-related security issues are discussed
in perlsec.
Hope this helps!
--
Tom Phoenix Perl Training and Hacking Esperanto
Randal Schwartz Case: http://www.rahul.net/jeffrey/ovs/
------------------------------
Date: Tue, 05 May 1998 13:00:22 -0700
From: Brian Wilson <brian_wilson@om.cv.hp.com>
Subject: out of memory problem
Message-Id: <354F6FD6.6A5C@om.cv.hp.com>
I get an 'out of memory!' error while building a scalar with an
sprintf(). The machine is running HP-UX 10.20 and perl5.004 with lots of
memory.
I create an array @keys from a database query and use the array to build
a list that I can pass to another query e.g.:
select t.param1, t.param2
from table t
where t.param3 in $in_list
code snippet:
@keys = `echo "$sql" | dbaccess dbspace 2> /dev/null`;
$in_list = "(";
foreach $k (@keys)
{
chop($k);
$k =~ s/^\s+//;
next if $k =~ /^$/;
next if $k !~ /^\d+/;
$in_list = sprintf("%s%s,", $in_list, $k);
}
$in_list =~ s/,$/)/;
print "$in_list\n";
$in_list looks like (1,2,3,4,5,etc)
Anyway, when the number of array elements grows beyond 3800, I get an
'out of memory!' error inside the above loop and the program aborts.
The length in bytes of $in_list at the time of the error is a little
over 18k.
Anybody have any ideas about why this would happen?
Thanks,
Brian Wilson
Hewlett-Packard Co.
------------------------------
Date: Tue, 05 May 1998 21:33:25 GMT
From: Tom Phoenix <rootbeer@teleport.com>
To: Brian Wilson <brian_wilson@om.cv.hp.com>
Subject: Re: out of memory problem
Message-Id: <Pine.GSO.3.96.980505142950.13101X-100000@user2.teleport.com>
On Tue, 5 May 1998, Brian Wilson wrote:
> I get an 'out of memory!' error while building a scalar with an
> sprintf().
Well, that may be a bug in perl...
> $in_list = sprintf("%s%s,", $in_list, $k);
...But that's probably not the best way to use Perl, anyway! :-) Try this
line instead.
$in_list .= "$k,";
Of course, you could probably do what you want with join. Hope this helps!
--
Tom Phoenix Perl Training and Hacking Esperanto
Randal Schwartz Case: http://www.rahul.net/jeffrey/ovs/
------------------------------
Date: Tue, 05 May 1998 20:45:01 GMT
From: Tom Phoenix <rootbeer@teleport.com>
To: Glenn Morgan <gmorgan@photographics.co.uk>
Subject: Re: passing a copy of data refered to via a hard ref
Message-Id: <Pine.GSO.3.96.980505134333.13101O-100000@user2.teleport.com>
On Tue, 5 May 1998, Glenn Morgan wrote:
> I want to pass a copy of this structure to a subroutine so that if I
> mistakenly modify its contents it will not affect the original data, any
> ideas?
Yes; my idea is that you should do that. There may be a module which will
help you. If you get stuck along the way, please let us know where you've
gotten stuck. Hope this helps!
--
Tom Phoenix Perl Training and Hacking Esperanto
Randal Schwartz Case: http://www.rahul.net/jeffrey/ovs/
------------------------------
Date: 5 May 1998 17:06:06 GMT
From: ehood@medusa.acs.uci.edu (Earl Hood)
Subject: Re: perl script to trans smtp mail text to html
Message-Id: <6ingtu$dve@news.service.uci.edu>
[mail & posted]
In article <6il2mv$h7h$1@nnrp1.dejanews.com>, <ageorge@best.com> wrote:
>Does anyone know where I can find a script that will translate smtp mail text
>to html?
I am guessing you mean RFC 822, and/or MIME, formatted mail. If so,
goto <URL:http://www.oac.uci.edu/indiv/ehood/mhonarc.html>.
--ewh
--
Earl Hood | University of California: Irvine
ehood@medusa.acs.uci.edu | Electronic Loiterer
http://www.oac.uci.edu/indiv/ehood/ | Dabbler of SGML/WWW/Perl/MIME
------------------------------
Date: Tue, 05 May 1998 16:57:20 GMT
From: Gellyfish@btinternet.com (Jonathan Stowe)
Subject: Re: perlio.h error using cc??
Message-Id: <354f3d83.13898771@news.btinternet.com>
On Mon, 04 May 1998 14:58:57 -0700, JDS wrote :
>We just installed 5.004.04 on Solaris.
>I'm trying to compile and run a simple C test
>that #includes perl.h.
>Using CC, I get syntax error on line 137 of perlio.h.
>
>If I use GCC all is fine.
>
>Can I still use CC with 5.004.04?
>
I cant remember the exact details (too many compilers to many beers),
but I had a similar problem with Solaris. There is a variant compiler
called c89 that lives in some /opt/SunWSPro/ (or something like that)
that will compile happily.
/J\
Jonathan Stowe
Some of your questions answered:
<URL:http://www.btinternet.com/~gellyfish/resources/wwwfaq.htm>
------------------------------
Date: Tue, 05 May 1998 23:49:50 +0200
From: jan.dubois@ibm.net (Jan Dubois)
Subject: Re: pod2texinfo?
Message-Id: <355488f3.13575370@news2.ibm.net>
[mailed & posted]
d3h486@emsl.pnl.gov (John L. Daschbach) wrote:
>I've searched a few of the pages AltaVista returns when I search on
>'pod2texinfo' but when I search these pages I don't find a mention of
>such a beast. Does anybody know of one? Also, or alternatively is
>there an elisp pod viewer?
You should have searched for pod2info instead :-)
Anyways, you can find pod2texi and also the 5.004_04 pod files (sans
FAQ, but including a lot of modules) in:
http://www.perl.com/CPAN-local/authors/Krishna_Shamu_Sethuraman/
-Jan
------------------------------
Date: Tue, 05 May 1998 16:57:03 GMT
From: Gellyfish@btinternet.com (Jonathan Stowe)
Subject: Re: Running script as background program.
Message-Id: <354f15d0.5031898@news.btinternet.com>
On Mon, 04 May 1998 23:49:10 -0700, Derek Blandford wrote :
>I've created a chat written in Perl that uses sockets. The main program
>needs to run continously, meaning I need to be able to shut down telnet
>without it closing the program. I'm not sure if this is a Perl question
>or a Unix question, so it's posted in both. Please reply at least via
>email....thanks a million.
>
A perl answer : fork
Example page from page 216 of pink camel:
unless(fork){
unless(fork){
sleep 1 until getppid == 1;
#
# Your code here
#
exit(0)
}
exit(0);
}
wait;
Thus preventing the creation of zombie process.
A unix solution:
$ nohup yourscript.pl &
/J\
Jonathan Stowe
Some of your questions answered:
<URL:http://www.btinternet.com/~gellyfish/resources/wwwfaq.htm>
------------------------------
Date: Tue, 05 May 1998 22:14:00 -0700
From: Jan Krynicky <jkry3025@comenius.ms.mff.cuni.cz>
To: Michael Kristensen <mk@eradic8.dk>
Subject: Re: Scheduling a perl script under NT, makes stat() return 1 no matter what!?
Message-Id: <354FF198.3CB9@comenius.ms.mff.cuni.cz>
Michael Kristensen wrote:
>
> I have made this function to automatically delete some files if they
> become more than a week old, in conjunction with some archivelogs to
> an Oracle database:
>
> ------------
> opendir(DIR, $directory);
> @arch_files = sort(grep(/001$/, readdir(DIR)));
> closedir(DIR);
>
> foreach (@arch_files) {
> $mtime = (localtime((stat($_))[9]))[3];
> if (($mtime-7) >= 0) {
> system "cmd /c del $directory\\$_\n";
> }
> }
> -------------
>
> If I run this script manually it does delete the files as it is
> surpose to do. But if I set the Windows NT Scheduler to run the
> script, it doesn't.
> It seems that stat() returns 1 on all files, no matter what the real
> motification date is.
>
> I've tried to run the Schedule service, both as "localsystem" and as a
> specific account, but with no luck.
>
> Any help would be appreciated. Please also reply by e-mail, since I
> don't read this conference often.
>
> Thanks in advance,
> Michael
1) I'd think the problem is with paths, stat simply isn't able to find
the files.
Either cwd to the directory where the files reside or prepend the $_
by the name of the directory.
2) It's silly to call external programs from tasks you may do from
within perl.
Use unlink($_) instead of system("cmd /c del $directory\\$_\n")!
I would do:
use Cwd;
chdir $directory or die "Cannot cwd to '$directory'! ($!)\n";
opendir(DIR, '.');
@arch_files = sort(grep(/001$/, readdir(DIR)));
closedir(DIR);
foreach (@arch_files) {
$mtime = (localtime((stat($_))[9]))[3];
if (($mtime-7) >= 0) {
unlink $_;
}
}
HTH, Jenda
------------------------------
Date: Tue, 05 May 1998 20:54:24 GMT
From: Tom Phoenix <rootbeer@teleport.com>
To: Michael Kristensen <mk@eradic8.dk>
Subject: Re: Scheduling a perl script under NT, makes stat() return 1 no matter what!?
Message-Id: <Pine.GSO.3.96.980505134623.13101P-100000@user2.teleport.com>
On Tue, 5 May 1998, Michael Kristensen wrote:
> opendir(DIR, $directory);
It's probably a good idea to check the return value of opendir, since it
won't always work. But I don't _think_ that's the problem in this case.
> @arch_files = sort(grep(/001$/, readdir(DIR)));
> closedir(DIR);
>
> foreach (@arch_files) {
> $mtime = (localtime((stat($_))[9]))[3];
That would be the day of the month in which the file was last modified,
right? (This is a good place for a comment.) It would be good to do the
stat in a separate call, so that you can see whether it failed. For
example, if the directory is not the current directory. (Hint, hint!)
> if (($mtime-7) >= 0) {
Hmmm... Are you trying to determine whether the file is a week old? Maybe
you want the -M filetest, instead. This code seems to be trying to
identify any files modified after the seventh of the month. Is that what
you meant?
> system "cmd /c del $directory\\$_\n";
> }
> }
If you're trying to delete a file, use unlink rather than system.
> It seems that stat() returns 1 on all files, no matter what the real
> motification date is.
If so, that's a bug in your system or your copy of Perl. But before you
report it, check it again. I think that's not the problem.
Hope this helps!
--
Tom Phoenix Perl Training and Hacking Esperanto
Randal Schwartz Case: http://www.rahul.net/jeffrey/ovs/
------------------------------
Date: Tue, 05 May 1998 16:57:22 GMT
From: Gellyfish@btinternet.com (Jonathan Stowe)
Subject: Re: Scheduling a perl script under NT, makes stat() return 1 no matter what!?
Message-Id: <354f428d.15187915@news.btinternet.com>
On Tue, 05 May 1998 12:57:39 GMT, Michael Kristensen wrote :
<snip>
>If I run this script manually it does delete the files as it is
>surpose to do. But if I set the Windows NT Scheduler to run the
>script, it doesn't.
>It seems that stat() returns 1 on all files, no matter what the real
>motification date is.
>
>I've tried to run the Schedule service, both as "localsystem" and as a
>specific account, but with no luck.
>
There is some strangeness associated with the schedule service which
seems to make certain API calls work differently (euphemism) whatever
user it runs as. I have been examining this myself although I hadnt
noticed this with stat.
You might try using the -M operator rather than stat() to see if that
works.
>Any help would be appreciated. Please also reply by e-mail, since I
>don't read this conference often.
>
Hmmph.
/J\
Jonathan Stowe
Some of your questions answered:
<URL:http://www.btinternet.com/~gellyfish/resources/wwwfaq.htm>
------------------------------
Date: Tue, 05 May 1998 22:22:58 -0700
From: Jan Krynicky <jkry3025@comenius.ms.mff.cuni.cz>
To: Henry Lifton <henlif@elsfl.com>
Subject: Re: Split problem Form
Message-Id: <354FF3B2.4935@comenius.ms.mff.cuni.cz>
Henry Lifton wrote:
>
> I have a form that has a select multiple statement in it that is used in a
> search engine to select a city or cities. The variable is $scity. I split it
> on the ' '
>
> This is fine EXCEPT that a number of the cities are made up of 2 words or
> more.
>
> Some of the cities are:
>
> Deerfield Beach
> Pompano Beach
> Highland Beach
>
> Ft. Lauderdale
> N. Lauderdale
> Lauderdale Lakes
>
> So the code is this
>
> { $ciok=1; $_=$scity;
> if(length($scity) && (!/any/)) {
> $ciok=0; $_=$bcity;
> foreach $wantcity (split(' ',$scity)) {
> if(/$wantcity/){ $ciok=1;}
> }
> }
>
> When you search for only one of the cities that have 2 words, you get all of
> the cities that share the word.
>
> It seems to be spliting them on each space instead of the space between the
> each form entry
>
> Has anyone a solution to this?
>
> Thanks
>
> Henry Lifton
> henlif@elsfl.com
1. How did you get the $scity? In the query it's encoded as
scity=First+name&scity=Other&somethin=value&scity=Last+city
so a decent query parser should be able to either provide
you with an array of values (so you do not have to split at all)
or at least let you specify what string to join the values with
so that you may then split it.
If neither of these works change the values of the <OPTION>s to
<OPTION value=":Name of city:"> and then
split /: :/, $scity;
! Do not forget to strip the : from begin and end of $scity !
chop $scity;$scity =~ s/^://;
HTH, Jenda
http://www.fmi.cz/private/Jenda - Deurl.pm+Enurl.pm
------------------------------
Date: Tue, 05 May 1998 21:20:15 GMT
From: Tom Phoenix <rootbeer@teleport.com>
To: Henry Lifton <henlif@elsfl.com>
Subject: Re: Split problem Form
Message-Id: <Pine.GSO.3.96.980505141205.13101U-100000@user2.teleport.com>
On Tue, 5 May 1998, Henry Lifton wrote:
> Some of the cities are:
>
> Deerfield Beach
> Pompano Beach
> Highland Beach
> When you search for only one of the cities that have 2 words, you get
> all of the cities that share the word.
When you have more than one word in a row, there's no universal way to be
certain which goes with which:
Key West Springfield
Is that Key West and Springfield, or Key and West Springfield? Nobody
knows.
You may need to keep a small database of common two-word city names, or to
have them grouped in some way. (Perhaps they could be quoted before
sending them, for example. The FAQ has information on parsing such data.)
But there's nothing Perl-specific about this problem, of course; the same
difficulty comes up when you're programming in any language - or even in
English. As Douglas Hofstadter says, no language can render every thought
unambiguously, especially this one. Hope this helps!
--
Tom Phoenix Perl Training and Hacking Esperanto
Randal Schwartz Case: http://www.rahul.net/jeffrey/ovs/
------------------------------
Date: Tue, 05 May 1998 15:11:44 -0500
From: Andy Glew <glew@cs.wisc.edu>
To: David Boyce <David.Boyce@fmr.com>
Subject: Re: Using executable pathname to define include path
Message-Id: <354F7280.9C359997@cs.wisc.edu>
> > Please respond by email; I don't read this group.
>
> Tom Phoenix has addressed this well.
Yeah, right.
David: your post was of absolutely no use to me,
since I do not read this newsgroup. I went and learned
about FindBin the hard way.
OF COURSE I expected you to post your reply to the newsgroup.
As a longstanding USEnet user and prolific newsposter myself,
that goes without saying.
But refusing to reply by email is just grandstanding.
------------------------------
Date: Tue, 05 May 1998 15:20:54 -0500
From: Andy Glew <glew@cs.wisc.edu>
Subject: Re: Using executable pathname to define include path
Message-Id: <354F74A6.B4077297@cs.wisc.edu>
> Can the following can be done in Perl:
> discovery of the pathname of the executable, for use in defining module paths?
Since no useful replies came to me from this newsgroup, I learned the following
by random reading:
The FindBin module does pretty much what I needed, discovering, insofar
as is possible, the absolute path of the directory from which the script was
executed.
At first I was somewhat scared off by the wording of its documentation:
This allows the user to setup a directory tree with some software
with directories <root>/bin and <root>/lib
which seems to have one particular usage model in mind - albeit a quite
typical one.
It turns out, however, that where they say
$Bin = path to bin directory from where script was invoked
you can simply s/bin//, i.e.
$Bin = path to bin directory from where script was invoked
Therefore, a typical usage model would be, as they document it:
use FindBin;
BEGIN { unshift(@INC, "$FindBin::Bin/../lib" }
more flexible usages might be
BEGIN {
my $tmp = $FindBin::Bin;
$tmp =~ s|/rootdir/.*|/rootdir/lib|;
unshift(@INC, $tmp);
}
although this last example breaks if you get pathnames
like
.../rootdir/..../rootdir/....
such as CVS is wont to encourage.
------------------------------
Date: 5 May 1998 21:13:08 GMT
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: Re: Using executable pathname to define include path
Message-Id: <6invd4$ik8$2@csnews.cs.colorado.edu>
[courtesy cc of this posting sent to cited author via email]
In comp.lang.perl.misc,
Andy Glew <glew@cs.wisc.edu> writes:
:> Can the following can be done in Perl:
:> discovery of the pathname of the executable, for use in defining module paths?
This *is* in the FAQ, you know. Here's a Perl Cookbook
entry on it. See the bottom of this posting.
--tom
Keeping Your Own Module Directory
Problem
You have your own personal modules, but you don't want to install
it in the standard per-system extension library.
Solution
You have a several choices: use the -I command line switch;
set your $PERL5LIB environment variable; or employ the `use lib'
pragma, possibly in conjunction with the FindBin module.
Discussion
The @INC array contains a list of directories that are consulted
every time a `do', `require', or `use' sources in another file,
library, or module. You can print these out easily enough from
the command line:
$ perl -e 'for (@INC) { printf "%d %s\n", $i++, $_ }'
0 /usr/local/perl/lib/i686-linux/5.004
1 /usr/local/perl/lib
2 /usr/local/perl/lib/site_perl/i686-linux
3 /usr/local/perl/lib/site_perl
4 .
The first two directories, elements 0 and 1 of @INC, are the
standard architecture-dependent and architecture-independent
directories which all standard libraries, modules, and pragmas
will go into. The reason you have two of them is because some
modules contain information or formatting that only makes
sense on that particular architecture. For example, the Config
module contains information that cannot be shared across several
architectures, so it goes in the 0th array element above. Modules
that include compiled C components, such as *Socket.so*, are also
placed there. Most modules, however, go in the next directory,
the platform- independent one in the 1st element.
The next pair, elements 2 and 3 above, fulfills roles analogous
to elements 0 and 1, but on a site-specific basis. Suppose you
have a module that didn't come with Perl, like a CPAN module
or one you wrote yourself. When you--or more likely your system
administrator--installs this module, its components go into one
of the second pair of directories. You are encouraged to use
these for any modules that your entire site should be able to
access conveniently.
The last standard component, "." (your current working directory),
is really only useful when developing and testing your software,
not when deploying it. If your modules are sitting there in the
same directory as you last `chdir'ed to, then you're fine. But
if you're anywhere else, it doesn't work.
So sometimes none of the @INC directories works out. Maybe you
have your own personal modules. Perhaps your project group has
particular modules that are only relevant to that project. In
any of these cases, you need a way to augment the standard
@INC search.
The first approach involves using a command line flag,
-I*dirlist*. The *dirlist* is a colon- separated list of one or
more directories to @INC, which will be prepended to the front
of the @INC array. This works well for simple command lines,
and thus can be used on a per- command basis, such as when you
call a quick one-liner from a shell script.
But this technique not suggested for inclusion in the
`#!' (pound-bang) line. First of all, it's not much fun
to modify each program. But more importantly, some older
operating systems have bugs related to how long that line
can be.[FOOTNOTE: Typically 32 characters, including the
`#!' part. That means if you have a very long path, such as
`#!/opt/languages/free/extrabits/perl', you may get the mysterious
`"Command not found"' error.] Perl does its best to rescan the
line manually, but it's still a bit too dicey to rely on.
A better solution in many cases is to set the $PERL5LIB
environment variable. This can be done in your shell start-up
file. Or your system administrator may want to so in a system-
wide start-up file so all users can benefit. For example,
suppose you have all your own modules in a directory called
*~/perllib*. You would place one of the following lines in your
shell start-up file, depending on which shell you use.
# syntax for sh, bash, ksh, or zsh
export PERL5LIB=$HOME/perllib
# syntax for csh or tcsh
setenv PERL5LIB ~/perllib
Probably the most convenient solution from your users' perspective
is for you to add a `use lib' pragma near the top of your
script. That way the users of the program don't need to take
any special action to run your program. Imagine a hypothetical
project called Spectre that has its own set of libraries,
libraries which its programs rely upon. Those programs might
well have a statement like this at their start.
use lib "/projects/spectre/lib":
But what happens when you don't know the exact path to the
library? This could be the case if you allow the whole project
be installed in an arbitrary path. You could create an elaborate
installation procedure to dynamically update the script, but even
if you did, paths would still be frozen at installation time. If
someone moved the files later, the libraries wouldn't be found.
A particularly elegant solution to this problem is the FindBin
module. This module computes the full path to the executing
script's enclosing directory, setting an importable package
variable called $Bin to that directory. Typical usage is either
to look for modules in the same directory as the program, or in
a *lib* directory at the same level.
To demonstrate the first case, suppose have a program
called */wherever/spectre/myprog* that needs to look in
*/wherever/spectre* for its modules, but you don't want to
hardcode that path.
use FindBin;
use lib $FindBin::Bin;
The second case would be if your program is in
*/wherever/spectre/bin/myprog* and needs to look at
*/wherever/spectre/lib* for its modules.
use FindBin qw($Bin);
use lib "$Bin/../lib";
--
Tom Christiansen tchrist@jhereg.perl.com
Show respect for age. Drink good Scotch for a change.
------------------------------
Date: 8 Mar 97 21:33:47 GMT (Last modified)
From: Perl-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 8 Mar 97)
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.misc (and this Digest), send your
article to perl-users@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.
The Meta-FAQ, an article containing information about the FAQ, is
available by requesting "send perl-users meta-faq". The real FAQ, as it
appeared last in the newsgroup, can be retrieved with the request "send
perl-users FAQ". Due to their sizes, neither the Meta-FAQ nor the FAQ
are included in the digest.
The "mini-FAQ", which is an updated version of the Meta-FAQ, is
available by requesting "send perl-users mini-faq". It appears twice
weekly in the group, but is not distributed in the digest.
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 V8 Issue 2508
**************************************