[8979] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 2597 Volume: 8

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu May 14 14:10:45 1998

Date: Thu, 14 May 98 11:01:37 -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           Thu, 14 May 1998     Volume: 8 Number: 2597

Today's topics:
    Re: Matching the last character in a scalar (Tad McClellan)
    Re: newbie question: handling errors (Marek Jedlinski)
    Re: OO perl and speed <jll@skynet.be>
    Re: OO perl and speed <tchrist@mox.perl.com>
    Re: output to a certain place in the HTML-file <rootbeer@teleport.com>
    Re: Perl CGI problem... <bentzen_nospam_@post8.tele.dk>
    Re: Perl not and ! operator differences <sb@sdm.de>
    Re: Perl not and ! operator differences <rootbeer@teleport.com>
    Re: Perl Not Showing Errors <rootbeer@teleport.com>
    Re: regexp for strings of chars (Craig Berry)
    Re: regexp for strings of chars <tchrist@mox.perl.com>
    Re: removing last line from file <nervick@bewellnet.com>
    Re: servers in a list <rootbeer@teleport.com>
        Startup overhead kathryn.nelson@usa.net
    Re: Startup overhead (Mike Stok)
    Re: Startup overhead <tchrist@mox.perl.com>
        TIMTOWTDI - regex vs. if (function) <ward.kaatz@pss.boeing.com>
        Digest Administrivia (Last modified: 8 Mar 97) (Perl-Users-Digest Admin)

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

Date: Thu, 14 May 1998 08:11:10 -0500
From: tadmc@flash.net (Tad McClellan)
Subject: Re: Matching the last character in a scalar
Message-Id: <ehqej6.lsa.ln@localhost>

Hauk Langlo (hauk@forumnett.no) wrote:

: I know how
: to detect a  / , but I would like a true condition only if it is the
: last character. 


   $


: For experienced perl programers, this should be no
: problem at all. 


Even for the newest of the new this should be no problem at all!

(ability to read is all that is required...)



Got a question about Regular Expressions?

Hmmmm...

How about seeing what the documentation that I already have on my
hard disk (assuming I have a proper perl installed) says?

 ...

Gotta wade through all those titles listed in 'perl.pod'...

 ...

Hey! There's one called 

   "perlre      Perl regular expressions"

that sounds pretty good...

 ...

The first major section there is called "Regular Expressions",
sounds good. I'm hot on the trail of getting this question
answered...

 ...

The first part of that section says:

----------------------------
The patterns used in pattern matching are regular expressions such as
those supplied in the Version 8 regexp routines.  (In fact, the
routines are derived (distantly) from Henry Spencer's freely
redistributable reimplementation of the V8 routines.)
See L<Version 8 Regular Expressions> for details.

In particular the following metacharacters have their standard I<egrep>-ish
meanings:

    \   Quote the next metacharacter
    ^   Match the beginning of the line
    .   Match any character (except newline)
    $   Match the end of the line (or before newline at the end)
    |   Alternation
    ()  Grouping
    []  Character class

By default, the "^" character is guaranteed to match at only the
beginning of the string, the "$" character at only the end (or before the
newline at the end) and Perl does certain optimizations with the
assumption that the string contains only one line.  Embedded newlines
will not be matched by "^" or "$".  You may, however, wish to treat a
----------------------------


There is your answer.   Right at the top of the "Regular Expressions"
                        section. Who would have thought to look there...
                    

How long would that take?

5-10 minutes?

Then you would be back to productive programming instead of waiting
to get past this problem.

How many minutes did it take for you to get this reply?

I don't expect it will compare favorably to 5-10 minutes  ;-)




*And* you will likely stumble across "extra" stuff that you weren't 
even looking for. Like "how to match the _beginning_ of a string".

So when that is needed, you'll already know it (or know how to find
out how immediately).



*And* you won't send a copy of an *already answered* question to 
tens of thousands of computers around the entire Earth.



*And* you can avoid getting your posting address entered into
configuration files for the newsreaders of some folks, which
causes the newsreader to automatically discard all of your
future posts too...



: I would be very thankfull if you could bother to help me
: on this one. Thanks

We would be thankful if you could find the time to do a cursory
look for the answer to your question *before* you ask for others
to spend their time on it...


--
    Tad McClellan                          SGML Consulting
    tadmc@metronet.com                     Perl programming
    Fort Worth, Texas


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

Date: Thu, 14 May 1998 16:15:33 GMT
From: cicho@polbox.com (Marek Jedlinski)
Subject: Re: newbie question: handling errors
Message-Id: <355ae346.2247085@news.nask.org.pl>
Keywords: If you're happy and you know it, clunk your chains.

