Reputation: 5749
In per5, I can just use a variable, such as $foo or @bar without using "my".
$foo=1; @bar=(1,2);
In perl6, why do I have to use "my" all the time? Otherwise compiler will say variable undeclared.Why can't perl6 just autovivify?
print "{my @a=1,2,3;}\n"; # have to use "my" to declare variable
print "{@a=1,2,3;}\n"; # this is error 'Variable '@a' is not declared'
I don't like the restriction of having to always use "my". This is too low level like C; very cumbersome.
Is there a way to turn on always autovivify?
Thanks.
Upvotes: 3
Views: 113
Reputation:
no strict;
$foo=1; @bar=(1,2);
print "{@a=1,2,3;}\n";
# OUTPUT«1 2 3»
Perl 6 tries to help you with proper error messages. If you declare all variables it will provide you with a guess what variable you meant if you have a typo. Also there are new language features like constants and sigilless variables that are not possible to write down without declarators.
Upvotes: 4
Reputation: 169623
Not having explicit variable declarations is a terrible idea from the perspective of language design for various reasons. Arguably, explicitly declared block-scoped lexical variables are the way to go, and I find it crazy how many languages of the 'scripting' variety get this 'wrong' (there's a reason why let
got added to Javascript...)
That said, Perl6 supports the no strict
pragma which allows you to omit the declaration.
Upvotes: 10