perl variables

https://w‮figi.ww‬tidea.com

In Perl, variables are used to store values of different data types. Here are some key points to know about variables in Perl:

  1. Variable names: Perl variable names start with a sigil ($, @, or %) followed by a name. Variable names can contain letters, digits, and underscores, but they cannot start with a digit.

  2. Scalar variables: Scalar variables in Perl start with the $ sigil and can hold a single value, such as a number or a string. Here's an example of assigning values to scalar variables:

my $name = "Alice"; # a string scalar
my $age = 30; # a numeric scalar
  1. Array variables: Array variables in Perl start with the @ sigil and can hold an ordered list of scalar values. Here's an example of defining and accessing array variables:
my @fruits = ("apple", "banana", "orange"); # an array of string scalars
print $fruits[0]; # prints "apple"
  1. Hash variables: Hash variables in Perl start with the % sigil and can hold an unordered collection of key-value pairs. Here's an example of defining and accessing hash variables:
my %person = (
  "name" => "Alice",
  "age" => 30,
  "occupation" => "programmer"
); # a hash of string and numeric scalars
print $person{"name"}; # prints "Alice"
  1. Global and local variables: Variables in Perl can be declared as global or local using the our and my keywords, respectively. Global variables can be accessed from anywhere in the program, while local variables are only accessible within the block of code where they are declared. Here's an example:
our $global_var = 42; # a global scalar variable
{
  my $local_var = "Hello, World!"; # a local scalar variable
  print $local_var; # prints "Hello, World!"
}
print $global_var; # prints 42

These are just a few examples of the different types of variables in Perl. Perl also has support for references, which allow you to refer to other variables or data structures.