angst <angst@scrye.com>  wrote:

>print "hello world" and die("i didn't really want to print that");

>open(FOO,"/restricted/file") or print "i didn't really want to open that file anyway.\n";

Admit, you've been secretly implementing Marvin the Paranoid Android in
perl :)

 .marek



-- 
After things go from bad to worse, the cycle will repeat itself.
http://come.to/fnord/



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

Date: Thu, 14 May 1998 19:01:40 +0200
From: Jean-Louis Leroy <jll@skynet.be>
Subject: Re: OO perl and speed
Message-Id: <VA.000000bd.0b4f9888@jll>

In article <6jetqa$l52$1@csnews.cs.colorado.edu>, Tom Christiansen wrote:

> sub fget_four { shift->{"fourth"} }

> timethese $COUNT, {
>     "get meth" => sub {  $ob->get_four },
>     "get dual" => sub {  $ob->four },
>     "get fast" => sub {  $ob->fget_four },
>     "get Hash"   => sub {  $ob->{fourth} },
> };

> Benchmark: timing 1000000 iterations of get Hash, get dual, get fast, get meth...
>   get Hash:  6 secs ( 5.21 usr  0.00 sys =  5.21 cpu)
>   get dual: 22 secs (20.91 usr  0.00 sys = 20.91 cpu)
>   get fast: 14 secs (12.48 usr  0.00 sys = 12.48 cpu)
>   get meth: 19 secs (16.96 usr  0.00 sys = 16.96 cpu)

> Your idea of rough diverges from my own.

No, we're in agreement. If method call costs approx the same as hash access, a fast 
'get' accessor like your fget_four will cost approx as much as two hash 
accesses...as your benchmark demonstrates.

Jean-Louis Leroy
http://ourworld.compuserve.com/homepages/jl_leroy

ps why do you sometimes write $obj->{"fourth"} and sometimes $obj->{fourth}?




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

Date: 14 May 1998 17:22:18 GMT
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: Re: OO perl and speed
Message-Id: <6jf98a$4oh$2@csnews.cs.colorado.edu>

 [courtesy cc of this posting sent to cited author via email]

In comp.lang.perl.misc, 
    jll@skynet.be writes:

:No, we're in agreement. If method call costs approx the same as hash
:access, a fast 'get' accessor like your fget_four will cost approx as
:much as two hash accesses...as your benchmark demonstrates.

But the fget_four is unrealistic.  Few access methods are so simple.
In fact, I'm the only one I've ever caught writing in so abbreviated
a fashion.  And for anything else, it's much worse.

I do question my own 10x number, though.  I wonder what 
test I ran.

:ps why do you sometimes write $obj->{"fourth"} and sometimes $obj->{fourth}?

To make you ask these questions. :-)

--tom
-- 
I've got plenty of inputs and outputs.  I don't need the video. --Andrew Hume


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

Date: Thu, 14 May 1998 16:45:59 GMT
From: Tom Phoenix <rootbeer@teleport.com>
To: Jari Martikainen <jm@kolumbus.fi>
Subject: Re: output to a certain place in the HTML-file
Message-Id: <Pine.GSO.3.96.980514094146.21974Z-100000@user2.teleport.com>

On Thu, 14 May 1998, Jari Martikainen wrote:

> I have a script that adds the data from a form to the bottom of the
> HTML-file. Is it possible to put the data to a certain place within
> HTML? What is the Perl syntax and how do I define the place in HTML?

The FAQ talks about inserting "a line in the middle of a file". Is that
what you want?

>   $tmp =~ s/\+/ /g ;
>   while ($tmp =~ /%([0-9A-Fa-f][0-9A-Fa-f])/)
> {
>       $num = $1;
>       $dec = hex($num);
>       $chr = pack("c",$dec);
>       $chr =~ s/&/and/g;
>       $tmp =~ s/%$num/$chr/g;
>   }

If you're doing what it looks like you're doing, there's a module which
does it for you, and better. :-)

>   open(TFILE,">$filename");

Even when your script is "just an example" (and perhaps especially in that
case!) you should _always_ check the return value after opening a file.

>  `Type $filename >> document.htm`;

In general, backticks in a void context should be avoided. But also, this
operation (if you're doing what I think you're doing) can be done easily
within Perl, so you don't need to start another program.

Hope this helps!

-- 
Tom Phoenix       Perl Training and Hacking       Esperanto
Randal Schwartz Case:     http://www.rahul.net/jeffrey/ovs/



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

