Topic variable: Difference between revisions
m (→{{header|Standard ML}}: Mark properly) |
m (Use common English terms.) |
||
Line 2: | Line 2: | ||
Several programming languages offer syntax shortcuts to deal with the notion of "current" or "topic" variable. A topic variable is basically nothing but some kind of a variable with a very short name which can also often be omitted. |
Several programming languages offer syntax shortcuts to deal with the notion of "current" or "topic" variable. A topic variable is basically nothing but some kind of a variable with a very short name which can also often be omitted. |
||
If your language has something similar, show how it can be used by affecting <math>\frac{\pi}{3}</math> to the topic variable, compute its |
If your language has something similar, show how it can be used by affecting <math>\frac{\pi}{3}</math> to the topic variable, compute its cosin, sin, and display its value up to 4 decimal digits. |
||
=={{header|Perl}}== |
=={{header|Perl}}== |
Revision as of 22:50, 24 January 2013
Several programming languages offer syntax shortcuts to deal with the notion of "current" or "topic" variable. A topic variable is basically nothing but some kind of a variable with a very short name which can also often be omitted.
If your language has something similar, show how it can be used by affecting to the topic variable, compute its cosin, sin, and display its value up to 4 decimal digits.
Perl
In Perl the topic variable is $_. It is the default argument for many functions, including trigonometric ones. It is also the default parameter for loops. The topic parameter is lexical, so its use can be nested into several lexical scopes.
<lang Perl>my $_ = 3.141592 / 3; print for cos, sin, sprintf "%.4f", $_;</lang>
Perl 6
As in previous versions of Perl, in Perl6 the topic variable is $_. In addition to a direct affectation, it can also be set with the 'given' keyword. A method can be called from it with an implicit call:
<lang Perl 6>given pi / 3 {
say .cos; say .sin; say .fmt: "%.4f";
}</lang>
Standard ML
If an SML expression is evaluated interactively (that is, the fragment of program provided interactively is an expression and not a variable initialization), the value of the expression is used to initialize a variable, it. This is a feature of the interactive shell only.