You want to weaken $foo, $bar, and $baz; then you want to keep $foo as a weak reference while making $bar and $baz both a strong, normal reference again. That's the way I read it, so here's one way of doing it:
#!/usr/bin/perl
use strict;
use warnings;
use Scalar::Util qw(weaken isweak);
use Scalar::Util::Refcount;
my $foo;
my $bar;
my $baz;
my $ref1 = \$foo;
weaken($ref1);
print my $weak = isweak($ref1), "\n";
print refcount($foo), "\n";
my $ref2 = \$bar;
weaken($ref2);
print $weak = isweak($ref2), "\n";
print refcount($baz), "\n";
my $ref3 = \$baz;
weaken($ref3);
print $weak = isweak($ref3), "\n";
print refcount($baz), "\n\n";
my $copy1 = $ref2;
$weak = isweak($copy1);
my $copy2 = $ref3;
$weak = isweak($copy2), "\n";
I used $copy to make $bar and $baz strong again; "isweak" from
Scalar::Util to test if the refs were weak. If they're weak, they'll come back true. If they're not weak, they'll come back false; also, I checked the refcount with
Scalar::Util::Refcount
to double-check what was happening.
Update: Fixed some omissions.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.