if \$a = 'some' . \$b, and \$b then changes, how to make \$a change too? See inside.

 on Jul 04, 2013 at 08:53 UTC Need Help??
Doctrin has asked for the wisdom of the Perl Monks concerning the following question:

Hello dear Monks ) Here's some code:
```my \$r = 300;
my \$g = 'g' . \$r;
print "\$g\n"; #would be 'g300', that's ok
\$r = 500;
print "\$g\n"; #would be 'g300', but wanted 'g500'
So, is there a way to make \$g change as \$r changes? Thanks in advance.
• Comment on if \$a = 'some' . \$b, and \$b then changes, how to make \$a change too? See inside.

Replies are listed 'Best First'.
Re: if \$a = 'some' . \$b, and \$b then changes, how to make \$a change too? See inside.
by kcott (Chancellor) on Jul 04, 2013 at 09:11 UTC

G'day Doctrin,

It sounds you want code like this which uses references:

```\$ perl -Mstrict -Mwarnings -E '
my \$r = 300;
my \$r_ref = \\$r;
my \$g = "g";
say \$g . \$\$r_ref;
\$r = 500;
say \$g . \$\$r_ref;
'
g300
g500

See perlreftut (a short tutorial) and perlref (the details).

-- Ken

Re: if \$a = 'some' . \$b, and \$b then changes, how to make \$a change too? sub
by Anonymous Monk on Jul 04, 2013 at 08:57 UTC

Don't do that :)(String::Defer) do

```print \$prefix, \$suffix, "\n";
Re: if \$a = 'some' . \$b, and \$b then changes, how to make \$a change too? See inside.
by hdb (Monsignor) on Jul 04, 2013 at 09:50 UTC

If this is part of a bigger question and you think some extra effort is worthwhile, then using closures is one way.

```use strict;
use warnings;

sub stringmaker {
my( \$start, \$stringref ) = @_;
return sub { \$start.\$\$stringref }
}

my \$r = 300;
my \$g = stringmaker( 'g', \\$r );
print \$g->()."\n"; #would be 'g300', that's ok
\$r = 500;
print \$g->()."\n"; #would be 'g500'

The disadvantage now is that \$g is not a string, but a code reference, and that you need a different stringmaker-routine for different types of strings. The latter can be mitigated somewhat by using sprintf and a template:

```use strict;
use warnings;

sub stringmaker {
my( \$template, @vals ) = @_;
return sub { sprintf \$template, map { \$\$_ } @vals }
}

my \$r = 300;
my \$i = 0;
my \$g = stringmaker( '%s: g%s', \\$i, \\$r );
print \$g->()."\n";  # '0: g300'
\$r = 500; \$i = 10;
print \$g->()."\n";  # '10: g500'
Re: if \$a = 'some' . \$b, and \$b then changes, how to make \$a change too? See inside.
by rjt (Deacon) on Jul 04, 2013 at 13:19 UTC

One of the existing packages Anonymous Monk recommended is probably the way to go for any production code. However, if this is a learning exercise, you might start with something like the following:

```#!/usr/bin/env perl

use 5.010;
use warnings;

my \$suf = 42;
my \$my_string = Sillystring->new('Prefix_', \\$suf);
say \$my_string;
\$suf = 31;
say \$my_string;

package Sillystring;
use Carp;

'""' => sub { my \$self = shift; \$self->{prefix} . \${\$self->{suffix
+}} };

sub new {
my (\$class, \$prefix, \$suffix) = @_;
croak "Suffix must be a SCALAR ref" unless ref(\$suffix) eq 'SCALAR
+';
bless {
prefix => \$prefix,
suffix => \$suffix,
}, \$class;
}

1;

This will do what you want, although it's pretty terrible OO design, and not all string operations are supported. Concatenation, for instance, will actually replace the object with an unblessed ordinary string. This could be avoided by overloading the concatenation operator.

If efficiency is a concern, realize that any such implementation is going to be far slower than ordinary scalars, but there's not much you can do about that except benchmark.

Re: if \$a = 'some' . \$b, and \$b then changes, how to make \$a change too? See inside.
by jwkrahn (Monsignor) on Jul 04, 2013 at 18:02 UTC
```\$ perl -e'
my \$g = "g300";
my \$r = \substr \$g, 1;
print "\$g\n";
\$\$r = 500;
print "\$g\n";
'
g300
g500

Create A New User
Node Status?
node history
Node Type: perlquestion [id://1042388]
Approved by marto
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2018-06-20 19:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Should cpanminus be part of the standard Perl release?

Results (117 votes). Check out past polls.

Notices?