Note that if you take a copy of a scalar with a weakened reference, the copy will be a strong reference.
my $var;
my $foo = \$var;
weaken($foo); # Make $foo a weak reference
my $bar = $foo; # $bar is now a strong reference
####
/usr/bin/perl
use strict;
use Scalar::Util qw/weaken isweak/;
my $foo = {};
my $bar = returnweak();
print "returnweak isweak - ".(isweak($bar)? "yes":"no")." - '$bar'\n";
weakenbyref(\$bar);
print "weakenbyref isweak - ".(isweak($bar)? "yes":"no")." - '$bar'\n";
sub returnweak {
my $ret = $foo;
weaken($ret);
return $ret;
}
sub weakenbyref {
my $arg = shift;
weaken($$arg = $foo);
return;
}
####
returnweak isweak - no - 'HASH(0x606df0)'
weakenbyref isweak - yes - 'HASH(0x606df0)'