הלינקייה: מגזין חודשי למפתחים

רוצה לשמוע על כל האירועים, המדריכים, הקורסים והמאמרים שנכתבו החודש ?
הלינקייה הינו מגזין חופשי בעברית שמשאיר אותך בעניינים.
בלי ספאם. בלי שטויות. פעם בחודש אצלך בתיבה.

Data Types

Perl provides us with three different data types. The scalar, the list and the hash.
A scalar is used to store a single value, be it a number, a string or a reference.
A list is used to store multiple scalar values in an orderly fashion.
A hash is used to store multiple scalar values in a named dictionary, each value has a "key" through which we can directly access the value.

Let's explore these in some examples:

use Modern::Perl;
 
# basic list is defined using brackets
my @l = (1,2,3);
 
say $l[2];
 
say @l;
say "@l";
 
my @pizza_vec = (6, 7, 4, 4);
my $num_of_pizza_slices = $pizza_vec[0];
$num_of_pizza_slices = $pizza_vec[6];
 
my $index = 3;
$num_of_pizza_slices = $pizza_vec[$index];
 
$pizza_vec[4] = 3;
$index = 6;
$pizza_vec[$index] = 2;
$pizza_vec[2] = $pizza_vec[0];
$pizza_vec[20] = 7;
$pizza_vec[-1] = 5;
 
$pizza_vec[5] = "I Love Perl";
use Modern::Perl;
 
my @pizza_vec = (1, 2, 3, 4);
my @copy_pizza_vec = @pizza_vec;
 
@copy_pizza_vec = (0, $pizza_vec[2]); 
 
@pizza_vec = (0, @pizza_vec);
@copy_pizza_vec = (1);
 
$array_size = @pizza_vec;
use Modern::Perl;
 
#!/usr/bin/perl 
 
my @rainbow = ("red", "green", "blue", "yellow", "orange", "violet", "indigo");
 
# extract slice "blue", "yellow" 
my @slice = @rainbow[2,3]; 
 
# extract slice "blue", "violet", "red", "indigo" 
@slice = @rainbow[2,5,0,6]; 
 
# extract slice "indigo", "red" 
@slice = @rainbow[-1,-7]; 
 
my @l = (1,2,3);
@l[2..5] = (1..10);
 
say "@l";
use Modern::Perl;
 
#A Hash automatically pops to existence when starting to use it
 
my $pizza_price{"plain"} = 6;
 
# Hash keys are bare-words. No need to quote here
$pizza_price{onion} = 7;
$pizza_price{olives} = 7;
$pizza_price{extra_cheese} = 7;
 
my $bus_price{61} = 4.80;
$bus_price{62} = 4.80;
$bus_price{641} = 12.10;
 
say "Bus line 641 costs ", $bus_price{641}," NIS";
use Modern::Perl;
 
# Iterating over a hash using each
my %h = ( a => 10, b => 20, c => 30 );
 
while ( my ($k, $v) = each %h) {
  say "$k => $v";
}
 
# Printing a hash with Data::Dumper
use Data::Dumper;
 
print Dumper(\%h);
use Modern::Perl;
use Data::Dumper;
 
 
my %h = (a => 10, b => 20, c => 30, d => 40);
 
# Use reverse
my %r1 = reverse %h;
 
# Iterate over the hash
my %r2;
while (my ($k, $v) = each %h) {
	$r2{$v} = $k;
}
 
# Use map
 
my %r3 = map { ($h{$_}, $_) } keys %h;
 
print "Original hash: ", Dumper(\%h);
print "Rev1: ", Dumper(\%r1);
print "Rev2: ", Dumper(\%r2);
print "Rev3: ", Dumper(\%r3);

Working With Contexts

use strict;
use warnings;

my @words = qw(foo bar buz);

my $x   = @words;
my ($y) = @words;
my ($z) = scalar @words;

my $rev = reverse "Hello World";

# dlroW olleH at /Users/ynonperek/Courses/perl/context.pl line 12.
warn $rev;

# Hello World at /Users/ynonperek/Courses/perl/context.pl line 13.
warn reverse "Hello World";

# x = 3, y=foo, z=3 at /Users/ynonperek/Courses/perl/context.pl line 15.
warn "x = $x, y=$y, z=$z";

course: