[18273] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 441 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Mar 7 21:10:48 2001

Date: Wed, 7 Mar 2001 18:10:14 -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: <984017414-v10-i441@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Wed, 7 Mar 2001     Volume: 10 Number: 441

Today's topics:
    Re: pi day <mischief@velma.motion.net>
        Question on sockets <hafateltec@hotmail.com>
    Re: Regexp matching between <tags> content </tags> (Sam Holden)
    Re: Regexp matching between <tags> content </tags> <godzilla@stomp.stomp.tokyo>
    Re: Regexp matching between <tags> content </tags> <godzilla@stomp.stomp.tokyo>
    Re: remove single quotes from filename under RHL <krahnj@acm.org>
        removing text from log files <mike@nospam.mishlercomputers.com>
    Re: removing text from log files <bwalton@rochester.rr.com>
    Re: RFC: FAQ3 update -- Using less memory <dan@tuatha.sidhe.org>
    Re: Setting cookies in header to pass to Web Server? <whataman@home.com>
        Small perl sort question <bcoon@sequenom.com>
    Re: Small perl sort question (Abigail)
        Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)

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

Date: Wed, 07 Mar 2001 23:39:34 -0000
From: Chris Stith <mischief@velma.motion.net>
Subject: Re: pi day
Message-Id: <tadhlm4o0t8la8@corp.supernews.com>

Ilmari Karonen <iltzu@sci.invalid> wrote:
> In article <taatr2pan9q5ad@corp.supernews.com>, Chris Stith wrote:
>>John McNamara <jmcnamara@cpan.org> wrote:
>>> Ar 6 Mar 2001 07:03:41 -0000, do scriobh Jonathan Stowe
>>> <gellyfish@gellyfish.com>:
>>>>On Mon, 05 Mar 2001 16:30:14 -0000 Greg Bacon wrote:
>>>>> Don't forget that it's also the birthday of Albert Einstein, Billy
>>>>> Crystal, and noted Perl plinker Greg Bacon.  Send Guinness. :-)
>>
>>>>I didnt think there was an implementation of the Network Guinness Transport
>>>>Protocol yet.
>>
>>> Guinapster anyone?
>>
>>Too specialized. We need a stable, general protocol. How about
>>RATT? Remote Alcohol Tap Transport?

> You could just set up a FreeBeer node and put the pints of Guinness on
> it.  Anyone who wants one would yell for it, and the pints would get
> passed between adjacent nodes until they reached the people who asked
> for them.

> Such a system would minimize the distance anyone would have to carry the
> pints, and eliminate any central location where the network could be
> compromised if someone, say, passed out, or stumbled while carrying the
> beers.  The disadvantage would be that, due to transmission losses, the
> pints would be likely to arrive empty.

Perhaps we should employ animal labor -- something that doesn't like
beer (or at least not stouts, since we're mostly interested in
Guinness). Perhaps we just need to make a pint a special case of
packet (with appropriate flags in the header, of course ;-) and
use a slight variation of Internet (IETF) RFC 1149.

Chris

p.s. How many swallows does it take to carry a pint?

p.p.s. Perhaps we should use African swallows?

-- 
Christopher E. Stith
It's not the U in UBE that pisses people off. It's the B.
  -- Martien Verbruggen in clp.misc



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

Date: Thu, 8 Mar 2001 09:07:05 +1000
From: "Copenhagen" <hafateltec@hotmail.com>
Subject: Question on sockets
Message-Id: <Iazp6.2639$Cd.53477598@news.randori.com>

This little script is giving me a headache.  It returns what I expect it to
except I am getting an error " Use of uninitialized value at soctest1.pl
line 13 "
My argument is 192.168.1.10 which is the webserver.  Where is my
'uninitialized value' comming from? "F" ? That is my filehandle for the
socket? I am bedazzaled.

#!/usr/bin/perl -w

use Socket;

# If no parameters were given, print out help text
if ($#ARGV) {
 print "Usage: $0 Ipaddress\n";
 print "\n Returns the HTTP result code from a web server.\n\n";
 exit(-1);
}

