[Seattle-SAGE] Need perl help - regex issue

Ski Kacoroski kacoroski at comcast.net
Mon Oct 24 13:39:54 PDT 2005


Hi,

I have a problem with regexes.  I am using perl v5.8.5 on linux.  From a 
perl debug session:

I create a subroutine that drops everything after the space in the 
variable $f:
   DB<18> sub test { $f = "first_in_sub"; $f =~ /(.*)\s.*$/; print $1;}

I then create some variables..
   DB<19> $name = "first last"
   DB<20> $fn = "first"

I check $fn and as expected nothing is printed:
   DB<21> $fn =~ /(.*)\s.*$/; print $1;

I run test() and as expected nothing is printed:
   DB<22> test()

I check with the $name variable and as expected, the first part of the 
name is printed:
   DB<23> $name =~ /(.*)\s.*$/; print $1;
first

I then run the same command, but run test() right after it and for some 
reason, $1 in the subroutine has the match from the $name in the calling 
program.
   DB<24> $name =~ /(.*)\s.*$/; print $1;test()
firstfirst

I run test() again without the calling program doing a match and nothing 
is returned like expected.
   DB<25> test()

I create a new sub that will match in the subroutine:
   DB<26> sub test1 { $f = "first_in_sub last_in_sub"; $f =~ 
/(.*)\s.*$/; print $1;}

It matches as expected:
   DB<27> test1
first_in_sub

I can run it with the match in the calling routine ok:
   DB<28> $name =~ /(.*)\s.*$/; print $1;test1() 

firstfirst_in_sub

I check the test subroutine again:
   DB<29> test()

And it fails again when there is a match in the calling routine.
   DB<30> $name =~ /(.*)\s.*$/; print $1;test()
firstfirst


I really need to have matches in subroutines work correctly all the time 
(e.g. return nothing when there is no match).  Appreciate any ideas you 
have for resolving this.

cheers,

ski


-- 
"When we try to pick out anything by itself, we find it
  connected to the entire universe"            John Muir

Chris "Ski" Kacoroski, kacoroski at comcast.net, 206-501-9803



More information about the Members mailing list