Skip to content

Initialized locals are first set to undef, breaking tied vars which can't store undef #19293

@jimav

Description

@jimav

If a tied scalar is localized with an initialization value, STORE is first called with undef, and then again with the initialization value. This breaks if the tie implementation is unable to store an undefined value.

Legalistically speaking, 'man perlsub' implies that this should not happen where says "If no initializer is given for a particular [localized] variable, it is created with an undefined value" (i.e. it should not be created with undef if an initializer is given).

Anyway, it would be great if either

  1. Undef is not first stored when an initializer is present, OR
  2. A documented way is provided for a STORE implementation to know that it is okay to ignore the call because another STORE with the initializer is coming.
#!/usr/bin/perl
use strict; use warnings;

package Tiepkg;
sub TIESCALAR { my ($class) = @_; bless {}, $class }
sub FETCH     { print "**FETCH\n"; 42 }
sub STORE     { print "**STORE ", ($_[1]//"undef"), "\n";
                print "!! I can't do that !!\n" unless defined($_[1]);
              }

package main;
our $tvar; tie $tvar, 'Tiepkg';

{
  print "--Before localizing\n";
  local $tvar = 100;
  print "--After localizing\n";
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions