This was a few days ago, but I just noticed Tim Bunce's comment about
the way other languages do it and thought of the way it is in another
language I know (one that a lot of people don't know), so I'm chiming
in briefly...
  
Austin Hastings <[EMAIL PROTECTED]> writes:
> How about a pre- or user- defined function that just does sprintf? 
> 
> "The values are $( sprintflike($format-string, @values))"

Inform has something like this (though printing is overall very
different than in Perl).  There are some functions defined by the
standard library, but any function can be used.  It works like this
(in Inform):

  ! Print stuff according to the usual rules:
  print foo, bar, " some constant string", baz;
  
  ! Print an object using its short name (which may be a
  ! routine, in which case it is run and is expected to print
  ! the appropriate thing, or a string):
  print (name) foo;
  
  ! Print an object, but use the article in addition to the
  ! short name.  (The object can override the article, and
  ! objects with the proper attribute don't use one, so this
  ! is not the same as expressly printing "the"):
  print (the) foo;
  
  ! Same thing, but capitalise the article:
  print (The) foo;

  ! There are some other predefined formatting routines, but here
  ! is the general case...
  
  ! Pass the object to the quux routine, which is expected to print it
  ! in some fashion:
  print (quux) foo;

The parentheses are not the right syntax for Perl, obviously.

This does come in really handy when interpolating objects into
sentences...

Object thief "Thief" somewhere
  with react_before [;
         Insert:
           move noun to self;
           "You attempt to put ", (the) noun, " into " (the) second,
           ", but ", (the) self, " snatches it away.";
           ! That looks better with inform-mode syntax highlighting.
         OtherAction:
           do_stuff();
       ],
       other_properties values, ! Elided here for brevity.
  has  proper animate;

I suspect an analagous feature would be really convenient in Perl.
If you can make the mental transition from the way Inform does things,
using the object's properties (which may be strings, routines,
whatever, depending on the object) to format it is not really very
different from using a format string to sprintf it.  Either way, some
piece of metainformation (which property to use, or the format
string), when applied to a specific item, produces results that look a
certain way.

Hmmm...  waitasec, now that I think about the above, we actually have
it, pretty much, even in Perl 5...

  $noun->{parent}=self; UpdateObjectTree(noun);
  print "You attempt to put ", the(noun), " into " the(second),
        ", but ", the(self), " snatches it away.\n";
        return 1;

Okay, the syntax is ugly, but isn't the Perl6 core going to be
flexible enough to allow syntactic sugar to be built on top?  So,
can't this be done outside of core?  In particular, isn't it going to
be easier in Perl6 to jump into and out of strings, so that the
combination of double quotes and commas can be reduced to a character?
ISTR something like that in one of the Apocalypse articles.  Or was
that for regexes?  I get strings and regexes confused...

Oh, and of course in Perl the routines would have to return the
strings rather than printing them, but that's definitely the Perlish
way to do it.

Reply via email to