Topic variable

From Rosetta Code
Revision as of 07:56, 24 January 2013 by Grondilu (talk | contribs) (Created page with "{{draft task}} Several programming languages offer syntax shortcuts to deal with the notion of "current" or "topic" variable. A topic variable is basically nothing but some k...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Topic variable is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

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 cosinus, sinus, 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>