home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!charon.amdahl.com!pacbell.com!sgiblab!zaphod.mps.ohio-state.edu!usc!news!netlabs!lwall
- From: lwall@netlabs.com (Larry Wall)
- Newsgroups: comp.lang.perl
- Subject: Re: Help with quotes
- Message-ID: <1992Nov6.190530.20726@netlabs.com>
- Date: 6 Nov 92 19:05:30 GMT
- References: <FILO.92Nov4211406@pegasus.Stanford.EDU>
- Sender: news@netlabs.com
- Distribution: comp
- Organization: NetLabs, Inc.
- Lines: 41
- Nntp-Posting-Host: scalpel.netlabs.com
-
- In article <FILO.92Nov4211406@pegasus.Stanford.EDU> filo@pegasus.stanford.edu writes:
- : I'm having a bit of a problem with quoting.
- :
- : This works as expected. It replaces the first character with a double-
- : quote.
- :
- : $ perl -pe 's/./sprintf ("\\"")/e; print "<$@>" if $@'
- : help
- : "elp
- : $
- :
- : Below is the same thing except $& has been added to the sprintf. In
- : this case the evaluation of the replacement string fails. I have not
- : been able to find a way to properly escape the double-quote to be
- : printed.
- :
- : $ perl -pe 's/./sprintf ("$&\\"")/e; print "<$@>" if $@'
- : help
- : <EOF in string at (eval) line 1, <> line 1.
- : >elp
- : $
- :
- : I know I could just changes quote characters, but I need a way to
- : include the quote character inside the quoted string.
-
- Admittedly it's probably a bug, but changing the inner quote characters
- is not your only option. You can also change the outer quote characters,
- like this:
-
- perl -pe 's/./sprintf(qq!$&"!)/e; print "<$@>" if $@'
-
- The whole idea of q and qq is to get away from the necessity of
- counting backslashes or parens. (One of the benefits of having More
- Than One Way To Do It is that if one of the ways doesn't work out, you
- can do it a different way, no?)
-
- By the way, why are you using sprintf? The only action of sprintf in
- this example would be to swallow the replacement if $& happened to
- evaluate to %. Doesn't seem too useful...
-
- Larry
-