| home / programming / perl / professional /chap7 / 5 |
[previous] |
|
Assignable SubroutinesSome of Perl's built-in functions allow us to assign to them as well as use them in expressions. In programming parlance, the result of the function is an lvalue, or a value that can appear on the left hand side of an assignment. The most common and obvious lvalues are variables, which we assign to all the time:
Some Perl functions can also be assigned to in this way, for example the substr function:
The substr function returns part of a string. If the string happens to be held in a variable then this returned string segment is an lvalue, and can be assigned to. Perl does not even require that the new text be the same length, as the above example illustrates. It would be wonderful to be able to do this kind of thing in our own subroutines. In fact, Perl does allow us to this, albeit only experimentally at the moment. Assignable subroutines make use of subroutine attributes (an experimental feature of Perl in version 5.6). Since attributes are likely to evolve, or possibly even disappear entirely, this technique is not guaranteed to work and should be avoided for production code. However, for the moment, to make a subroutine assignable we can use the special attribute lvalue, as this simple assignable subroutine script demonstrates:
In order for an assignable subroutine to function correctly, it must return a variable. In addition, it must not use return, because with an assignable subroutine data can pass in as well as out. Currently (as of Perl 5.6) only scalar values may be used as assignable return values. This includes an array element or hash value, however. Future versions of Perl are expected to lift this restriction. Attributes do not preclude prototypes. If we want to specify a prototype, we can do so after the subroutine, before any attributes. The following example shows a prototyped assignable subroutine that provides an example of assigning to an array via the returned lvalue.
In itself this is not a particularly useful example, of course, but it may lead to some interesting possibilities. |
| home / programming / perl / professional /chap7 / 5 |
[previous] |
Created: April 18, 2001
Revised: April 18, 2001