Skip to content

Instantly share code, notes, and snippets.

@ynonp
Created August 29, 2012 07:48
Show Gist options
  • Save ynonp/3508129 to your computer and use it in GitHub Desktop.
Save ynonp/3508129 to your computer and use it in GitHub Desktop.
use v5.14;
my %couples = (
Yehuda => 'Ninet',
Yossi => 'Jager',
Bibi => 'Sarah',
Brad => 'Jeffiner',
);
$couples{Brad} = 'Angelina';
$couples{Turji} = 'Oshri';
while ( my ($m, $f) = each %couples ) {
say "$m is with $f";
}
my @keys = keys %couples;
foreach my $k (keys %couples) {
say "$k is with $couples{$k}";
}
my @values = values %couples;
say "values = @values";
use v5.14;
#################
# For older versions:
#
# use strict;
# use warnings;
#
#########################
# Variables
# Use "my" to declare a new variable
my $x = 7;
my $y = 15;
$y = 9;
my $name = "Ynon Perek";
my $website = "ynonperek.com";
my $z = $x + $y;
$y *= 15;
my $text = "$name website is $website";
my $t = $name . $website;
#############################
# Flow Control
#
if ( 2 < 5 ) {
say "Hello World";
}
for ( my $i=0; $i < 10; $i ++ ) {
say "i = $i";
}
my $n = 10;
while ( $n > 0 ) {
$n--;
}
#####################
# Input/Output
#
say "Welcome To Perl. Who are you ?";
chomp( my $me = <> );
say "-----> Welcome, $me <-------";
while (my $line = <>) {
chomp $line;
say "You said: $line";
}
##############
# Other Tips
#
# Show help on command chomp
# perldoc -f chomp
#
# List all perl functions:
# perldoc perlfunc
my $rnd = rand(10);
my $int = int($rnd);
say "rand = $rnd; int = $int";
use v5.14;
use warnings;
my $all_digits = qr{([0-9])};
my $all_caps = qr{[A-Z]};
my $caps_sep = qr{[A-Z]([^A-Z]+|$)};
while(<>) {
my @all_digits = /$all_digits/g;
say "1" if @all_digits >= 2;
say "2" if @all_digits == 4;
say "3" if /[A-Z]{5}/;
my @all_caps = /$caps_sep/g;
say "4" if @all_caps == 5;
say "5" if /^w.*\d$/;
my @words = split;
say "6" if @words >= 3;
my @long_words = grep /...../, @words;
say "7" if @long_words >= 2;
s/foo/bar/ for (@words);
say "8";
###############
my @l = qw/red blue green white yellow/;
# l1 has: blue, green, white, yellow
my @l1 = grep /..../, @l;
# l2 has: blue, white
my @l2 = grep /e$/, @l;
my @n = (1, 2, 3, 5, 9, 12, 14);
my @n1 = grep { $_ % 2 == 0 } @n;
}
use v5.14;
use warnings;
my $text = "My name is Inigo Montoya, you killed my father";
my $email_re = qr{^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$};
my $config_line_re = qr {
# title = Ultimate Fighter 7
# author = Mad Mike
(\w+) \s* = \s* (.+)
}x;
my $start_with_long_word_re = qr{
# a long word is longer than 4 lowercase
# letters
^ [a-z]{4,}
}x;
# Use =~ to match a regular expression
#
if ( $text =~ /[a-z]{4,}/ ) {
say "Your text has a long word";
}
my $digits_re = qr{ ([0-9]) }x;
#while (<>) {
#my ($k, $v) = /$config_line_re/;
#next if ! defined($k);
#say "title: $k, value: $v";
#}
#while (<>) {
#my @digits = /$digits_re/;
#say "@digits";
#}
while(<>) {
# replace in a variable
# $text =~ s/dog/cat/;
s/dog/cat/;
s/$spaces_at_start//;
s/\s+$//;
say;
}
(my $new = $old) =~ s/foo/bar;
my $new = $old;
$new =~ s/foo/bar;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment