From: Michael K. <ki...@cs...> - 2004-10-05 01:46:24
|
> There was an unclear spot in the patch (the chop($whatever); in the middle > of that string), which I asked Mitchell to clarify. > > This perl code > $whatever = <<'hee'; chop($whatever); > bar > hee > ...other statements... > is equivalent to > $whatever = 'bar'."\n"; chop($whatever); ...other statements... I see - thanks a lot! I wasn't aware of the $whatever = <<END; command; ... END idiom. I thought that 'command' becomes part of the assigned string and was wondering what was going on ... --michael > The chop($whatever) chops off that excess trailing newline. > Chop is called _after_ the string assignment, and mutates the string. > The end marker (here "hee") has to occur on a line by itself, so we > add a trailing newline to the input string (here "bar") to make sure > this happens, and then strip it away again after the string is assigned > and before it is used. > > So the patch changes something like > "$__text__='%s'" > into something like > "$__text__= <<'ENDj9yq6QC43b'; chop($__text__);\n%s\nENDj9yq6QC43b\n" > > I called the variable "$whatever" in the patch summary because the > actual variable name varies among perlmatch functions ($__text__, > $__string__, and $string). > > From a language design standpoint, though here-documents look odd at > first, they permit use like > foo( arg1 , <<'END_ARG2', <<'END_ARG3' , arg4 ); > arg2... > END_ARG2 > arg3... > END_ARG3 > which can make code with large text blocks, or quoting issues, much > clearer. > > Sorry for the confusion. > > Mitchell Charity > > > And btw, just in case anyone out there has been using perlmatch's old > broken quoting to make it serve as a nice general perl eval(), note > one can still do so with a (slightly more complex) idiom like: > // Flora code > #perl_eval(Code,StrVal) :- > #join(['ENDj9yq6QC43b > $__ret__ = "".do{',Code,'}; > $__ignore__ = <<ENDj9yq6QC43b; > '],Src), > #perl_substitute(Src,'s/^/$__ret__/s',StrVal). > Fyi. It can be quite useful. > > > ------------------------------------------------------- > This SF.net email is sponsored by: IT Product Guide on ITManagersJournal > Use IT products in your business? Tell us what you think of them. Give us > Your Opinions, Get Free ThinkGeek Gift Certificates! Click to find out more > http://productguide.itmanagersjournal.com/guidepromo.tmpl > _______________________________________________ > Xsb-development mailing list > Xsb...@li... > https://lists.sourceforge.net/lists/listinfo/xsb-development > |