Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!wuarchive!mit-eddie!uw-beaver!ubc-cs!phillips From: phillips@cs.ubc.ca (George Phillips) Newsgroups: comp.lang.perl Subject: Re: Is this a Bug? Summary: yes Keywords: perl bug Message-ID: <9888@ubc-cs.UUCP> Date: 5 Oct 90 18:45:14 GMT References: <9134@cg-atla.UUCP> Sender: news@cs.ubc.ca Organization: University of British Columbia, Vancouver, B.C., Canada Lines: 32 In article <9134@cg-atla.UUCP> white@cg-atla.UUCP (Frank ) writes: > > I am using 'perl' PL18. Running the following script >causes "WORD WITH PARENS" to be printed. What's the difference? >----------------- cut here ------------------------------------------- >#!/usr/local/bin/perl > >$_ = "This line contains a\nword beginning a line"; >if ( /^word/ ) { > print "WORD NO PARENS\n"; >} elsif ( /^(word)/ ) { > print "WORD WITH PARENS\n"; # I get this message !! >} Yep, this is a bug. There's even a passage in the manual page which says, more or less, that ^ does not necessarily work as advertised. If you're using ^ in a regular expression and you're not sure if the string has a newline in it, you'd better do something like: if (/^regexp/ && $` eq "") { # yep, it really did do an anchored match So here is a fixed version of your script: $_ = "This line contains a\nword beginning a line"; if ( /^word/ ) { print "WORD NO PARENS\n"; } elsif ( /^(word)/ && $` eq "" ) { print "WORD WITH PARENS\n"; # I get this message !! } -- George Phillips phillips@cs.ubc.ca {alberta,uw-beaver,uunet}!ubc-cs!phillips