# contact the server
if (open_TCP(F, $ARGV[0], 80) == undef) {
 print "Error connecting to server at $ARGV[0]\n";
 exit(-1);
}

# send the GET method with / as a parameter
print F "GET / HTTP/1.0\n\n";

# get the response
$return_line=<F>;

# print out the response
print "The server had a response line of: $return_line";
close(F);


sub open_TCP
 {
  # get parameters
  my ($FS, $dest, $port) = @_;

  my $proto = getprotobyname('tcp');
  socket($FS, PF_INET, SOCK_STREAM, $proto);
  my $sin = sockaddr_in($port,inet_aton($dest));
  connect($FS,$sin) || return undef;

  my $old_fh = select($FS);
  $| = 1;           # don't buffer output
  select($old_fh);
  1;
 }

--
##############Þ
print "\n Welcome to NEPP";$Þ=1;while ($Þ){
print "\n$Þ";$Þ++;if ($Þ == 1000) {
print "\n$Þ"."\nWell almost never ending :þ";exit;}}
##############Þ




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

Date: 7 Mar 2001 23:46:38 GMT
From: sholden@pgrad.cs.usyd.edu.au (Sam Holden)
Subject: Re: Regexp matching between <tags> content </tags>
Message-Id: <slrn9adi2t.jnb.sholden@pgrad.cs.usyd.edu.au>

On Wed, 07 Mar 2001 14:36:46 -0800,
	Godzilla! <godzilla@stomp.stomp.tokyo> wrote:
>Bart Lateur wrote:
> 
>> Godzilla! wrote:
> 
>> >Use of a regex or multiple regexes to accomplish
>> >your task would be a poor choice in programming.
> 
>> That's not the first time this week you say something liek this. I think
>> you're using the wrong language. You'd better go to C, or any other
>> compiled language, if you insist on doing things in such a low level
>> manner. Regexes are one of the selling poitns of Perl, and it's really
>> good (fast) at it. If you want to bypass regexes for a (IMO)
>> misconceived idea of "efficiency", you're using the wrong language.
>
>
>
>
>Your code methodology is an average 2.5 times slower than
>my methodology. Your code is exceptionally inefficient and
>consumes memory to an even greater degree.

Why don't you listen to the advice... If you really care more about runtime
speed and memory usage than flexibility and easy expression of concepts then
you should move on over to C...

int main() {
	puts("A string\n");
}

Runs in 0.01 seconds and takes up 800k of memory on my machine.

#!/usr/bin/perl
print "A string\n";

Runs in 0.04 seconds and takes up 2500k of memory on my machine.

So switch to C and you'll get a 4x speed increase and a 3x memory usage
descrease.

Well as accurately as your benchmark which compared techniques
that did different things, did error checking on the one you wanted
to be much slower and didn't bother on the other, used hard coded
constants to make modification difficult on the one your wanted faster, and
only tested with one data set.

