Perl has three contexts: void, scalar and list.
func(); # void my $ret = func(); # scalar my ($ret) = func(); # list my @ret = func(); # list
An example of where context affects return values is in dealing with regular expressions:
my $str = 'Perl 101 Perl Context Demo'; my @ret = $str =~ /Perl/g; # @ret = ('Perl','Perl'); my $ret = $str =~ /Perl/g; # $ret is true
These are called the range operators, and they can help with code that deals with ranges of integers or characters.
In the previous example,
@array was filled by hand. But these are equivalent:
my @array = ( 0, 1, 2, 3, 4, 5 ); my @array = 0..5;
Oddly enough, it works with characters, too. If you want to get a list of letters from
z, you can do:
my @array = 'a'..'z';
When used in this way,
... are equivalent
The range operators only increment. This will produce a zero-size list:
my @array = 10..1; # @array is empty
If you want the reverse, ask for it.
my @array = reverse 1..10; # @array descends from 10 to 1
You can also use the range operator in a scalar context, but that is outside the scope of this presentation. Check the man page for more details.
Submit a PR to github.com/petdance/perl101
© 2007-2018 Andy Lester