develooper Front page | perl.perl5.porters | Postings from December 2014

All I want for Christmas is: Control over SV stringification

Thread Next
Paul "LeoNerd" Evans
December 25, 2014 15:04
All I want for Christmas is: Control over SV stringification
Message ID:
The "stringification" CPAN module provides a lexically-scoped pragma
that disables stringification of references.

  no stringification;

  say "My array is " . [];

this will die with

  Attempted to concat a reference at ...

This has already been quite useful in finding all sorts of accidental
bare-ref stringification bugs in various code. It intentionally avoids
a complaint when refs are used as hash keys, allowing well-behaved
objects to still act nicely as hash keys. Being a lexical pragma you
can of course still re-enable it if you require it in some scope.

However, the module is still marked as a DEVEL-only module because the
implementation of it is terrible. I couldn't find a nice way to hook
into ref stringification itself, so instead I had to override every
"pp_*" function that might stringify a reference and wrap it in a small
checking wrapper that throws an exception if it doesn't like any of the
arguments, or else just passes it back down to the original op code. In
fact the module doesn't even hook all of the ops this could happen in,
only a few of the more common easy ones. Other ones are much harder -
e.g. for pack or sprintf, you'd have to walk the format string to even
work out which positional arguments are going to be used as strings.

While this is a horrible implementation, it has at least served its
purpose to demonstrate how useful this ability can be to find bugs in
code. It would be nice though if core provided some nicer hook to let
me do this.


In summary:

  I'd like a way to hook into Perl's stringification of non-overloaded
  references, to provide my own custom behaviour; at least to the
  ability of being able to veto it with an exception, or print a
  warning and continue.

Paul "LeoNerd" Evans  |

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About