[18122] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 282 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Feb 13 14:13:11 2001

Date: Tue, 13 Feb 2001 11:10:22 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <982091422-v10-i282@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Tue, 13 Feb 2001     Volume: 10 Number: 282

Today's topics:
        Locking files in a CGI script <lurvas@excite.com>
    Re: measuring sizeof of STDIN <mischief@velma.motion.net>
        opening files <mcomerf@nortelnetworks.com>
        Potential race condition? Other optimization pointers? <nospam@nospam.com>
        Problem with browser timeout <odd@multinet.no>
    Re: Problem with browser timeout <godzilla@stomp.stomp.tokyo>
    Re: Problem with browser timeout <odd@multinet.no>
    Re: Problem with browser timeout <joe+usenet@sunstarsys.com>
    Re: Problem with browser timeout <godzilla@stomp.stomp.tokyo>
        rand function does not work (Falc2199)
    Re: rand function does not work <jdf@pobox.com>
    Re: rand function does not work (Abigail)
    Re: rand function does not work <godzilla@stomp.stomp.tokyo>
    Re: Replacing single-letter words doesn't work (Holger Marzen)
        Sendmail::Milter delrcpt problems <dr.bob@dowcarter.com>
    Re: shadow file, replace *LK* (LK)
    Re: STDIN - mutliple actions <jdf@pobox.com>
        Strings and pointers in Perl? <fw@lanvision.nl>
    Re: Strings and pointers in Perl? <foo@bar.va>
    Re: Strings and pointers in Perl? <tshoenfe@cisco.com>
    Re: TCP server <pp@ludusdesign.com>
    Re: the best solution? <mischief@velma.motion.net>
        Using perl to write shell scripts <lanaS55nospam@hotmail.com>
    Re: Using perl to write shell scripts <foo@bar.va>
    Re: Using perl to write shell scripts <bart.lateur@skynet.be>
    Re: Using perl to write shell scripts <mjcarman@home.com>
    Re: Using perl to write shell scripts <lanaS55nospam@hotmail.com>
    Re: Using perl to write shell scripts <lanaS55nospam@hotmail.com>
    Re: Using perl to write shell scripts <lanaS55nospam@hotmail.com>
        What  v65  refer to ? <dpiche@speedware.com>
    Re: What  v65  refer to ? <jdf@pobox.com>
    Re: What  v65  refer to ? <dpiche@speedware.com>
        Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)

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

Date: Tue, 13 Feb 2001 19:07:14 +0100
From: Hariga Ben <lurvas@excite.com>
Subject: Locking files in a CGI script
Message-Id: <01HW.B6AF3662000394C904CE5CE0@news.communique.se>

I'm pretty sure this is covered by some FAQ or something similar but I can't 
find it. So, if someone knows where I can find some hints on how to do this I 
would be grateful for a pointer.

I'm writing a few simple CGI scripts which will allow two people to change 
some information. Since the updates will be few (probably in the order of 
once or twice a week) there will probably be no problem with concurrent 
changes etc  ... but I would like to learn a few new thing I thought I would 
take the opportunity to play a bit 8-)

Adding information is no problem, then I can 'flock'. But I don't really know 
how to proceed when it comes to changing some info. I mean I can't use 
'flock' since I can't be sure that there will be a second call that submits 
the changes and releases the lock ... (I'm probably looking for a lock that 
times out after 15 minutes or so).

I'll have limited access to the server so I'm unsure if I can have some 
regular "clean up" script running. The info I'm changing are a few text 
files.
			               h.B.
-- 
Håriga Ben
lurvas@excite.com



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

Date: Tue, 13 Feb 2001 19:01:39 -0000
From: Chris Stith <mischief@velma.motion.net>
Subject: Re: measuring sizeof of STDIN
Message-Id: <t8j14jj01r8maa@corp.supernews.com>

Logan Shaw <logan@cs.utexas.edu> wrote:
> In article <Zn4i6.17625$lI2.19423@news1.rochd1.qld.optushome.com.au>,
> Sean <sean@tnq.com> wrote:
>>When I do something like:
>>cat test.msg | ./perltest.pl

>>What is available to me to also find out the size (in bytes) that is being
>>piped to the perl script?

[snip]

> The best that you can do is simply read all the input, count how much
> you've read, and hope that the program sending data through the pipe
> terminates.

Or close when you've seen what interests you, then don't worry about it.
This only works if you have some limit on what you need. You can usually
trust a web server to give you all of a CGI POST at once. The shell and
the standard libs will usually give you an end-of-file upon control-D
(on Windows control-Z) with input from a user at a prompt (although this
character is capable of being redefined on most Unix-like systems, the
users should know their environment). If you're getting a pipe from
an uncontrolled program and it's unformatted data, you have to figure
out what your program should read. If you have truly arbitrary data from
an arbitrary data source, you just have to hope.

Chris

-- 
Christopher E. Stith

You can never entirely stop being what you once were. That's
why it's important to be the right person today, and not put
it off till tomorrow. -- Larry Wall, 3rd State of the Onion



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

