home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.perl
- Path: sparky!uunet!panther!mothost!merlin.dev.cdx.mot.com!fendahl.dev.cdx.mot.com!mcook
- From: mcook@fendahl.dev.cdx.mot.com (Michael Cook)
- Subject: Re: Why doesn't this work?
- Message-ID: <mcook.715476247@fendahl.dev.cdx.mot.com>
- Sender: news@merlin.dev.cdx.mot.com (USENET News System)
- Nntp-Posting-Host: fendahl.dev.cdx.mot.com
- Organization: Motorola Codex, Canton, Massachusetts
- References: <1992Sep02.191014.28892@CS.ORST.EDU>
- Distribution: usa
- Date: Wed, 2 Sep 1992 23:24:07 GMT
- Lines: 45
-
- @prism.cs.orst.edu writes:
-
- >Why doesn't this work?
-
- > while ( ($eval) = ($path =~ /(\`.*\`)/) ) {
- > $val = "";
- > if ( $eval ) {
- > $val = eval( $eval );
- > chop $val;
- > }
- > $path =~ s/$eval/$val/;
- > }
-
- [...]
-
- > DB<5> p $eval
- >`echo David D. Perkins--071160 | cut -f1 -d-`
-
- In s/$eval/$val/, the value of $eval is used as a regular expression. So,
- you're trying to match either "`echo David D. Perkins--071160 " or " cut -f1
- -d-`".
-
- You should be able to get away with this:
-
- $path =~ s//$val/;
-
- (From the manual page: "If the PATTERN evaluates to a null string, the most
- recent successful regular expression is used instead.")
-
- Otherwise, you could do this:
-
- $eval =~ s/\W/\\$&/g; # escape any special RE characters.
- $path =~ s/$eval/$val/;
-
- Or this:
-
- substr($path, index($path, $eval), length($eval)) = $val;
-
- Or use the /e modifier, and replace the whole loop with this:
-
- $path =~ s/`[^`]*`/chop($val = eval($&)); $val/eg;
-
- (You'll probably want to use /`[^`]*`/ instead of /`.*`/.)
-
- Michael.
-