官术网_书友最值得收藏!

  • Perl 6 Deep Dive
  • Andrew Shitov
  • 215字
  • 2021-07-03 00:05:47

Declaring variables

Every variable must be declared before its use in the program. You don't need to declare all the variables at the beginning of the program. From a practical perspective, the point of declaration can (and should) be as close as possible to the place where it is first used. The most practical reason to do that is to make the visibility of the variable better—if you declare too early, you force the reader of your program to think about the purpose of the variable; on the other hand, if you make changes in the code, there is a big chance of forgetting to remove the variable declaration if it is not located close to the place it is used.

To declare a variable, use the my keyword, as shown here:

my $x;

It is possible to declare a variable together with initialization:

my $x = 42;

Perl 6 also defines the concept of constants. These are variables whose value can only  be set once in the initializer. To create a constant, use the constant keyword, as shown here:

constant $C = 10;

It is not possible to assign a new value to the constant.

Now, let's see what kind of variables are available in Perl 6.

主站蜘蛛池模板: 土默特左旗| 玉门市| 怀来县| 济源市| 通榆县| 桦川县| 曲靖市| 稻城县| 淮阳县| 皋兰县| 古田县| 观塘区| 闽清县| 甘肃省| 天祝| 上饶县| 沙河市| 龙口市| 富蕴县| 诏安县| 南充市| 大安市| 安陆市| 淳化县| 准格尔旗| 清流县| 崇明县| 渑池县| 织金县| 阿图什市| 师宗县| 永顺县| 罗平县| 恩施市| 墨竹工卡县| 陇南市| 台南县| 巴里| 伊宁市| 丽江市| 曲靖市|