[comp.lang.perl] Perl bug

dupuy@cs.columbia.edu (Alexander Dupuy) (02/09/91)

The following (erroneous?) script gets a memory fault on a Sun-4 running 4.0.3:

while (<>)
{
    ( $start, $end, $line ) =~ /(\d+)-(\d+):(.*\n)/;

    @foo = $start .. $end;

}

perl -v

This is perl, version 3.0

$Header: perly.c,v 3.0.1.10 91/01/11 18:22:48 lwall Locked $
Patch level: 44

@alex
--
-- 
inet: dupuy@cs.columbia.edu
uucp: ...!rutgers!cs.columbia.edu!dupuy

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (02/12/91)

In article <DUPUY.91Feb8224616@hudson.cs.columbia.edu> dupuy@cs.columbia.edu writes:
: The following (erroneous?) script gets a memory fault on a Sun-4 running 4.0.3:
: 
: while (<>)
: {
:     ( $start, $end, $line ) =~ /(\d+)-(\d+):(.*\n)/;
: 
:     @foo = $start .. $end;
: 
: }

It's not really erroneous in the sense that it should do what you've
asked without core-dumping.  But you're not asking it for what you want:
the =~ should be an =.  But using =~ on a list should merely do the pattern
match on the last element, $line in this case.  The real problem is that
$start and $end are undefined--a program consisting of just the @foo line
will core-dump too, because of a null-pointer dereference (fixed in 4.0).

Thanks.

Larry

piet@cs.ruu.nl (Piet van Oostrum) (02/15/91)

>>>>> In message <MARVIT.91Feb13190935@hplpm.hpl.hp.com>, marvit@hplpm.hpl.hp.com (Peter Marvit) (PM) writes:

PM> My own thanks to the fellow who needed to UPPERCASE certain key words in
PM> a file.  It's a problem I just needed a solution for.  However...

PM> I tried doing a s|\w.|tr[a-z][A-Z]| and some variants, but nothing worked
PM> correctly.  I plead to the perl community for help.

I thought the following might be a good try:

$_ = "this is a test case OF UPPERCASE\n";

s|\b(.)|$1=~tr/a-z/A-Z/|e;

print $_

