Aliasing (computing) - Specified Aliasing

Specified Aliasing

Controlled aliasing behaviour may be desirable in some cases (that is, aliasing behaviour that is specified, unlike that relevant to memory layout in C). It is common practice in Fortran. The Perl programming language specifies, in some constructs, aliasing behaviour, such as in foreach loops. This allows certain data structures to be modified directly with less code. For example,

my @array = (1, 2, 3); foreach my $element (@array) { # Increment $element, thus automatically # modifying @array, since $element is ''aliased'' # to each of @array's elements in turn. $element++; } print "@array \n";

will print out "2 3 4" as a result. If one wanted to bypass aliasing effects, one could copy the contents of the index variable into another and change the copy.

Read more about this topic:  Aliasing (computing)