Date: Tue, 13 Feb 2001 12:20:58 -0500
From: "M Comerford" <mcomerf@nortelnetworks.com>
Subject: opening files
Message-Id: <96bqi8$5kf$1@bcrkh13.ca.nortel.com>

Hi, I am new at programming with perl and I was wondering if perl scripts
can open up files directly over the internet.  For example, my script is
running on one server and I want it to be able to open a .txt file that runs
on a completely different external server.  So, in the "open" command I am
going to need a web address.  Is this possible?

Any advice would be greatly appreciated.
Mike




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

Date: 13 Feb 2001 17:59:01 GMT
From: The WebDragon <nospam@nospam.com>
Subject: Potential race condition? Other optimization pointers?
Message-Id: <96bsl5$g31$0@216.155.33.24>

I think I've identified a potential race condition in a perl script I'm 
working on. It seems to me that it's possible to avoid this using flock, 
but I'm not exactly sure of where and how I should implement it 
correctly, and would appreciate a pointer or two. 

the code is here, all nicely commented for the rest, and I've identified 
the point at which I think there's a race condition with a comment to 
that effect. 

    <http://216.155.0.50/~sgodin/misc/codesample.htm>

I'm also interested to hear about any further optimizations I might make 
to this script that would enable me to streamline the speed with which 
it outputs.. I've been made aware that the DBD::CSV format is somewhat 
slower than a DBD::mysql format would be, due mostly to the fact that 
there's a SQL server involved (and eventually the script will be 
migrated to use mysql as soon as the admins at the service that we'll be 
using this on can be coerced to update the many many out-of-date modules 
that they have there *sigh*) 

I use a 21" display to code on, and can only apologize for the longer 
lines of text evident in the code.. I have it, and take advantage of it, 
in my programming style, and apologize in advance if this makes it 
harder to read on a smaller display.

-- 
unmunge e-mail here:
#!perl -w
print map {chr(ord($_)-3)} split //, "zhepdvwhuCzhegudjrq1qhw"; 
# ( damn spammers. *shakes fist* take a hint. =:P )


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

Date: Tue, 13 Feb 2001 16:02:07 +0100
From: Odd Benestad <odd@multinet.no>
Subject: Problem with browser timeout
Message-Id: <3A894C6F.9C93D54F@multinet.no>

Hi !

I have programmed a quite neat web publishing system, which seemed to
work very well. But, when text in form fields is getting big, and there
is a lot of data to process, the browser gets a timeout.

Can anyone provide me with som hints ? I'm really stuck here.

Best regards
Odd Benestad d.y


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

Date: Tue, 13 Feb 2001 07:09:39 -0800
From: "Godzilla!" <godzilla@stomp.stomp.tokyo>
Subject: Re: Problem with browser timeout
Message-Id: <3A894E33.E022EE20@stomp.stomp.tokyo>

Odd Benestad wrote:
 
> I have programmed a quite neat web publishing system, which seemed to
> work very well. But, when text in form fields is getting big, and there
> is a lot of data to process, the browser gets a timeout.
 
> Can anyone provide me with som hints ? I'm really stuck here.


You have a classic "Infinite Loop Bug" in line 13 of your script.

Godzilla!


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

Date: Tue, 13 Feb 2001 16:26:51 +0100
From: Odd Benestad <odd@multinet.no>
Subject: Re: Problem with browser timeout
Message-Id: <3A89523B.5F4F327B@multinet.no>

Well, is there anything else to say than thank you Godzilla...*ss*

Best regards
Odd Benestad d.y

"Godzilla!" wrote:
> 
> Odd Benestad wrote:
> 
> > I have programmed a quite neat web publishing system, which seemed to
> > work very well. But, when text in form fields is getting big, and there
> > is a lot of data to process, the browser gets a timeout.
> 
> > Can anyone provide me with som hints ? I'm really stuck here.
> 
> You have a classic "Infinite Loop Bug" in line 13 of your script.
> 
> Godzilla!


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

Date: 13 Feb 2001 11:03:31 -0500
From: Joe Schaefer <joe+usenet@sunstarsys.com>
Subject: Re: Problem with browser timeout
Message-Id: <m33ddik1t8.fsf@mumonkan.sunstarsys.com>

Odd Benestad <odd@multinet.no> writes:

> I have programmed a quite neat web publishing system, which seemed to
> work very well. But, when text in form fields is getting big, and there
> is a lot of data to process, the browser gets a timeout.
> 
> Can anyone provide me with som hints ? I'm really stuck here.

As your description indicates, the problem with your script is not 
a Perl issue. It's a cgi question for another ng; but perhaps you 
should flush  its  output to the browser incrementally instead of 
waiting until it's completely finished processing.

-- 
Joe Schaefer                 "Language is the dress of thought."
                                               -- Samuel Johnson


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

