Can you reliably make a reference to a capture buffer?

C

Clint Olsen

I tried to return a reference to a capture buffer ($1), and it looks like
the contents are destroyed out from under me. Is there something special
about a capture buffer that makes this impossible?

#!/usr/bin/perl -w

use strict;
use warnings;

sub foo {
my $foo = "barbell";
if ($foo =~ /(bar)/) {
print "$1\n";
\$1;
}
}

print "output of foo is ", ${&foo}, "\n";

results in:

bar
Use of uninitialized value in print at /tmp/reftest line 14.
output of foo is


Thanks,

-Clint
 
J

Jeff 'japhy' Pinyan

I tried to return a reference to a capture buffer ($1), and it looks like
the contents are destroyed out from under me. Is there something special
about a capture buffer that makes this impossible?

The $<DIGIT> variables are automagically dynamically scoped (like local()
variables). However, when you leave the scope in which they were created,
they revert to their previous value. This is because $1 and its ilk are
merely conduits to get to values, they don't actually hold a value
themselves. They are, in Perl source terms, "magic" variables.

Thus, taking a reference to the conduit, not the value.

"foo" =~ /(.)/;
{
"bar" =~ /(.)/;
$x = \$1;
print "$x, $$x\n"; # SCALAR(0x814ea14), b
}
$y = \$1;
print "$x, $$x\n"; # SCALAR(0x814ea14), f
print "$y, $$y\n"; # SCALAR(0x814ea14), f
 
A

Anno Siegel

Jeff 'japhy' Pinyan said:
The $<DIGIT> variables are automagically dynamically scoped (like local()
variables).

Yet you go on to show that the $<DIGIT> variables don't behave like localized
variables at all. Your explanation below is fine (as far as I can tell)
but it's unwise to liken the behavior of $<DIGIT> to that of a localized
package variable. The analogy breaks down soon. A reference to a localized
scalar is to the current value, and will not change when the value goes
out of scope. A ref to $1 changes its content all the time, not only
on matches, but also when the block is left where the match occurred.

Anno
However, when you leave the scope in which they were created,
they revert to their previous value. This is because $1 and its ilk are
merely conduits to get to values, they don't actually hold a value
themselves. They are, in Perl source terms, "magic" variables.

print "Localized package variable:\n";
our $x = 123;
my $ref1 = \ $x;
my $ref2;
{
local $x = 456;
$ref2 = \ $x;
print "inside: $_ -> $$_\n" for $ref1, $ref2;
}
print "outside: $_ -> $$_\n" for $ref1, $ref2;
print "\n";

print "Capture variable:\n";
'123' =~ /(...)/;
$ref1 = \ $1;
{
'456' =~ /(...)/;
$ref2 = \ $1;
print "inside: $_ -> $$_\n" for $ref1, $ref2;
}
print "outside: $_ -> $$_\n" for $ref1, $ref2;

__END__
Localized package variable:
inside: SCALAR(0xff778) -> 123
inside: SCALAR(0xf5578) -> 456
outside: SCALAR(0xff778) -> 123
outside: SCALAR(0xf5578) -> 456

Capture variable:
inside: SCALAR(0x126324) -> 456
inside: SCALAR(0x126324) -> 456
outside: SCALAR(0x126324) -> 123
outside: SCALAR(0x126324) -> 123
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top