home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / perl / 5690 < prev    next >
Encoding:
Text File  |  1992-09-02  |  1.5 KB  |  59 lines

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!panther!mothost!merlin.dev.cdx.mot.com!fendahl.dev.cdx.mot.com!mcook
  3. From: mcook@fendahl.dev.cdx.mot.com (Michael Cook)
  4. Subject: Re: Why doesn't this work?
  5. Message-ID: <mcook.715476247@fendahl.dev.cdx.mot.com>
  6. Sender: news@merlin.dev.cdx.mot.com (USENET News System)
  7. Nntp-Posting-Host: fendahl.dev.cdx.mot.com
  8. Organization: Motorola Codex, Canton, Massachusetts
  9. References: <1992Sep02.191014.28892@CS.ORST.EDU>
  10. Distribution: usa
  11. Date: Wed, 2 Sep 1992 23:24:07 GMT
  12. Lines: 45
  13.  
  14. @prism.cs.orst.edu writes:
  15.  
  16. >Why doesn't this work?
  17.  
  18. >   while ( ($eval) = ($path =~ /(\`.*\`)/) ) {
  19. >      $val = "";
  20. >      if ( $eval ) {
  21. >         $val = eval( $eval );
  22. >         chop $val;
  23. >      }
  24. >      $path =~ s/$eval/$val/;
  25. >   }
  26.  
  27. [...]
  28.  
  29. >  DB<5> p $eval
  30. >`echo David D. Perkins--071160 | cut -f1 -d-`
  31.  
  32. In s/$eval/$val/, the value of $eval is used as a regular expression.  So,
  33. you're trying to match either "`echo David D. Perkins--071160 " or " cut -f1
  34. -d-`".
  35.  
  36. You should be able to get away with this:
  37.  
  38.       $path =~ s//$val/;
  39.  
  40. (From the manual page: "If the PATTERN evaluates to a null string, the most
  41. recent successful regular expression is used instead.")
  42.  
  43. Otherwise, you could do this:
  44.  
  45.       $eval =~ s/\W/\\$&/g;    # escape any special RE characters.
  46.       $path =~ s/$eval/$val/;
  47.  
  48. Or this:
  49.  
  50.       substr($path, index($path, $eval), length($eval)) = $val;
  51.  
  52. Or use the /e modifier, and replace the whole loop with this:
  53.  
  54.       $path =~ s/`[^`]*`/chop($val = eval($&)); $val/eg;
  55.  
  56. (You'll probably want to use /`[^`]*`/ instead of /`.*`/.)
  57.  
  58. Michael.
  59.