Date: Tue, 12 May 1998 18:35:59 +0200
From: "Martin Bentzen" <bentzen_nospam_@post8.tele.dk>
Subject: Re: Perl CGI problem...
Message-Id: <6ja53r$h28$1@news-inn.inet.tele.dk>

OK - thank you all
 - my problem is solved !!


/greetings
Martin




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

Date: 14 May 1998 16:21:07 GMT
From: Steffen Beyer <sb@sdm.de>
Subject: Re: Perl not and ! operator differences
Message-Id: <6jf5lj$bm0$1@bsdti6.sdm.de>

In article <tbaf8llyg8.fsf@blue.sea.net>,
Jari Aalto Jari Aalto+usenet.nil > <<jari.aalto@poboxes.com> wrote:

>     Can someone give me an example when I have to use ! operator
>     over "not". It seems that the more readable "not" is much
>     better in code that will be maintained in big projects.

The difference is just operator precedence. See "man perlop" for details.

If you always use parentheses to force evaluation order, you can use these
two operators interchangeably.

Yours,
-- 
    Steffen Beyer <sb@engelschall.com>
    Free Perl and C Software for Download: www.engelschall.com/u/sb/download/


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

Date: Thu, 14 May 1998 17:03:33 GMT
From: Tom Phoenix <rootbeer@teleport.com>
Subject: Re: Perl not and ! operator differences
Message-Id: <Pine.GSO.3.96.980514094708.21974b-100000@user2.teleport.com>

On 14 May 1998, it was written:

>     Can someone give me an example when I have to use ! operator
>     over "not". 