>    'name1' =>
>    '$content = "Ignore This <!-- #myinclude -->
>     Test One <!-- #myincludeEnd --> Ignore This";
>     if ($content=~/<!-- #myinclude -->(.*?)<!-- #myincludeEnd -->/si)
>      { $content = $1; }',

Does case insensitive matching which is required in many domains.
Matches the first ending block after the starting block.
Does error checking in case there was no start or end block.

>
>    'name2' =>
>    '$content = "Ignore This <!-- #myinclude -->
>     Test One <!-- #myincludeEnd --> Ignore This";
>     $start = index ($content, "<!-- #myinclude -->") + 19;
>     $content = substr ($content, $start, rindex ($content,
>                        "<!-- #myincludeEnd -->") - $start);',
 
Does case sensitive matching - performance will be hit if you fix that.
Matches the last ending block after the staring block.
Doesn't bother with error checking at all.
Hardcodes 19 for a miniscule performance and memory gain over using a variable
and length to make the code modifiable easily later. I guess the milliseconds
in runtime are better than hours in debug time to you - Another reason
to switch to C.


So you produce some buggy code (doesn't do the same as the code your comparing
it with) which means if you do 100,000 matches you'll save an entire second
(OK a bit less than a second) of runtime. Sign me up now.

I save an entire second every 100,000 matches that I do... and the only cost
is that the code doesn't actually work.

-- 
Sam

i am not grouchy. i have a personality deficiency.
	-- Uri Guttman in <x7k8tshmn9.fsf@home.sysarch.com>


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

Date: Wed, 07 Mar 2001 15:56:19 -0800
From: "Godzilla!" <godzilla@stomp.stomp.tokyo>
Subject: Re: Regexp matching between <tags> content </tags>
Message-Id: <3AA6CAA3.8A2699DE@stomp.stomp.tokyo>

Bart Lateur wrote:
 
> Godzilla! wrote:
> > Bart Lateur wrote:
> > > Godzilla! wrote:

(more snippage not noted by Lateur)


> > > > Use of a regex or multiple regexes to accomplish
> > > > your task would be a poor choice in programming.
 
> > > That's not the first time this week you say something liek this. I think
> > > you're using the wrong language. You'd better go to C, or any other
> > > compiled language, if you insist on doing things in such a low level
> > > manner. Regexes are one of the selling poitns of Perl, and it's really
> > > good (fast) at it. If you want to bypass regexes for a (IMO)
> > > misconceived idea of "efficiency", you're using the wrong language.
 
> > Your code methodology is an average 2.5 times slower than
> > my methodology. Your code is exceptionally inefficient and
> > consumes memory to an even greater degree.
 
> You're talking about a difference of one second for 100000 matches. That
> is a difference of 10 microseconds per regex match. I would hardly call
> that "exceptionally inefficient".


I consider a two-hundred-fifty percent loss in efficiency
to be exceptional. This is further exasperated by your code's
exhorbitant use of memory, in comparison. Additionally, your
code does not provide for multiple matching as my originally
posted code does.

Would you like some salt and pepper with your Lambaste crow,
or would you simply prefer Limburger cheese with your whine?

Godzilla!


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

Date: Wed, 07 Mar 2001 16:06:39 -0800
From: "Godzilla!" <godzilla@stomp.stomp.tokyo>
Subject: Re: Regexp matching between <tags> content </tags>
Message-Id: <3AA6CD0F.42136304@stomp.stomp.tokyo>

Sam Holden wrote:
 
> Godzilla! wrote:
> >Bart Lateur wrote:
> > > Godzilla! wrote:


(lots of snippage)


> Why don't you listen to the advice... If you really care more
> about runtime speed and memory usage than flexibility and easy
> expression of concepts then you should move on over to C...


I do believe you mean these inane statements you made
under another of your hundreds of fake personalities,
just as inane as are your remarks in this article.

Well gosh, seems this week I am not bruising your fragile
masculine ego, I am stomping your fragile masculine ego,
by your beckoning, Frank.



* laughs *

Godzilla!


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

Date: Thu, 08 Mar 2001 00:04:16 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: remove single quotes from filename under RHL
Message-Id: <3AA6CDD7.DF94B5C2@acm.org>

John Hunter wrote:
> 
> I am on a Redhat Linux 6.2 system and am writing a perl script to
> automatically remove some single quotes from filenames.  So far with
> no luck.
> 
> The script:
> 
> #!/usr/local/bin/perl -w
> 
> use strict;
> use Shell('mv');
> foreach my $file (@ARGV) {
>   next unless -e $file;
>   my $newname = $file;
>   $newname =~ s/\'//g;
>   $file =~ s/\'/\\'/g;
>   print "Moving $file to $newname\n";
>   mv($file, $newname);
> }
> 
> A sample run:
> video:/video/hunter/backups/tcuriel> testperl querytoAnn\'sPrayers
> Moving querytoAnn\'sPrayers to querytoAnnsPrayers
> sh: unexpected EOF while looking for `''
> sh: -c: line 2: syntax error
> video:/video/hunter/backups/tcuriel>
> 
> Suggestions please ....

#!/usr/local/bin/perl -w
use strict;

foreach my $file ( @ARGV ) {
  next unless -e $file;
  ( my $newname = $file ) =~ tr/'//d;
  print "Moving $file to $newname\n";
  rename( $file, $newname );
}


John


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

Date: Wed, 7 Mar 2001 16:41:23 -0700
From: "Michael Mishler" <mike@nospam.mishlercomputers.com>
Subject: removing text from log files
Message-Id: <4Dzp6.1810$g14.371237@news.uswest.net>

Basically a newbie here, and was just wondering how to remove text from a
log file.

For example:  I can make a perl script write to a log file and append
message to that log file, but what commands are available in perl to remove
text?

Please explain or point me in the right direction.

Thanks for any replys




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

Date: Thu, 08 Mar 2001 02:00:52 GMT
From: Bob Walton <bwalton@rochester.rr.com>
Subject: Re: removing text from log files
Message-Id: <3AA6E8CD.22509B86@rochester.rr.com>

Michael Mishler wrote:
> 
> Basically a newbie here, and was just wondering how to remove text from a
> log file.
> 
> For example:  I can make a perl script write to a log file and append
> message to that log file, but what commands are available in perl to remove
> text?
> 
> Please explain or point me in the right direction.
 ...
Well, I assume you mean you want to remove text from the *beginning* of
the log file, and that your log file is a plain old text file.  If so,
the best deal is probably to create a new log file containing just the
information you want to keep, unlink the old log file, and then rename
the new one to the same name as the old one.  Generally, writing to a
file is a process which either adds data to a file or overwrites
existing data in a file.  It doesn't remove data from a file (or insert
data into the start or middle of a file).  If the process writing the
log file remains active during these operations, you will want to use
cooperative file locking to ensure you don't miss or mangle data.

Appropriate Perl functions are open, close, rename, unlink, flock.
-- 
Bob Walton


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

Date: Wed, 07 Mar 2001 23:10:37 GMT
From: Dan Sugalski <dan@tuatha.sidhe.org>
Subject: Re: RFC: FAQ3 update -- Using less memory
Message-Id: <Ndzp6.14573$Ok4.1941411@news1.rdc1.ct.home.com>

Bart Lateur <bart.lateur@skynet.be> wrote:
> Dan Sugalski wrote:

>>glibc, particularly, seems to use mmap() to get a chunk of memory in
>>some circumstances, and when you release a segment that was allocated
>>in this way it gets unmapped. (And no, I'm not sure of the size, nor
>>of the vintage of glibc needed for this) The Mac port of perl will
>>do the same thing

> Heh? Maybe you're talking about the LWP tool, I'm not familiar with
> those, but on an ordinary Mac, applications NEVER request more memory
> from the OS, and they NEVER give anything back either, during their
> life. All apps have a memory preference set, and they get a certain
> amount of memory at startup, and that they have to get by with for the
> rest of their life. All memory allocations and frees are within the
> scope of this chunk. The Finder (OS with GUI) is the only exception to
> this rule.

I'm pretty sure that this isn't the case, and you can ask for memory
that's not in your allocated memory chunk--whether you get it or not
is an entirely different question, but you can ask. Regardless, the
Mac keeps track of how much of the allocated memory space for an app
is actually used. If you don't use a piece of the space, it
won't take up RAM (or swap, IIRC, which I might not) leaving it free for
other apps.

					Dan


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

Date: Wed, 07 Mar 2001 23:12:11 GMT
From: "What A Man !" <whataman@home.com>
Subject: Re: Setting cookies in header to pass to Web Server?
Message-Id: <3AA6C0C1.258A54EF@home.com>

Rafael Garcia-Suarez wrote:
> 
> >                 $data = $response->as_string();
> 
> You should use $response->content() here if you don't want headers to
> show up.
> 


Much thanks, Rafael! You have no idea the grief you saved
me. Also... I wasn't going to post that script again that
someone so graciously gave me, but in case someone uses
it, it had an error in it. Here it is again, fixed.

my ($err_msg, $data) = &hackhost($url);
if ($err_msg) {
        print "Error: $err_msg.\n";
        }
elsif (open(TFILE, ">$tmpfile")) {
        binmode(TFILE);
        print TFILE $data;
        close(TFILE);
        }
else {
        print "Error: could not saved retrieved data -
$!.\n";
        }

sub hackhost {
        my ($url) = @_;
        my $data = '';
        my $err_msg = '';
        Err: {
                my $url_data = $url;
                my $url_html = $url;
                $url_html =~ s!/([^/]*)$!/!o; # strip to
folder

                use LWP;
                use HTTP::Request;
                use HTTP::Cookies;
                use HTTP::Headers;
                use LWP::UserAgent;

                my $request = ();
                my $response = ();

# Make initial request to the first page.  This makes
things
# "look good" to any smart observers on the far side.  We
also
# catch the first cookie and persist it:

                my $ua = new LWP::UserAgent;
                my $headers = new HTTP::Headers;
                my $cookie_jar = new HTTP::Cookies;
              
# Simulate IE 5.5 browser:
$ua->agent( 'Mozilla/4.0 (compatible; MSIE 5.01; Windows
NT 5.0)' );
$headers->header(
                  'HTTP_ACCEPT' => 'application/x-tar,
application/zip',
                  'HTTP_ACCEPT-ENCODING' => 'gzip,
deflate',       
                  'HTTP_ACCEPT_LANGUAGE' =>
'en-us,ko;q=0.7,ja;q=0.3',
                  'HTTP_CONNECTION' => 'close',  
                  'HTTP_REFERER' => '',
                   );

# Request initial HTML page:
$request = new HTTP::Request('GET' => $url_html); 
$response = $ua->request( $request);
$cookie_jar->extract_cookies( $response );

                unless ($response->is_success()) {
                        $err_msg =
$response->error_as_HTML();
                        next Err;
                        }

# Request follow-up page, using simulated REFERER and
cookies:
                $headers->header('HTTP_REFERER' =>
$url_html);
                $request = new HTTP::Request('GET' =>
$url_data);
                $cookie_jar->add_cookie_header( $request
);
                $response = $ua->request( $request );
                $cookie_jar->extract_cookies( $response );

                unless ($response->is_success()) {
                        $err_msg =
$response->error_as_HTML();
                        next Err;
                        }
                $data = $response->as_string();

                last Err;
                }
        return ($err_msg, $data);
        };


Regards,
--Dennis


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

Date: Wed, 07 Mar 2001 15:15:31 -0800
From: Bryan Coon <bcoon@sequenom.com>
Subject: Small perl sort question
Message-Id: <3AA6C113.664B72EC@sequenom.com>

A small question...

With the following array:
my @list = ("5", "8", "15", "X", "7", "Y");

my @sorted = sort { $a <=> $b } @list;

works like a charm, with the exception that I get complaints that
'Argument "Y" isn't numeric in sort at.. etc.' .  This is not such a
problem, but is there a generally accepted way to use this sort after a
test for numerics?  This seems simple but I was unable to find such a
test.... It is also not important that the strings are sorted.

Thanks!



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

Date: 7 Mar 2001 23:31:12 GMT
From: abigail@foad.org (Abigail)
Subject: Re: Small perl sort question
Message-Id: <slrn9adh60.q1s.abigail@tsathoggua.rlyeh.net>

Bryan Coon (bcoon@sequenom.com) wrote on MMDCCXLV September MCMXCIII in
<URL:news:3AA6C113.664B72EC@sequenom.com>:
__ A small question...
__ 
__ With the following array:
__ my @list = ("5", "8", "15", "X", "7", "Y");
__ 
__ my @sorted = sort { $a <=> $b } @list;
__ 
__ works like a charm, with the exception that I get complaints that
__ 'Argument "Y" isn't numeric in sort at.. etc.' .  This is not such a
__ problem, but is there a generally accepted way to use this sort after a
__ test for numerics?  This seems simple but I was unable to find such a
__ test.... It is also not important that the strings are sorted.


One way of doing it would be:

    my @sorted = sort (grep {/^\d+$/} @list), grep {/\D/ || /^$/} @list;


Abigail
-- 
$" = "/"; split // => eval join "+" => 1 .. 7;
*{"@_"} = sub {foreach (sort keys %_) {print "$_ $_{$_} "}};
%_ = (Just => another => Perl => Hacker); &{%_};


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

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


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