However, perl dumps core (segmentation fault) on this. Looks like a bug to
me (or otherwise I should get an errormessage)
-- 
Piet* van Oostrum, Dept of Computer Science, Utrecht University,
Padualaan 14, P.O. Box 80.089, 3508 TB Utrecht, The Netherlands.
Telephone: +31 30 531806   Uucp:   uunet!mcsun!ruuinf!piet
Telefax:   +31 30 513791   Internet:  piet@cs.ruu.nl   (*`Pete')

tchrist@convex.COM (Tom Christiansen) (02/15/91)

From the keyboard of piet@cs.ruu.nl (Piet van Oostrum):
:>>>>> In message <MARVIT.91Feb13190935@hplpm.hpl.hp.com>, marvit@hplpm.hpl.hp.com (Peter Marvit) (PM) writes:
:
:PM> My own thanks to the fellow who needed to UPPERCASE certain key words in
:PM> a file.  It's a problem I just needed a solution for.  However...
:
:PM> I tried doing a s|\w.|tr[a-z][A-Z]| and some variants, but nothing worked
:PM> correctly.  I plead to the perl community for help.
:
:I thought the following might be a good try:
:
:$_ = "this is a test case OF UPPERCASE\n";
:
:s|\b(.)|$1=~tr/a-z/A-Z/|e;
:
:print $_
:
:However, perl dumps core (segmentation fault) on this. Looks like a bug to
:me (or otherwise I should get an errormessage)

Yes, you can't do that (right now).  The $N variables aren't really 
references back into the string, although I've talked a bit to Larry 
about it.  What you have there wouldn't work anyway, as tr/// returns 
the number of substitutions.  This works:

    s/\b(.)/($x = $1) =~ tr|a-z|A-Z|, $x/ge;

although I'd really rather be able to do this:

    s/\b(.)/\u$1/g;

or more precisely:

    s/\b([a-z])/\u$1/g;

But perl doesn't support \l, \u, \L, \U, and \E.  I think it would make
things like this a lot easier, and probably more efficient as well.

--tom
--
Tom Christiansen		tchrist@convex.com	convex!tchrist
 "All things are possible, but not all expedient."  (in life, UNIX, and perl)

chip@tct.uucp (Chip Salzenberg) (02/16/91)

According to marvit@hplpm.hpl.hp.com (Peter Marvit):
>I tried doing a s|\w.|tr[a-z][A-Z]| and some variants, but nothing worked
>correctly.  I plead to the perl community for help.

This worked for me:

    s/\b([a-z])/$x=$1,$x=~tr#a-z#A-Z#,$x/ge

-- 
Chip Salzenberg at Teltronics/TCT     <chip@tct.uucp>, <uunet!pdn!tct!chip>
 "I want to mention that my opinions whether real or not are MY opinions."
             -- the inevitable William "Billy" Steinmetz

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (02/16/91)

In article <1991Feb15.153133.8849@convex.com> tchrist@convex.COM (Tom Christiansen) writes:
: But perl doesn't support \l, \u, \L, \U, and \E.  I think it would make
: things like this a lot easier, and probably more efficient as well.

But it will.  And yes, they'll be more efficient.  Mostly I'm putting them
in because people will expect them to be there.

Incidentally, I think Perl will do a better job than vi: you'll be able to
say \u\L$& to force both initial cap and subsequent lowercase.

Larry

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (02/19/91)

In article <27BC1A2A.642C@tct.uucp> chip@tct.uucp (Chip Salzenberg) writes:
: According to marvit@hplpm.hpl.hp.com (Peter Marvit):
: >I tried doing a s|\w.|tr[a-z][A-Z]| and some variants, but nothing worked
: >correctly.  I plead to the perl community for help.
: 
: This worked for me:
: 
:     s/\b([a-z])/$x=$1,$x=~tr#a-z#A-Z#,$x/ge

In 4.0 beta, you can say

	s/\b([a-z])/\u$1/g;

which vi users may recognize.  In fact, you could get away with

	s/\b./\u$&/g;

though that does twice as much work.

Larry

allbery@NCoast.ORG (Brandon S. Allbery KB8JRR) (02/21/91)

As quoted from <11457@jpl-devvax.JPL.NASA.GOV> by lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall):
+---------------
| But it will.  And yes, they'll be more efficient.  Mostly I'm putting them
| in because people will expect them to be there.
| 
| Incidentally, I think Perl will do a better job than vi: you'll be able to
| say \u\L$& to force both initial cap and subsequent lowercase.
+---------------

You know, by the time you get some with all this, the "Swiss Army CHainsaw" is
going to be more like a Swiss Army Tactical Nuke....  :-)

++Brandon
-- 
Me: Brandon S. Allbery			    VHF/UHF: KB8JRR on 220, 2m, 440
Internet: allbery@NCoast.ORG		    Packet: KB8JRR @ WA8BXN
America OnLine: KB8JRR			    AMPR: KB8JRR.AmPR.ORG [44.70.4.88]
uunet!usenet.ins.cwru.edu!ncoast!allbery    Delphi: ALLBERY

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (02/21/91)

In article <1991Feb21.002412.20045@NCoast.ORG> allbery@ncoast.ORG (Brandon S. Allbery KB8JRR) writes:
: You know, by the time you get some with all this, the "Swiss Army CHainsaw" is
: going to be more like a Swiss Army Tactical Nuke....  :-)

Tactical?  TACTICAL!?!?  Hey, buddy, we went from kilotons to megatons
several minutes ago.  We don't need no stinkin' tactical nukes.

(By the way, do you have change for 10 million people?)

And how do you know how long it will be till I get some, anyway?

:-)

Larry

allbery@NCoast.ORG (Brandon S. Allbery KB8JRR) (02/26/91)

As quoted from <11527@jpl-devvax.JPL.NASA.GOV> by lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall):
+---------------
| In article <1991Feb21.002412.20045@NCoast.ORG> allbery@ncoast.ORG (Brandon S. Allbery KB8JRR) writes:
| : You know, by the time you get some with all this, the "Swiss Army CHainsaw" is
| 
| And how do you know how long it will be till I get some, anyway?
+---------------

Yeesh.  I put my fingers on autopilot and stick my foot in my mouth.  :-)

++Brandon
-- 
Me: Brandon S. Allbery			    VHF/UHF: KB8JRR on 220, 2m, 440
Internet: allbery@NCoast.ORG		    Packet: KB8JRR @ WA8BXN
America OnLine: KB8JRR			    AMPR: KB8JRR.AmPR.ORG [44.70.4.88]
uunet!usenet.ins.cwru.edu!ncoast!allbery    Delphi: ALLBERY

lwall@jpl-devvax.jpl.nasa.gov (Larry Wall) (04/19/91)

In article <27C563F0.52B@tct.uucp> chip@tct.uucp (Chip Salzenberg) writes:
: According to allbery@ncoast.ORG (Brandon S. Allbery KB8JRR):
: >You know, by the time you get some with all this, the "Swiss Army CHainsaw" is
: >going to be more like a Swiss Army Tactical Nuke....  :-)
: 
: What's with the future tense?

Nukes always make your future tense.

[Ouch.  Sorry.]

Larry