Nope. :-)  In any case where one works, the other can be made to work as
well. (Given, of course, that you're not using some old version of Perl.)
The difference is essentially that one may require parentheses that the
other doesn't.

> It seems that the more readable "not" is much
>     better in code that will be maintained in big projects.

Perhaps that's true, perhaps that's false. Part of the reason that Perl
gives you More Than One Way To Do It is so that you can choose one which
will be easily understood by you and (we hope) easily understood by your
maintenance programmer. But if there's any doubt, you should always use
parentheses to show the desired precedence, since that should be clear to
everyone.

Hope this helps!

-- 
Tom Phoenix       Perl Training and Hacking       Esperanto
Randal Schwartz Case:     http://www.rahul.net/jeffrey/ovs/



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

Date: Thu, 14 May 1998 17:08:43 GMT
From: Tom Phoenix <rootbeer@teleport.com>
To: gwebb@reedtech.com
Subject: Re: Perl Not Showing Errors
Message-Id: <Pine.GSO.3.96.980514100418.21974c-100000@user2.teleport.com>

On Thu, 14 May 1998 gwebb@reedtech.com wrote:

> I have a problem when running large Perl programs where if there is an
> error someplace, Perl will throw its hands up in the air and report
> only: 
> 
> "XXXX had compilation errors."
> 
> and not tell me *anything* about those errors.  

For real? That message should show up only at the end of a 'perl -c'
check, and only after other diagnostic messages. If that's not happening
for you, are you using a recent version of Perl? Cheers! 

-- 
Tom Phoenix       Perl Training and Hacking       Esperanto
Randal Schwartz Case:     http://www.rahul.net/jeffrey/ovs/



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

Date: 14 May 1998 16:48:29 GMT
From: cberry@cinenet.net (Craig Berry)
Subject: Re: regexp for strings of chars
Message-Id: <6jf78u$nf6$1@marina.cinenet.net>

Jim Monty (monty@primenet.com) wrote:
: I'm a newbie trying to understand, so pardon me if I'm missing something
: obvious, but...
: 
: $ cat test.pl
: #!/usr/bin/perl -w
: 
: @a = 'aaaaaabbb,,,xyfooffff' =~ m/(.)\1{2}/g;
: 
: foreach (@a) {
:     print "$_\n";
: }
: $ ./test.pl
: a
: a
: b
: ,
: f
: $
: 
: Is this the intended match/output?

Yes; my regex above tells you which chars are repeated three times.  To
get the actual three-char strings is a bit trickier, since you need two
sets of capturing parens, which blows the accumulate to list with /g
strategy. 

---------------------------------------------------------------------
   |   Craig Berry - cberry@cinenet.net
 --*--    Home Page: http://www.cinenet.net/users/cberry/home.html
   |      Member of The HTML Writers Guild: http://www.hwg.org/   
       "Every man and every woman is a star."


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

Date: 14 May 1998 17:17:44 GMT
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: Re: regexp for strings of chars
Message-Id: <6jf8vo$4oh$1@csnews.cs.colorado.edu>

 [courtesy cc of this posting sent to cited author via email]

In comp.lang.perl.misc, 
    cberry@cinenet.net (Craig Berry) writes:
:Yes; my regex above tells you which chars are repeated three times.  To
:get the actual three-char strings is a bit trickier, since you need two
:sets of capturing parens, which blows the accumulate to list with /g
:strategy. 

You can use a while loop

    my $string = 'aaaaaabbb,,,xyfooffff';
    push(@trips, $1) while $string =~ m/((.)\2{2})/g;

Or you get use careful greps:

    my $count  = 0;
    my $string = 'aaaaaabbb,,,xyfooffff';
    @matches = grep { ++$count & 1 } $string =~ m/((.)\2{2})/g;

For overlapping matches, do this:

    my $string = 'aaaaaabbb,,,xyfooffff';
    push(@trips, $1) while $string =~ m/(?=((.)\2{2}))/g;

or

    my $count  = 0;
    my $string = 'aaaaaabbb,,,xyfooffff';
    @matches = grep { ++$count & 1 } $string =~ m/(?=((.)\2{2}))/g;

--tom
-- 
I wish there was a knob on the TV to turn up the intelligence.  There's
a knob called "brightness", but it doesn't work.
                --Gallagher


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

Date: Thu, 14 May 1998 10:30:34 -0600
From: "Nervick" <nervick@bewellnet.com>
Subject: Re: removing last line from file
Message-Id: <355b1c48.0@news.bewellnet.com>

I would try something like this.

open FILE, "file.txt";
@lines = <FILE>;
close FILE;

$lastline = pop @lines;

open FILE, ">file.txt";
foreach $line ( @lines ) {
   print FILE "$line";
}
close FILE;

This is very simple to read.  It may not be the fastest way to go, but it
works.

Nathan Franzen wrote ...
>I first thought of something along the lines of:
>
>perl -nibak -e 'unless(eof){print} else{undef $^I;print "last: $_"}' fname
>
>However, the "undef $^I" didn't do what I expected, and I had to resort to
>
>perl -nibak -e 'unless(eof){print} else{undef $^I;print STDOUT "last: $_"}'
>
>to get what I wanted.  The mysteries of $^I are still shrouded to me.
>So I wonder: once I have invoked $^I, how do I get rid of it?
>
>Now, my one-line response to baton's question probably isn't what is
>desired, since baton probably wants to have user input as to whether the
>last line should be deleted or not.  I think more that one line will be
>required.
>
>-Nathan
>




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

Date: Thu, 14 May 1998 16:35:56 GMT
From: Tom Phoenix <rootbeer@teleport.com>
To: "Bruce J. Downs" <downs.bj.1@pg.com>
Subject: Re: servers in a list
Message-Id: <Pine.GSO.3.96.980514092941.21974X-100000@user2.teleport.com>

On Thu, 14 May 1998, Bruce J. Downs wrote:

> I have a perl script that I run on my NT server.  The script is similar
> to a "dir" command.  It will return some info on the directories.  My
> problem is, I want to run this script command on a number of servers. 

If it's installed on each server on which you wish to run it, you can
easily make a Perl script (using the LWP module) which will fetch one
after another. Would that do what you need?

Of course, if a script isn't installed, it's pretty hard to run it. :-) 

Hope this helps!

-- 
Tom Phoenix       Perl Training and Hacking       Esperanto
Randal Schwartz Case:     http://www.rahul.net/jeffrey/ovs/



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

Date: Thu, 14 May 1998 15:56:56 GMT
From: kathryn.nelson@usa.net
Subject: Startup overhead
Message-Id: <6jf487$gbk$1@nnrp1.dejanews.com>

I wrote a 40 line script to put up a form using the CGI module.
The server is running FreeBSD and perl 5.0 patchlevel 4 subversion 4.
The form took about 7 seconds to appear in my browser window after entering
the URL.

Running the Benchmark module showed .83 seconds of cpu time. I imagine
the other 6 seconds is overhead at start up.  I removed all my "use"
statements like strict, diagnostics, constant and left only "use CGI;".

Now I'm down to about 3 seconds.

My question is: are there any other things I can do to reduce this further?
Most code optimizations I've tried reduces the cpu time a tenth of a second
or two but startup remains lengthy.

--
Kathy.

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/   Now offering spam-free web-based newsreading


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

