The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.


DynScalar - closure-in-a-box for simple scalars


  use DynScalar;  # imports as dynamic()
  use strict;
  use vars '$name';
  my $foo = dynamic { "Hello, $name!\n" };
  for $name ("Jeff", "Joe", "Jonas") { print $foo }


This module creates closures, and masks them as objects that stringify themselves when used. This allows you to make incredibly simplistic string templates:

  use DynScalar 'delay';  # import as delay()
  use strict;
  use vars qw( $name $age $sex );
  my $template = delay {
    "Hello, $name.  You're a good-looking $age-year-old $sex.\n"
  while (my $rec = get_person()) {
    ($name,$age,$sex) = $rec->features;
    print $template;

You can embed arbitrarily complex code in the block.


Lexically scoped variables can be used inside the block, but you must do so with caution. The variable must be visible, as in this example:

  use DynScalar;
  my $name;
  my $str = dynamic { $name };
  for ("Jeff", "Joe", "Jonas") { $name = $_; print $str }

If you use the lexically scoped variable as the iterator variable in the loop, however, Perl will scope it even further, and the DynScalar object will not be able to see it:

  use DynScalar;
  my $name;
  my $str = dynamic { $name };
  # this next line will not print as you hoped
  for $name ("Jeff", "Joe", "Jonas") { print $str }


  Jeff "japhy" Pinyan