CGI::App to Perl6: Another improvement for references

markjugg on 2006-08-31T23:25:41

Earlier I wrote about how references are gone in Perl6. However, subroutine signature Perl6 expose just opposite-- that really everything is a reference.

We use modify-by-reference in CGI::Application to prevent copying large HTML documents into the "postrun" routine.

This is a technique that should be used sparingly, as returning explicit values creates for clearer code flow.

Perl6 helps promotes good programming practices in two ways in this regard. 1. You have explicitly turn on modify-by-reference for a subroutine argument, and 2. This explicitness makes it clear that this trick is being employeed. Let's a take a look at the syntax:

  Was: sub foo {...};        foo(\$bar)
  Now: sub foo ($bar is rw); foo($bar)
Perl6 is /always/ passing references to subroutines in the background, they are just read-only by default. While in Perl5 you might have a dig around to see if modification by reference was happening in a subroutine, in Perl6 you can look no further than the signature.

If you want a copy of the value passed, Perl6 covers that too:

  sub foo ($bar is copy); 
Aspects of this may seem formal compared to the looseness of Perl5. So far I've generally liked these changes, as compactness and clarity often come with them.