home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / perl / 6873 < prev    next >
Encoding:
Internet Message Format  |  1992-11-08  |  1.8 KB

  1. Path: sparky!uunet!charon.amdahl.com!pacbell.com!sgiblab!zaphod.mps.ohio-state.edu!usc!news!netlabs!lwall
  2. From: lwall@netlabs.com (Larry Wall)
  3. Newsgroups: comp.lang.perl
  4. Subject: Re: Help with quotes
  5. Message-ID: <1992Nov6.190530.20726@netlabs.com>
  6. Date: 6 Nov 92 19:05:30 GMT
  7. References: <FILO.92Nov4211406@pegasus.Stanford.EDU>
  8. Sender: news@netlabs.com
  9. Distribution: comp
  10. Organization: NetLabs, Inc.
  11. Lines: 41
  12. Nntp-Posting-Host: scalpel.netlabs.com
  13.  
  14. In article <FILO.92Nov4211406@pegasus.Stanford.EDU> filo@pegasus.stanford.edu writes:
  15. : I'm having a bit of a problem with quoting.
  16. : This works as expected.  It replaces the first character with a double-
  17. : quote.
  18. : $ perl -pe 's/./sprintf ("\\"")/e; print "<$@>" if $@'
  19. : help
  20. : "elp
  21. : $
  22. : Below is the same thing except $& has been added to the sprintf.  In
  23. : this case the evaluation of the replacement string fails.  I have not
  24. : been able to find a way to properly escape the double-quote to be
  25. : printed.
  26. : $ perl -pe 's/./sprintf ("$&\\"")/e; print "<$@>" if $@'
  27. : help
  28. : <EOF in string at (eval) line 1, <> line 1.
  29. : >elp
  30. : $
  31. : I know I could just changes quote characters, but I need a way to
  32. : include the quote character inside the quoted string.
  33.  
  34. Admittedly it's probably a bug, but changing the inner quote characters
  35. is not your only option.  You can also change the outer quote characters,
  36. like this:
  37.  
  38.     perl -pe 's/./sprintf(qq!$&"!)/e; print "<$@>" if $@'
  39.  
  40. The whole idea of q and qq is to get away from the necessity of
  41. counting backslashes or parens.  (One of the benefits of having More
  42. Than One Way To Do It is that if one of the ways doesn't work out, you
  43. can do it a different way, no?)
  44.  
  45. By the way, why are you using sprintf?  The only action of sprintf in
  46. this example would be to swallow the replacement if $& happened to
  47. evaluate to %.  Doesn't seem too useful...
  48.  
  49. Larry
  50.