Copy a string: Difference between revisions

Content deleted Content added
Underscore (talk | contribs)
Line 269:
 
=={{header|Perl}}==
 
my $src = "Hello";
To copy a string, just use ordinary assignment:
my $dst = $src;
 
<perl>my $srcoriginal = "'Hello".';
my $dstnew = $srcoriginal;
$new = 'Goodbye.';
print $original, "\n"; # prints "Hello."</perl>
 
To create a reference to an existing string, so that modifying the referent changes the original string, use a backslash:
 
<perl>my $ref = \$original;
$$ref = 'Goodbye.';
print $original, "\n"; # prints "Goodbye."</perl>
 
If you want a new name for the same string, so that you can modify it without dereferencing a reference, assign a reference to a typeglob:
 
<perl>our $alias;
local *alias = \$original;
$alias = 'Good evening.';
print $original, "\n"; # prints "Good evening."</perl>
 
Note that <code>our $alias</code>, though in most cases a no-op, is necessary under stricture. Beware that <code>local</code> binds dynamically, so any subroutines called in this scope will see (and possibly modify!) the value of <code>$alias</code> assigned here.
 
=={{header|PHP}}==