Date: Tue, 13 Feb 2001 08:25:11 -0800
From: "Godzilla!" <godzilla@stomp.stomp.tokyo>
Subject: Re: Problem with browser timeout
Message-Id: <3A895FE7.2A701907@stomp.stomp.tokyo>

Odd Benestad wrote:

> > Odd Benestad wrote:
> Godzilla! wrote:

> > > I have programmed a quite neat web publishing system, which seemed to
> > > work very well. But, when text in form fields is getting big, and there
> > > is a lot of data to process, the browser gets a timeout.

> > > Can anyone provide me with som hints ? I'm really stuck here.

> > You have a classic "Infinite Loop Bug" in line 13 of your script.

> Well, is there anything else to say than thank you Godzilla...*ss*


With your asking, there is something else you could say,
silently within your mind,

"Why did I not post some sample code?"

However, this is not a real problem. Like all regulars
here, I am a practicing Internet Mind Reader. I made
a psychic connection through your Internet Service
Provider, read your mind, read your code and, found
this Infinite Loop Bug in line 13 of your code.

I am pleased to have been of assistance.

Godzilla!


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

Date: 13 Feb 2001 17:22:39 GMT
From: falc2199@aol.comNOJUNK (Falc2199)
Subject: rand function does not work
Message-Id: <20010213122239.17652.00000644@ng-cm1.aol.com>

The rand function in the following script does not seem to work. It is suppose
to choose the index of one of the three files in the array and load that file.
But it always chooses the first file (meaning the rand function is always
returning 1, I think). Can ne1 make sense of this...

#!/usr/bin/perl

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

print "<HTML>\n<BODY>\n";

srand;

@articles = ("fact1.html", "fact2.html", "fact3.html");

$random = rand(@aritcles);

$article = $articles[$random];

open(FILE, $article);

while(<FILE>)
{

print $_;

}
close(FILE);
print "</BODY>\n";
print "</HTML>\n";

Thanks in advance for any help,
Jehan




To e-mail, remove NOJUNK


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

Date: 13 Feb 2001 12:31:17 -0500
From: Jonathan Feinberg <jdf@pobox.com>
Subject: Re: rand function does not work
Message-Id: <itmebica.fsf@pobox.com>

falc2199@aol.comNOJUNK (Falc2199) writes:

> The rand function in the following script does not seem to work.

I'm afraid the problem lies elsewhere.  If you cross your eyes
verrrry hard, you might see it. :)

> #!/usr/bin/perl

You forgot the -w switch, which would have told you what I'm about to
tell you.  You'd also benefit from 

  use strict;

> @articles = ("fact1.html", "fact2.html", "fact3.html");

That's fine, though

  @articles = qw( fact1.html fact2.html fact3.html );

is arguably easier to read.

> $random = rand(@aritcles);
                 ^^^^^^^^^
What the heck is @aritcles?

-- 
Jonathan Feinberg   jdf@pobox.com   Sunny Brooklyn, NY
http://pobox.com/~jdf


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

Date: 13 Feb 2001 18:13:08 GMT
From: abigail@foad.org (Abigail)
Subject: Re: rand function does not work
Message-Id: <slrn98iu9k.6o7.abigail@tsathoggua.rlyeh.net>

Falc2199 (falc2199@aol.comNOJUNK) wrote on MMDCCXXIII September MCMXCIII
in <URL:news:20010213122239.17652.00000644@ng-cm1.aol.com>:
## The rand function in the following script does not seem to work. It is suppose
## to choose the index of one of the three files in the array and load that file.
## But it always chooses the first file (meaning the rand function is always
## returning 1, I think). Can ne1 make sense of this...

Use 'use strict;' and -w and Perl will tell you were it went wrong.



