Title here
Summary here
In Perl, variables are declared implicitly when they are first used. Unlike Go, Perl doesn’t require explicit type declarations.
#!/usr/bin/perl
use strict;
use warnings;
# Variables in Perl are declared with a sigil ($, @, or %)
# followed by the variable name.
my $a = "initial";
print "$a\n";
# You can declare multiple variables at once.
my ($b, $c) = (1, 2);
print "$b $c\n";
# Perl automatically infers the type of variables.
my $d = 1; # This is a number
print "$d\n";
# Variables declared without initialization are undef.
my $e;
print defined $e ? $e : "undef", "\n";
# Perl doesn't have a specific shorthand for declaration and initialization.
# You always use 'my' for lexical variables.
my $f = "apple";
print "$f\n";
To run the program:
$ perl variables.pl
initial
1 2
1
undef
apple
In Perl:
my
keyword for lexical scope.$
for scalars, @
for arrays, and %
for hashes.undef
in Perl, which is similar to Go’s zero values.:=
. You always use my
for declaring variables.Perl’s dynamic typing and implicit declarations make it more flexible but less strict than Go in terms of variable handling.