Date: 14 May 1998 16:16:11 GMT
From: mike@stok.co.uk (Mike Stok)
Subject: Re: Startup overhead
Message-Id: <6jf5cb$nrm@news-central.tiac.net>

Have you looked at the possibility of using an Apache server with mod_perl
(see http://perl.apache.org )  This allows the server to cache scripts in
their bytecode form once they've been compiled (or even pre-load scripts &
modules you know you'll need) so the startup time for subsequent requests
is much smaller.

Hope this helps,

Mike

In article <6jf487$gbk$1@nnrp1.dejanews.com>,  <kathryn.nelson@usa.net> wrote:
>I wrote a 40 line script to put up a form using the CGI module.
>The server is running FreeBSD and perl 5.0 patchlevel 4 subversion 4.
>The form took about 7 seconds to appear in my browser window after entering
>the URL.
>
>Running the Benchmark module showed .83 seconds of cpu time. I imagine
>the other 6 seconds is overhead at start up.  I removed all my "use"
>statements like strict, diagnostics, constant and left only "use CGI;".
>
>Now I'm down to about 3 seconds.
>
>My question is: are there any other things I can do to reduce this further?
>Most code optimizations I've tried reduces the cpu time a tenth of a second
>or two but startup remains lengthy.
>
>--
>Kathy.
>
>-----== Posted via Deja News, The Leader in Internet Discussion ==-----
>http://www.dejanews.com/   Now offering spam-free web-based newsreading


-- 
mike@stok.co.uk                    |           The "`Stok' disclaimers" apply.
http://www.stok.co.uk/~mike/       |   PGP fingerprint FE 56 4D 7D 42 1A 4A 9C
http://www.tiac.net/users/stok/    |                   65 F3 3F 1D 27 22 B7 41
stok@colltech.com                  |            Collective Technologies (work)


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

Date: 14 May 1998 16:33:30 GMT
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: Re: Startup overhead
Message-Id: <6jf6cq$r7j$3@csnews.cs.colorado.edu>

 [courtesy cc of this posting sent to cited author via email]

In comp.lang.perl.misc, 
    kathryn.nelson@usa.net writes:
:My question is: are there any other things I can do to reduce this further?

    % man perlfaq3
       How can I make my CGI script more efficient?

Answer: try Apache's mod_perl.  It really will make you
remarkably happy.

--tom
-- 
    "If it makes goo on the windshield, we'll call it a bug." --Larry Wall


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

Date: Thu, 14 May 1998 17:17:16 GMT
From: Ward Kaatz <ward.kaatz@pss.boeing.com>
Subject: TIMTOWTDI - regex vs. if (function)
Message-Id: <355B271C.61F6@pss.boeing.com>

perl mongers,

I am reformatting day and month fields returned from localtime(time) to
comply with a YYYYMMDD format.

Since localtime(time) will return month/day without a leading zero, I
have to prefix a single digit month/day with a zero for storage in a
datafile.  my original cut just checked the length of month/day and if
length is equal to one, concatinates the digit after a leading zero. 
this works just fine.

however, it seemed to me I could do the same thing with a regular
expression.  Not being entirely comfortable (yet) with regex's, I
cracked open the camel and with some trial and error got a regex to
produce the same results as my original code.

below is the 'original' code, note the use of $ENV{uppercaserequired} is
an os/390-specific "feature" (in case you wonder) for retreiving a pased
parm to a called language extension routine within the IBM net.data
product.  (I am passing the result of a DB2 query).

the regex works, however I am curious if it is the best way to do it
(and/or is it actually just working by chance!), or rather, are they
other ways to do it that may be more efficient (or perhaps, more
"perlish").

I must say, the more I use perl, the more fun it gets!

# code snippet begins
$Count = $ENV{PARM1}; # this is how variables are passed on os/390 in
net.data's perl lei
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
chomp $mon;
chomp $mday;
if ( length($mon) == 1 ) { $mon = "0".($mon + 1); }
if ( length($mday) == 1 ) { $mday = "0".$mday; }
open(FNAME, ">>\/webserver\/htdocs\/mast\/data\/pncount.dat") || die
"could not open: $!";
print FNAME (1900 + $year),$mon,$mday, "::", $Count, "\n";
close(FNAME);
# code snippet ends

the regex I swapped in for the length tests looks like:

# start
$mon =~ s/^\d$/0$&/;
$mday =~ s/^\d$/0$&/;
# end

Best regards,
Ward Kaatz

PS: Hopefully my cheesy news reader does not mangle any of the above! 
boeing=pob
PSS: CC'd (or direct) replies to this address are welcome


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

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

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