Abigail
-- 
srand 123456;$-=rand$_--=>@[[$-,$_]=@[[$_,$-]for(reverse+1..(@[=split
//=>"IGrACVGQ\x02GJCWVhP\x02PL\x02jNMP"));print+(map{$_^q^"^}@[),"\n"


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

Date: Tue, 13 Feb 2001 10:56:34 -0800
From: "Godzilla!" <godzilla@stomp.stomp.tokyo>
Subject: Re: rand function does not work
Message-Id: <3A898362.723D1D1B@stomp.stomp.tokyo>

Falc2199 wrote:

(snippage)

> The rand function in the following script does not seem to work.
 
> $random = rand(@aritcles);

You have a misspelled word, "aritcles" in your code.


This code of yours can be written for better
efficiency and speed.


@articles = ("fact1.html", "fact2.html", "fact3.html");

open(FILE, $articles[rand(@articles)]);

while(<FILE>)
{ print $_; }

close(FILE);


Your use of srand is ok but is not needed for modern
versions of Perl. However, for greater portability,
should your code be used under a very old version
of Perl, leave it in. This causes no significant
harm worthy of worry. Remove it if not needed.

My " $articles[rand(@articles)] " performs the same 
function as your srand and random selection coding.

Godzilla!


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

Date: 13 Feb 2001 14:05:28 GMT
From: holger.marzen@sik-gmbh.de (Holger Marzen)
Subject: Re: Replacing single-letter words doesn't work
Message-Id: <slrn98ifp8.f34.holger.marzen@sally.sik-gmbh.de>

On Mon, 12 Feb 2001 20:42:10 GMT, Uri Guttman wrote:

>>>>>> "DN" == David Ness <DNess@Home.Com> writes:
>
>  DN> Holger Marzen wrote:
>  >> 
>  >> I want to delete single-letter words (or replace them by a space) in a
>  >> string and tried:
>  >> 
>  >> $suchtext =~ s/([\^ ])[^ ][ \^]/ /g;
>  >> 
>  >> However, it does not work. "foo a b c d e" is changed to "foo b d".
>  >> Any ideas?
>
>  DN> I think I'd try something based on: 
>  DN>      s/(\b)\S(\b)/$1$2/g;
>  DN> where the $1 and/or $2 only appear if you want to preserve the `break'
>  DN> whitespace...
>
>\b is an ZERO-WIDTH assertion which cannot be grabbed. so putting parens
>around them and using $1 and $2 there is meaningless.
>
>if the OP would show some example input and output it would be easier to
>figure out what he wants. this works in a general way. it can be fine
>tuned by replacing \w with a char class.
>
>perl -pe 's/\b\w\b/ /g'

Finally I found out that I cannot use \b if there are umlaut characters
contained in the strings. Perl accidentally sees the "b" in "börse" as a
single character delimited by a non-word-character "ö". Setting the
locale does not help. So I found out how to build zero-width expressions
that are neccessary that all single-letter-words (and not every 2nd) are
deleted in a string:

$suchtext =~ s/(^| )[A-Za-z0-9ÄÖÜäöüß]{1}(?=[ ]|$)//g;

First it looks for the beginning-of-line or a SPACE.
Then for exactly one of the characters A-Za-z0-9ÄÖÜäöüß.
Then for end-of-line or a SPACE, but that SPACE will be seen after the
deletion.


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

Date: 13 Feb 2001 16:50:29 +0000
From: Rob Blake <dr.bob@dowcarter.com>
Subject: Sendmail::Milter delrcpt problems
Message-Id: <96boks$184t$1@dunlop.dowcarter.com>


Has anyone else managed to get delrcpt to work properly in the 
Sendmail::Milter module?

I've been playing all day with it, and I've got to the stage where I can
rewrite headers, add new headers, and add new recipients successfully.
The problem comes when I try and delete current recipients.

I've tried passing the recipient name from the envrcpt callback, and
I've tried entering a particular address hardcoded in the script.
Each time the ctx->delrcpt command reports success, but the recipient
still gets a copy of the message.

Concerned, I tried adding a bogus email address to be deleted from the
recipient list. The delrcpt command happily reported success for this
one too, despite the fact it didn't appear anywhere in the message.

Here's the appropriate bit of code:

    for $rcpt ("dr.bob\@dowcarter.com","arse\@feck.com") {
      if ($ctx->delrcpt($rcpt) == undef) {
        print "Argh, recipient $rcpt not deleted\n";
      } else {
        print "Removed recipient $rcpt\n";
      }
    }

Both email addresses reported success despite the obviously bogus one
not existing anywhere in the message.
I've also tried testing "if (not $ctx->delrcpt......" with the same 
result.

Anyone ready to show me what I'm doing wrong?

Rob.



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

Date: Tue, 13 Feb 2001 18:45:10 GMT
From: lkenny@fisheries.org (LK)
Subject: Re: shadow file, replace *LK*
Message-Id: <3a8980a3.17672595@wingate>

On Tue, 13 Feb 2001 10:44:18 +0100, "Per- Fredrik Pollnow"
<Per-fredrik.Pollnow@epk.ericsson.se> wrote:

>Hi,
>
>I need some help with a script...
>
>The script is going to replace *LK* in a Solaris shadow file, and I cant
>figure out how to do.
>
>The shadow file look like this:
>
>/etc/shadow
>
>bin:NP:6445::::::
>sys:NP:6445::::::
>adm:NP:6445::::::
>lp:NP:6445::::::
>smtp:NP:6445::::::
>uucp:NP:6445::::::
>nuucp:NP:6445::::::
>listen:*LK*:::::::
>nobody:NP:6445::::::
>noaccess:NP:6445::::::
>nobody4:NP:6445::::::
>
>I want the: listen:*LK*:::::::
>to bee: listen:tada:::::::
>

I'm getting paranoid.

LK


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

Date: 13 Feb 2001 11:52:11 -0500
From: Jonathan Feinberg <jdf@pobox.com>
Subject: Re: STDIN - mutliple actions
Message-Id: <y9vabk5g.fsf@pobox.com>

"Sean" <sean@tnq.com> writes:

> Trying to do 2 different actions to stdin, can I not seek to the start of
> the STDIN after I use it?

> while (<STDIN>) {
>     $bytes +=length;
# put whatever else you want to do here
> }

-- 
Jonathan Feinberg   jdf@pobox.com   Sunny Brooklyn, NY
http://pobox.com/~jdf


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

Date: Tue, 13 Feb 2001 16:31:24 +0100
From: Frank van Wensveen <fw@lanvision.nl>
Subject: Strings and pointers in Perl?
Message-Id: <raki8t841gb7832eb54m496uld45tc6oo9@4ax.com>

I'm originally a C programmer, and now I'm trying port a piece of C
code to Perl. Nasty business. :-) Typical C practices won't work. 

I have a string s which I'm scanning with a pointer p from begin to
(\0-terminated) end, in C:

char *p, *s;

p = s;
while (*p) {
  /* do something to *p */
 p++;
}

How can I do something similar in Perl? I have a string $s that I need
to process character by character, but something like $s[p] doesn't
work, since that requires s to be an array instead of a scalar. In
other words, something like

	@s = "Hi there";
	print $s[1];

obviously won't work.

I know I'm overlooking something very basic here, but I'm wearing my C
hat right now and that's probably the reason why I don't see it. :-)

Would someone please enlighten me?



FVW


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

Date: Tue, 13 Feb 2001 17:18:06 +0100
From: Marco Natoni <foo@bar.va>
Subject: Re: Strings and pointers in Perl?
Message-Id: <3A895E3E.ED6EFE9B@bar.va>

Frank,

Frank van Wensveen wrote:
> I'm originally a C programmer, and now I'm trying port a piece of 
> C code to Perl. Nasty business. :-) Typical C practices won't work. 
> I have a string s which I'm scanning with a pointer p from begin 
> to (\0-terminated) end, in C:
> char *p, *s;
> p = s;
> while (*p) {
>   /* do something to *p */
>  p++;
> }
> How can I do something similar in Perl? I have a string $s that I 
> need to process character by character, but something like 
> $s[p] doesn't work, since that requires s to be an array instead of 
> a scalar. In other words, something like
>         @s = "Hi there";
>         print $s[1];
> obviously won't work.
> I know I'm overlooking something very basic here, but I'm wearing 
> my C hat right now and that's probably the reason why I don't see 
> it. :-) 
> Would someone please enlighten me?

  Generally, Perl and C have two different approaches to the string
scanning:  While in C a string is a (nul-terminated) array of
characters, in Perl it is a primitive type.  That is an advantage of
Perl, born, in fact, exactly to handle strings. ;)

  If you want to follow the C approach, you can split your string in a
list of character, for example with the idiom:

	@c_string=split //,$perl_string

do your task and then rebuild the string with:

	$new_perl_string=join '',@c_string

but I keenly suggest you to explore the marvellous world of regular
expressions, that could reserve you some interesting surprises, and thus
to join the Perl-way-of-life in the string task.

	$ perldoc perlre


	Best regards,
		Marco


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

Date: Tue, 13 Feb 2001 09:05:17 -0800
From: Todd Shoenfelt <tshoenfe@cisco.com>
Subject: Re: Strings and pointers in Perl?
Message-Id: <3A89694C.DC9A8A76@cisco.com>

Hi Frank,

You probably don't need to process individual characters in Perl.  Perl's
native regular expressions usually allow you to avoid that.  See below for
code.

> I have a string $s that I need to process character by character

while ( $s =~ /(.)/g ) {
    print "$1\n";
}

# or

for ( split( //, $s ) ) {
    print "$_\n";
}

--
Todd Shoenfelt
Cisco Systems, Inc.
EMAN (Enterprise Management)
Web Developer
Bldg. 12, First Floor, Cube A5-13
408-527-8958




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

Date: Tue, 13 Feb 2001 13:56:05 -0500
From: Pierre Phaneuf <pp@ludusdesign.com>
Subject: Re: TCP server
Message-Id: <3A898345.89BC71D2@ludusdesign.com>

Studio 51 wrote:

> I understand it's bad to put a whole lot of code into a handler for a
> signal. Apparently (among other things) if you use a function in that
> handler that was already in use when the signal arrived, Perl might dump
> core. Well, I have a big fat mess of stuff I want to do when I get a SIGHUP
> (Kill a bunch of child processes, write to a log, write to STDOUT, close
> filehandles, exec(), etc...). The recommended way to handle incoming signals
> seems to be to just set a simple flag that the rest of the program can deal
> with. The problem is that I can't check that flag while I'm blocking with
> accept(). My little server may not get a new connection for some time, and
> if it gets the SIGHUP while it's waiting for accept() to return then it
> won't get a chance to check for the flag till it gets a new connection.

I suggest creating a pipe (with the "pipe" function) and doing your
accept() with the non-blocking flag. Then, put both file descriptors
(the socket and the reading end of the pipe) in a select (IO::Select)
and have your signal handler write a single byte in the writer end of
the pipe.

This enables you to juggle many file descriptors at once without using
too much CPU, but if you only have a single file descriptor that you are
doing accept on, then you could just look for EINTR.

-- 
"A Perl script is correct if it gets the job done before your boss
fires you." -- Larry Wall


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

Date: Tue, 13 Feb 2001 17:31:57 -0000
From: Chris Stith <mischief@velma.motion.net>
Subject: Re: the best solution?
Message-Id: <t8irsdmbq69a6d@corp.supernews.com>

Joe Schaefer <joe+usenet@sunstarsys.com> wrote:
> Joe Schaefer <joe+usenet@sunstarsys.com> writes:

>> You forgot to try varying the string; try running it with 
>> 
>>     my $string = 'substring of string' x 100;
>> 
>> In that case, on linux 5.005_03 I don't see any performance 
>> difference between them; and on 5.6 the advantage shifts 
>> significantly (~20%) to the s/string//g algorithm.
>   ^^^^^^^^^^^^^^^^^^^^

I did vary the string. I used six strings, up to 100 chars.
Not nearly as long as your above sample, I admit. I ran the
benchmark three times for each. The results I posted were the
middle values of the runs on the third of six strings, which
I took to be an optimistic approximation of average. I tested
with a null string, with just the exact string to be matched,
with the string I mentioned before, with a 100 char repetition
of the word "string" (with the last chopped to fit), and with
a 100 char string containing no matches. I admit this is still
ad-hoc, but it's not as ad-hoc as you assume. I have little
time to do comprehensive testing for other people. I showed
my results and the OP is free to test it himself. I showed
in my post how he could do that. So, what's your problem with
that?

> No- I think I got that wrong.  The tests I ran using 5.6 had a 

>         study $string;

> line in the setup, which apparently caused a slowdown in the
> OP_option sub.  After commenting it out, there doesn't seem 
> to be any appreciable difference on my box for 5.6, either.

>> Big deal.

I only offered benchmarks in the first place because the OP
wanted to know. I warned that Perl is probably not the right
langauge if memory usage and speed are a concern. Get the
hell off your high horse and join the rest of us in responding
to what the OP has asked. You say "big deal" to exactly what
the OP asked for. I don't consider that very helpful. If you're
going to critique my posts, at least take the time to read the
thread first.

> And another, even slower and more obscure solution:

>   sub LAiSC {
>         $count = () = $string =~ /string/g;  
>   }

I thought about offering that one myself. However, the OP stated
explicitly that he needed speed and memory efficiency. He also
mentionbed two possible solutions he wanted to compare, neither
of which is the above.

> Joe Schaefer      "I am not one of those who in expressing opinions confine
>                                     themselves to facts."
>                                                --Mark Twain

Excellent signature for your post.

Chris

-- 
Christopher E. Stith

You must not lose faith in humanity. Humanity is an ocean;
if a few drops of the ocean are dirty, the ocean does not
become dirty.  -- Mohandas K. Gandhi



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

Date: Tue, 13 Feb 2001 16:05:36 GMT
From: Alexandra <lanaS55nospam@hotmail.com>
Subject: Using perl to write shell scripts
Message-Id: <3A88F63F.E86A83B0@hotmail.com>

I would prefer to write some shell scripts in perl because I'm more
familiar with it that
with writing bourne shell scripts.
The problem is that I have to do a lot of file copying, deleting, and
permission changing.
I wrote a perl script but it has 6 straight lines of back ticks to call
a shell command.
I was wondering if the performance of the script suffers any by making
all these calls
and whether it would be beneficial to use the Shell module.  I really
don't understand
the usefulness of the Shell module.  What's the difference between
using:
`cp /usr/local/thisfile /usr/local/thatfile`;
as opposed to using the Shell module:
cp("/usr/local/thisfile","/usr/local/thatfile");


Scott



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

Date: Tue, 13 Feb 2001 17:25:22 +0100
From: Marco Natoni <foo@bar.va>
Subject: Re: Using perl to write shell scripts
Message-Id: <3A895FF2.D6D14C8A@bar.va>

Scott (?),

Alexandra wrote:
> I would prefer to write some shell scripts in perl because I'm 
> more familiar with it that with writing bourne shell scripts.
> The problem is that I have to do a lot of file copying, deleting, 
> and permission changing. I wrote a perl script but it has 6 
> straight lines of back ticks to call a shell command. I was 
> wondering if the performance of the script suffers any by 
> making all these calls and whether it would be beneficial to use 
> the Shell module.  I really don't understand the usefulness of 
> the Shell module.  What's the difference between using:
> `cp /usr/local/thisfile /usr/local/thatfile`;
> as opposed to using the Shell module:
> cp("/usr/local/thisfile","/usr/local/thatfile");

  The first idiom surely forks a new process and opens a pipe with it,
while the second (maybe) not.  Another, and better, solution is to use
the File::Copy module, that will make your script more stable and
portable.

  However, you can test your idioms using the Benchmark module.


	Best regards,
		Marco


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

Date: Tue, 13 Feb 2001 16:37:18 GMT
From: Bart Lateur <bart.lateur@skynet.be>
Subject: Re: Using perl to write shell scripts
Message-Id: <kkoi8t8fhqf5rs79ue2natli7uvpr85kio@4ax.com>

Alexandra wrote:

>The problem is that I have to do a lot of file copying, deleting, and
>permission changing.
>I wrote a perl script but it has 6 straight lines of back ticks to call
>a shell command.

Er... why? chmod() and unlink() are both perl built-ins, and

>`cp /usr/local/thisfile /usr/local/thatfile`;

with the module File::Copy, you can do that, too.

	use File::Copy;
	cp '/usr/local/thisfile', '/usr/local/thatfile';
or
	copy '/usr/local/thisfile', '/usr/local/thatfile';

-- 
	Bart.


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

Date: Tue, 13 Feb 2001 11:08:54 -0600
From: Michael Carman <mjcarman@home.com>
Subject: Re: Using perl to write shell scripts
Message-Id: <3A896A26.DF2E8B73@home.com>

Alexandra wrote:
> 
> I would prefer to write some shell scripts in perl because I'm more
> familiar with it that with writing bourne shell scripts. The problem
> is that I have to do a lot of file copying, deleting, and permission
> changing.

Perl does have unlink() and chmod() builtins. You can copy/move files
directly in Perl with the File::Copy module.

> I wrote a perl script but it has 6 straight lines of back ticks to call
> a shell command.

I like Perl too, but that doesn't mean you always have to use it.

If that's all your script does, you could go ahead and learn a little
about shell scripting. There's no point in just wrapping OS calls in
Perl. Or, you could implement everything in Perl using the functions I
mentioned above.

Sometimes the decision to write something in Perl vs. shell is
determined by where the script will be used. If it's only going to run
on your local *nix box shell is fine, or Perl with liberal use system
calls. If you want to use it on multiple platforms, then try to write it
in pure Perl.

> I was wondering if the performance of the script suffers any by making
> all these calls and whether it would be beneficial to use the Shell
> module.

Every time you make a system call, Perl has to fork a subprocess and
wait for it to return. There is some overhead with doing that, so
writing your script in shell might be marginally faster. But unless
you're forking a *lot* of processes, the difference shouldn't be
noticable.

Once again, you're delegating a lot more to the OS than you have to. Use
the builtins whenever you can.

> I really don't understand the usefulness of the Shell module.

It allows you to use shell commands as if they were Perl builtins.

> What's the difference between using:
> `cp /usr/local/thisfile /usr/local/thatfile`;
> as opposed to using the Shell module:
> cp("/usr/local/thisfile","/usr/local/thatfile");

AFAIK, nothing other than personal preference for which form you would
rather write.

-mjc


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

Date: Tue, 13 Feb 2001 18:53:51 GMT
From: Alexandra <lanaS55nospam@hotmail.com>
Subject: Re: Using perl to write shell scripts
Message-Id: <3A891DA0.35ACBA64@hotmail.com>



Bart Lateur wrote:

> Er... why? chmod() and unlink() are both perl built-ins, and
>
> >`cp /usr/local/thisfile /usr/local/thatfile`;
>

chmod() is a builtin.  Fascinating.  I did not know that.  ulink deletes
files!!!
Wow! I just looked in my perl book and chown's a builtin too!!! Happy days!
Rmdir and mkdir are builtins!!!  I wish, I'd have checked this out before
attempting
to write a non perl script.   I don't know why I never stopped to check to
see if perl had equivalent commands!?  I don't need to put shell commands in
my perl script.
I can write my shell scripts totally in perl!!!  If I had known this
yesterday I could have saved my self hours of aggravation.  I'm delighted to
realize that I can write all my scripts in perl :)))))))))


Thanks,
Scott



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

Date: Tue, 13 Feb 2001 18:37:49 GMT
From: Alexandra <lanaS55nospam@hotmail.com>
Subject: Re: Using perl to write shell scripts
Message-Id: <3A8919DF.8DA08A35@hotmail.com>



Marco Natoni wrote:

> The first idiom surely forks a new process and opens a pipe with it,
> while the second (maybe) not.  Another, and better, solution is to use
> the File::Copy module, that will make your script more stable and
> portable.
>
>   However, you can test your idioms using the Benchmark module.
>
>         Best regards,
>                 Marco

That's a good idea.  I also need to mess with the file permissions,delete
files, delete directories, and add directories.  Is there a perl module
that'll handle these? (besides Shell.pm of course)

Thanks,
Scott



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

Date: Tue, 13 Feb 2001 19:04:11 GMT
From: Alexandra <lanaS55nospam@hotmail.com>
Subject: Re: Using perl to write shell scripts
Message-Id: <3A89200C.A0A54A29@hotmail.com>



Michael Carman wrote:

> Alexandra wrote:
> Perl does have unlink() and chmod() builtins. You can copy/move files
> directly in Perl with the File::Copy module.

I just found this out from another poster. I can now write in perl w/o using
system calls.

> I like Perl too, but that doesn't mean you always have to use it.
>
> If that's all your script does, you could go ahead and learn a little
> about shell scripting. There's no point in just wrapping OS calls in
> Perl. Or, you could implement everything in Perl using the functions I
> mentioned above.

I'm choosing to do the latter after struggling to "convert" this simple line
in a shell script:  @action=split(':',$line); Plus my scripts need to open,
close databases and CDB files.  The only reason I resorted to shell scripting
was to use rm, rmdir, mkdir,
chown, chmod because I didn't realize perl had builtin's for these.

> Once again, you're delegating a lot more to the OS than you have to. Use
> the builtins whenever you can.

Now that I realized that chown, ulink, chmod, mkdir, rmdir ARE builtins and I
can use File::Copy I really have no need to use shell scripting.

Thanks for the info
Scott




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

Date: Tue, 13 Feb 2001 11:56:04 -0500
From: "Daniel Piché" <dpiche@speedware.com>
Subject: What  v65  refer to ?
Message-Id: <TGdi6.130575$Z2.1698443@nnrp1.uunet.ca>

Hello,

While creating an unnamed hash table, I entered  v followed by digits as the
key
   ie:    $href = {  v65 => 'some value', .... }
I was not abel to retrieve that key later on (remember, left-end side are
treated as string).

As a test, I try:    print v65  and I got   A  (humm!!)

So, my question is the following,  what does  v[0..9]* represent in Perl and
why it does'nt work the way I have use it?

Thanks in advance.

PS:  It's my first week using Perl.




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

Date: 13 Feb 2001 12:27:31 -0500
From: Jonathan Feinberg <jdf@pobox.com>
Subject: Re: What  v65  refer to ?
Message-Id: <n1bqbiik.fsf@pobox.com>

"Daniel Piché" <dpiche@speedware.com> writes:

> While creating an unnamed hash table, I entered v followed by digits
> as the key
>    ie:    $href = {  v65 => 'some value', .... }
> I was not abel to retrieve that key later on (remember, left-end
> side are treated as string).
> 
> As a test, I try:    print v65  and I got   A  (humm!!)

You are not retrieving anything from the hash reference in $href, you
are merely printing the character whose ordinal is 65 (which is
capital A, as you so cleverly discovered).  You meant

   print $href->{v65};

> So, my question is the following, what does v[0..9]* represent in
> Perl and why it does'nt work the way I have use it?

The v[0-9]+ notation for character strings is documented in the
perldata manual.

> PS:  It's my first week using Perl.

Bienvenue!  I hope you enjoy it.

-- 
Jonathan Feinberg   jdf@pobox.com   Sunny Brooklyn, NY
http://pobox.com/~jdf


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

Date: Tue, 13 Feb 2001 13:12:37 -0500
From: "Daniel Piché" <dpiche@speedware.com>
Subject: Re: What  v65  refer to ?
Message-Id: <EOei6.130582$Z2.1699109@nnrp1.uunet.ca>

Thanks to be so quick.....

As you discover, English is not my first language....

So,

First, I was not trying to get a value from the hash table within the print
statement. That print was just to see what v65 was refering to (since it not
a scalar, array, hash or function,...).

Secondly, if you try $href->{v65} it doesn't work.

Here is my test...

$href = { v65 => 'aaaaaaa', w65 => 'bbbbbbbb' };

print $href->{w65};
print $href->{v65};

It print out ,  bbbbbbbb,  no aaaaaaa ????

It look like the token v1, v2, v3, ... represent some Perl entiry!

I did find any info in documentation under  predefine variables, operator,
functions,...


Jonathan Feinberg wrote in message ...
>"Daniel Piché" <dpiche@speedware.com> writes:
>
>> While creating an unnamed hash table, I entered v followed by digits
>> as the key
>>    ie:    $href = {  v65 => 'some value', .... }
>> I was not abel to retrieve that key later on (remember, left-end
>> side are treated as string).
>>
>> As a test, I try:    print v65  and I got   A  (humm!!)
>
>You are not retrieving anything from the hash reference in $href, you
>are merely printing the character whose ordinal is 65 (which is
>capital A, as you so cleverly discovered).  You meant
>
>   print $href->{v65};
>
>> So, my question is the following, what does v[0..9]* represent in
>> Perl and why it does'nt work the way I have use it?
>
>The v[0-9]+ notation for character strings is documented in the
>perldata manual.
>
>> PS:  It's my first week using Perl.
>
>Bienvenue!  I hope you enjoy it.
>
>--
>Jonathan Feinberg   jdf@pobox.com   Sunny Brooklyn, NY
>http://pobox.com/~jdf




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

Date: 16 Sep 99 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 16 Sep 99)
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: The mail to news gateway, and thus the ability to submit articles
| through this service to the newsgroup, has been removed. I do not have
| time to individually vet each article to make sure that someone isn't
| abusing the service, and I no longer have any desire to waste my time
| dealing with the campus admins when some fool complains to them about an
| article that has come through the gateway instead of complaining
| to the source.

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


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