Merge remote-tracking (subtree) branch 'PracticingPerl/master'
This commit is contained in:
3735
PracticingPerl/cli/alice-in-wonderland.txt
Normal file
3735
PracticingPerl/cli/alice-in-wonderland.txt
Normal file
File diff suppressed because it is too large
Load Diff
10
PracticingPerl/cli/argv.pl
Normal file
10
PracticingPerl/cli/argv.pl
Normal file
@@ -0,0 +1,10 @@
|
||||
#!/usr/bin/env perl
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
foreach my $a (@ARGV) {
|
||||
printf("$a\n");
|
||||
}
|
||||
|
||||
1;
|
||||
__END__
|
48
PracticingPerl/cli/array_variables.pl
Normal file
48
PracticingPerl/cli/array_variables.pl
Normal file
@@ -0,0 +1,48 @@
|
||||
#!/usr/bin/env perl
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
my ($length, $width, $depth) = (10, 20, 15);
|
||||
|
||||
print "The values are: ", $length, $width, $depth, "\n";
|
||||
|
||||
my ($name1, $name2, $name3, $name4) = ('Paul', 'Michael', 'Jessica', 'Megan');
|
||||
|
||||
print "Names are: $name1, $name2, $name3, $name4\n";
|
||||
|
||||
($name1, $name2, $name3, $name4) = qw/Paul Michael Jessica Megan/;
|
||||
|
||||
print "Names are: $name1, $name2, $name3, $name4\n";
|
||||
|
||||
($name1, $name2, $name3, $name4) = qw(Paul Michael Jessica Megan);
|
||||
|
||||
print "Names are: $name1, $name2, $name3, $name4\n";
|
||||
|
||||
|
||||
my @nums = (1, 2, 3, 4, 5);
|
||||
print "\@nums=@nums\n";
|
||||
my @more = 6 .. 1000;
|
||||
print "\@more=@more\n";
|
||||
my @none = ();
|
||||
print "\@none=@none\n";
|
||||
my @names = qw/Paul Michael Jessica Megan/;
|
||||
print "\@names=@names\n";
|
||||
my @all = (@nums, @more);
|
||||
print "\@all=@all\n";
|
||||
my @nested = (@nums, \@more);
|
||||
print "\@nested=@nested\n";
|
||||
|
||||
my $all_len = @all;
|
||||
my $nested_len = @nested;
|
||||
|
||||
print "\@all has length $all_len\n";
|
||||
print "\@nested has length $nested_len\n";
|
||||
|
||||
|
||||
my @array = (1, 2, 3, 4, 5);
|
||||
print $array[0] . "\n";
|
||||
print $array[3] . "\n";
|
||||
print $array[-1] . "\n";
|
||||
print $array[4] . "\n";
|
||||
print $array[-1] . "\n";
|
||||
print $array[$#array] . "\n";
|
19
PracticingPerl/cli/datatypes.pl
Normal file
19
PracticingPerl/cli/datatypes.pl
Normal file
@@ -0,0 +1,19 @@
|
||||
#!/usr/bin/env perl
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
my $foo = 'foo';
|
||||
print "\$foo=$foo\n";
|
||||
|
||||
my @foo = (1, 2, 3);
|
||||
print "\@foo=@foo\n";
|
||||
|
||||
my %foo = (
|
||||
1 => 'one',
|
||||
2 => 'two',
|
||||
3 => 'three',
|
||||
);
|
||||
print "\%foo=" . %foo . "\n";
|
||||
|
||||
my $log = *STDIN;
|
||||
print $log . "\n";
|
10
PracticingPerl/cli/ex01/a.pl
Normal file
10
PracticingPerl/cli/ex01/a.pl
Normal file
@@ -0,0 +1,10 @@
|
||||
#!/usr/bin/env perl
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
|
||||
print "Hello world\n";
|
||||
|
||||
|
||||
1;
|
||||
__END__
|
30
PracticingPerl/cli/ex01/b.pl
Normal file
30
PracticingPerl/cli/ex01/b.pl
Normal file
@@ -0,0 +1,30 @@
|
||||
#!/usr/bin/env perl
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
|
||||
my $num = 4000 / 7;
|
||||
print "$num\n";
|
||||
|
||||
print int($num + 0.005) . "\n";
|
||||
|
||||
printf("%.3f\n", $num);
|
||||
|
||||
printf("00%.3f\n", $num);
|
||||
|
||||
|
||||
sub show_signed {
|
||||
my ($num) = @_;
|
||||
if ($num gt 0) {
|
||||
printf("+%.3f\n", $num);
|
||||
} else {
|
||||
printf("%.3f\n", $num);
|
||||
}
|
||||
}
|
||||
|
||||
show_signed($num);
|
||||
show_signed($num - 1000);
|
||||
|
||||
|
||||
1;
|
||||
__END__
|
32
PracticingPerl/cli/ex01/c.pl
Normal file
32
PracticingPerl/cli/ex01/c.pl
Normal file
@@ -0,0 +1,32 @@
|
||||
#!/usr/bin/env perl
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
use Math::Complex;
|
||||
|
||||
|
||||
sub quad {
|
||||
my ($a, $b, $c) = @_;
|
||||
my $positive = (
|
||||
($b * -1) - sqrt(($b ** 2) - (4 * ($a * $c))) / (2 * $a)
|
||||
);
|
||||
my $negative = (
|
||||
($b * -1) + sqrt(($b ** 2) - (4 * ($a * $c))) / (2 * $a)
|
||||
);
|
||||
|
||||
return ($positive, $negative);
|
||||
}
|
||||
|
||||
|
||||
my @xx0 = quad(1, 2, 3);
|
||||
printf("%s, %s\n", $xx0[0], $xx0[1]);
|
||||
|
||||
my @xx1 = quad(4, 5, 6);
|
||||
printf("%s, %s\n", $xx1[0], $xx1[1]);
|
||||
|
||||
my @xx2 = quad(7, 8, 9);
|
||||
printf("%s, %s\n", $xx2[0], $xx2[1]);
|
||||
|
||||
|
||||
1;
|
||||
__END__
|
40
PracticingPerl/cli/ex01/d.pl
Normal file
40
PracticingPerl/cli/ex01/d.pl
Normal file
@@ -0,0 +1,40 @@
|
||||
#!/usr/bin/env perl
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
use Math::Complex;
|
||||
|
||||
|
||||
print "Entrez-vous\n";
|
||||
|
||||
my ($a, $b, $c) = (rand(100), rand(100), rand(100));
|
||||
|
||||
print "\$a=$a\n";
|
||||
print "\$b=$b\n";
|
||||
print "\$c=$c\n";
|
||||
|
||||
|
||||
sub quad {
|
||||
my ($a, $b, $c) = @_;
|
||||
my $positive = (
|
||||
($b * -1) - sqrt(($b ** 2) - (4 * ($a * $c))) / (2 * $a)
|
||||
);
|
||||
my $negative = (
|
||||
($b * -1) + sqrt(($b ** 2) - (4 * ($a * $c))) / (2 * $a)
|
||||
);
|
||||
|
||||
return ($positive, $negative);
|
||||
}
|
||||
|
||||
|
||||
my @result = quad($a, $b, $c);
|
||||
printf("%s, %s\n", $result[0], $result[1]);
|
||||
|
||||
|
||||
1;
|
||||
__END__
|
||||
|
||||
|
||||
|
||||
1;
|
||||
__END__
|
22
PracticingPerl/cli/ex02/input.pl
Normal file
22
PracticingPerl/cli/ex02/input.pl
Normal file
@@ -0,0 +1,22 @@
|
||||
#!/usr/bin/env perl
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
|
||||
my @inlines = ();
|
||||
my $i = 0;
|
||||
|
||||
for ($i = 0; $i < 3; $i++) {
|
||||
print "input please: ";
|
||||
my $inline = <STDIN>;
|
||||
$inline =~ s/\s*$//;
|
||||
$inlines[$i] = $inline;
|
||||
}
|
||||
|
||||
|
||||
print "You said:\n";
|
||||
print join("|", $inlines[0], $inlines[1], $inlines[2] . "\n");
|
||||
|
||||
|
||||
1;
|
||||
__END__
|
11
PracticingPerl/cli/ex02/input_argv.pl
Normal file
11
PracticingPerl/cli/ex02/input_argv.pl
Normal file
@@ -0,0 +1,11 @@
|
||||
#!/usr/bin/env perl
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
foreach my $inline (@ARGV) {
|
||||
print $inline . "\n";
|
||||
}
|
||||
|
||||
|
||||
1;
|
||||
__END__
|
33
PracticingPerl/cli/ex02/input_fromfile.pl
Normal file
33
PracticingPerl/cli/ex02/input_fromfile.pl
Normal file
@@ -0,0 +1,33 @@
|
||||
#!/usr/bin/env perl
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
use IO::File;
|
||||
use File::Basename;
|
||||
|
||||
|
||||
my $script = basename($0);
|
||||
my $USAGE = <<USAGE
|
||||
Usage: $script <infile>
|
||||
USAGE
|
||||
;
|
||||
|
||||
if (!defined $ARGV[0]) {
|
||||
die $USAGE;
|
||||
}
|
||||
|
||||
my $infile = $ARGV[0];
|
||||
open(DAT, $infile) or die "$infile: $!";
|
||||
my @cleaned = ();
|
||||
foreach my $line (<DAT>) {
|
||||
$line =~ s/\s*$//;
|
||||
$cleaned[++$#cleaned] = $line;
|
||||
}
|
||||
|
||||
print join("|", @cleaned) . "\n";
|
||||
|
||||
close(DAT);
|
||||
|
||||
|
||||
1;
|
||||
__END__
|
53
PracticingPerl/cli/ex02/quadratic.pl
Normal file
53
PracticingPerl/cli/ex02/quadratic.pl
Normal file
@@ -0,0 +1,53 @@
|
||||
#!/usr/bin/env perl
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
use Math::Complex;
|
||||
use File::Basename;
|
||||
|
||||
|
||||
my $prog = basename($0);
|
||||
my $USAGE = <<USAGE
|
||||
Usage: $prog <a> <b> <c>
|
||||
USAGE
|
||||
;
|
||||
|
||||
|
||||
sub quad {
|
||||
my ($a, $b, $c) = @_;
|
||||
my $positive = (
|
||||
($b * -1) - sqrt(($b ** 2) - (4 * ($a * $c))) / (2 * $a)
|
||||
);
|
||||
my $negative = (
|
||||
($b * -1) + sqrt(($b ** 2) - (4 * ($a * $c))) / (2 * $a)
|
||||
);
|
||||
|
||||
return ($positive, $negative);
|
||||
}
|
||||
|
||||
foreach my $arg (@ARGV) {
|
||||
if (($arg eq "-h") || ($arg eq "--help")) {
|
||||
die $USAGE;
|
||||
}
|
||||
}
|
||||
|
||||
my ($a, $b, $c) = @ARGV;
|
||||
if (!defined $a) {
|
||||
print '$a = ';
|
||||
$a = <STDIN>;
|
||||
}
|
||||
if (!defined $b) {
|
||||
print '$b = ';
|
||||
$b = <STDIN>;
|
||||
}
|
||||
if (!defined $c) {
|
||||
print '$c = ';
|
||||
$c = <STDIN>;
|
||||
}
|
||||
|
||||
my @result = quad($a, $b, $c);
|
||||
printf("%s, %s\n", $result[0], $result[1]);
|
||||
|
||||
|
||||
1;
|
||||
__END__
|
61
PracticingPerl/cli/ex03/rwfakegrep.pl
Normal file
61
PracticingPerl/cli/ex03/rwfakegrep.pl
Normal file
@@ -0,0 +1,61 @@
|
||||
#!/usr/bin/env perl
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
use File::Basename;
|
||||
use English qw/$PROGRAM_NAME/;
|
||||
|
||||
my $basename = basename($0);
|
||||
my $USAGE = <<USAGE
|
||||
Usage: $basename <infile> <outfile>
|
||||
USAGE
|
||||
;
|
||||
|
||||
|
||||
sub show_last_chapter_with_nlines {
|
||||
my ($ch, $nlines) = @_;
|
||||
print "Chapter $ch has $nlines lines\n";
|
||||
}
|
||||
|
||||
|
||||
sub main {
|
||||
open(INFILE, '<', $ARGV[0]) or die "$USAGE\n$!";
|
||||
open(OUTFILE, '>', $ARGV[1]) or die "$USAGE\n$!";
|
||||
|
||||
my $have_seen_firstchapter = 'no';
|
||||
my $nlines = 0;
|
||||
my $chline = '';
|
||||
my $ch = '';
|
||||
|
||||
foreach my $inline (<INFILE>) {
|
||||
if ($inline =~ /^\s*CHAPTER.*$/) {
|
||||
|
||||
$chline = "$inline";
|
||||
$ch = "$chline";
|
||||
$ch =~ s/^\s*CHAPTER\s*([0-9]*).*/$1/;
|
||||
$ch =~ s/[\r\n]//;
|
||||
|
||||
if ($have_seen_firstchapter eq 'yes') {
|
||||
show_last_chapter_with_nlines($ch - 1, $nlines);
|
||||
$nlines = 0;
|
||||
} else {
|
||||
$have_seen_firstchapter = 'yes';
|
||||
$nlines = 0;
|
||||
}
|
||||
print OUTFILE $inline;
|
||||
}
|
||||
$nlines++;
|
||||
}
|
||||
|
||||
show_last_chapter_with_nlines($ch, $nlines);
|
||||
|
||||
}
|
||||
|
||||
|
||||
if ($PROGRAM_NAME eq __FILE__) {
|
||||
main();
|
||||
}
|
||||
|
||||
|
||||
1;
|
||||
__END__
|
23
PracticingPerl/cli/ex03/rwfiles.pl
Normal file
23
PracticingPerl/cli/ex03/rwfiles.pl
Normal file
@@ -0,0 +1,23 @@
|
||||
#!/usr/bin/env perl
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
use File::Basename;
|
||||
|
||||
my $basename = basename($0);
|
||||
my $USAGE = <<USAGE
|
||||
Usage: $basename <infile> <outfile>
|
||||
USAGE
|
||||
;
|
||||
|
||||
open(INFILE, '<', $ARGV[0]) or die "$USAGE\n$!";
|
||||
open(OUTFILE, '>', $ARGV[1]) or die "$USAGE\n$!";
|
||||
|
||||
foreach my $inline (<INFILE>) {
|
||||
print OUTFILE $inline;
|
||||
}
|
||||
|
||||
|
||||
|
||||
1;
|
||||
__END__
|
36
PracticingPerl/cli/filehandles.pl
Normal file
36
PracticingPerl/cli/filehandles.pl
Normal file
@@ -0,0 +1,36 @@
|
||||
#!/usr/bin/env perl
|
||||
use strict;
|
||||
use warnings;
|
||||
use IO::File;
|
||||
use Data::Dumper;
|
||||
|
||||
open(my $fh, '<', 'foo.txt');
|
||||
|
||||
my @lines = <$fh>;
|
||||
|
||||
print Data::Dumper->Dump([\@lines], [qw(lines)]);
|
||||
|
||||
|
||||
open(my $fh2, '<', 'foo.txt');
|
||||
|
||||
while(my $line = <$fh2>) {
|
||||
print $line;
|
||||
}
|
||||
|
||||
|
||||
my $fh3 = IO::File->new('foo.txt', 'r');
|
||||
|
||||
while(my $line = $fh3->getline()) {
|
||||
print $line;
|
||||
}
|
||||
|
||||
|
||||
my $fh4 = IO::File->new('foo.txt', 'r');
|
||||
my @lines4 = $fh4->getlines();
|
||||
|
||||
print @lines4;
|
||||
print Data::Dumper->Dump([\@lines4], [qw(lines)]);
|
||||
|
||||
|
||||
1;
|
||||
__END__
|
6
PracticingPerl/cli/foo.txt
Normal file
6
PracticingPerl/cli/foo.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
these
|
||||
runes
|
||||
are
|
||||
dusty
|
||||
sir
|
||||
?
|
54
PracticingPerl/cli/functions.pl
Normal file
54
PracticingPerl/cli/functions.pl
Normal file
@@ -0,0 +1,54 @@
|
||||
#!/usr/bin/env perl
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
sub print_error_message {
|
||||
my ($message) = @_;
|
||||
print STDOUT "ERROR: " . $message . "!!!\n";
|
||||
}
|
||||
|
||||
|
||||
print_error_message("something bad happened");
|
||||
print_error_message("something really horrible happened");
|
||||
print_error_message("something sort of annoying happened");
|
||||
|
||||
|
||||
sub add_two_numbers {
|
||||
my ($x, $y) = @_;
|
||||
my $sum = $x + $y;
|
||||
return $sum;
|
||||
}
|
||||
|
||||
print add_two_numbers(3, 4) . "\n";
|
||||
print add_two_numbers(5, 6) . "\n";
|
||||
print add_two_numbers(7, 8) . "\n";
|
||||
|
||||
|
||||
sub add_two_numbers_and_mult_by_three {
|
||||
my ($x, $y) = @_;
|
||||
my $sum = add_two_numbers($x, $y);
|
||||
my $sum_times_three = $sum * 3;
|
||||
return $sum_times_three;
|
||||
}
|
||||
|
||||
print add_two_numbers_and_mult_by_three(3, 4) . "\n";
|
||||
print add_two_numbers_and_mult_by_three(5, 6) . "\n";
|
||||
print add_two_numbers_and_mult_by_three(7, 8) . "\n";
|
||||
|
||||
|
||||
sub factorial {
|
||||
my ($num) = @_;
|
||||
if ($num == 1) {
|
||||
return 1;
|
||||
} else {
|
||||
return $num * factorial($num - 1);
|
||||
}
|
||||
}
|
||||
|
||||
foreach my $n (1 .. 10) {
|
||||
print factorial($n) . "\n";
|
||||
}
|
||||
|
||||
|
||||
1;
|
||||
__END__
|
47
PracticingPerl/cli/hash_variables.pl
Normal file
47
PracticingPerl/cli/hash_variables.pl
Normal file
@@ -0,0 +1,47 @@
|
||||
#!/usr/bin/env perl
|
||||
use strict;
|
||||
use warnings;
|
||||
use Data::Dumper;
|
||||
|
||||
|
||||
my %petsounds = (
|
||||
"cat" => "meow",
|
||||
"dog" => "woof",
|
||||
"snake" => "hiss"
|
||||
);
|
||||
|
||||
|
||||
print "The cat goes " . $petsounds{"cat"} . ".\n";
|
||||
|
||||
|
||||
$petsounds{"mouse"} = "squeak!";
|
||||
$petsounds{"dog"} = "arf!";
|
||||
|
||||
|
||||
delete($petsounds{"cat"});
|
||||
|
||||
|
||||
print "The mouse goes " . $petsounds{"mouse"} . ".\n";
|
||||
|
||||
|
||||
print Dumper(\%petsounds) . "\n";
|
||||
|
||||
|
||||
my %a = ();
|
||||
$a{1}{"a"}{"A"} = "FIRST";
|
||||
$a{1}{"c"}{"B"} = "THIRD";
|
||||
$a{1}{"b"}{"C"} = "SECOND";
|
||||
|
||||
foreach my $k1 (sort keys %a) {
|
||||
foreach my $k2 (sort keys %{$a{$k1}}) {
|
||||
foreach my $k3 (sort keys %{$a{$k1}{$k2}}) {
|
||||
print "$k1\t$k2\t$k3\t$a{$k1}{$k2}{$k3}\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
print Dumper(\%a);
|
||||
|
||||
|
||||
1;
|
||||
__END__
|
10
PracticingPerl/cli/heredoc.pl
Normal file
10
PracticingPerl/cli/heredoc.pl
Normal file
@@ -0,0 +1,10 @@
|
||||
#!/usr/bin/perl
|
||||
|
||||
print <<EOF
|
||||
this
|
||||
is a
|
||||
multiline
|
||||
string
|
||||
yes?
|
||||
EOF
|
||||
;
|
5
PracticingPerl/cli/myprog.pl
Executable file
5
PracticingPerl/cli/myprog.pl
Executable file
@@ -0,0 +1,5 @@
|
||||
#!/usr/bin/perl
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
print "Hello World\n";
|
11
PracticingPerl/cli/numbers.pl
Normal file
11
PracticingPerl/cli/numbers.pl
Normal file
@@ -0,0 +1,11 @@
|
||||
#!/usr/bin/perl
|
||||
|
||||
my $a = 4;
|
||||
|
||||
print "\$a = $a\n";
|
||||
print "\$a++ = " . $a++ . "\n";
|
||||
print "++\$a = " . ++$a . "\n";
|
||||
print "\$a-- = " . $a-- . "\n";
|
||||
print "--\$a = " . --$a . "\n";
|
||||
print "\$a += 5 = " . ($a += 5) . "\n";
|
||||
print "\$a -= 2 = " . ($a -= 2) . "\n";
|
154
PracticingPerl/cli/operators.pl
Normal file
154
PracticingPerl/cli/operators.pl
Normal file
@@ -0,0 +1,154 @@
|
||||
#!/usr/bin/env perl
|
||||
use strict;
|
||||
use warnings;
|
||||
use 5.0100;
|
||||
|
||||
|
||||
sub println {
|
||||
local $, = "";
|
||||
print +(@_ ? @_ : $_), $/;
|
||||
}
|
||||
|
||||
|
||||
println(4 % 3);
|
||||
|
||||
println(4 % 2);
|
||||
|
||||
println(-4 % 3);
|
||||
|
||||
println(4 ** 2);
|
||||
|
||||
println(2 ** (1/2));
|
||||
|
||||
my $foo = 1;
|
||||
|
||||
println($foo--);
|
||||
|
||||
println($foo);
|
||||
|
||||
println("dog");
|
||||
|
||||
$foo = 1;
|
||||
|
||||
println(--$foo);
|
||||
|
||||
println($foo);
|
||||
|
||||
$foo = 'd';
|
||||
|
||||
println(--$foo);
|
||||
|
||||
println($foo);
|
||||
|
||||
$foo = 'Z';
|
||||
|
||||
println($foo++);
|
||||
|
||||
println($foo);
|
||||
|
||||
$foo = 'Hello';
|
||||
$foo .= ', world';
|
||||
println($foo);
|
||||
|
||||
my $bar = '+';
|
||||
$bar x= 6;
|
||||
println($bar);
|
||||
|
||||
print '$a = ';
|
||||
my $a = <STDIN>;
|
||||
print '$b = ';
|
||||
my $b = <STDIN>;
|
||||
print '$c = ';
|
||||
my $c = <STDIN>;
|
||||
|
||||
|
||||
my $ncookies = 0;
|
||||
|
||||
sub printcookies {
|
||||
println("I like cookies " . $ncookies++);
|
||||
}
|
||||
|
||||
|
||||
if ($a == 5 && $b == 2) {
|
||||
printcookies();
|
||||
}
|
||||
|
||||
|
||||
if ($a == 5 && $b == 2 || $c == 2) {
|
||||
printcookies();
|
||||
}
|
||||
|
||||
|
||||
if ($a == 5 and ($b == 2 || $c == 3)) {
|
||||
printcookies();
|
||||
}
|
||||
|
||||
|
||||
if ($a == 5 && $b == 2 or $c == 3) {
|
||||
printcookies();
|
||||
}
|
||||
|
||||
|
||||
if ($a == 5 && $b == 2 or $c == 3) {
|
||||
printcookies();
|
||||
}
|
||||
|
||||
|
||||
if (($a == 5 && $b == 2) or $c == 3) {
|
||||
printcookies();
|
||||
}
|
||||
|
||||
|
||||
($a, $b) = (5, 2);
|
||||
if ($a == 4 && $b < 3) {
|
||||
printcookies();
|
||||
}
|
||||
|
||||
sub foo {
|
||||
return ($c eq 0);
|
||||
}
|
||||
|
||||
foo() or print "foo() failed\n";
|
||||
|
||||
|
||||
print 'Hello' . ' world';
|
||||
print "\n";
|
||||
|
||||
my $str = "hi";
|
||||
my $repeated_str = $str x 5;
|
||||
println("$repeated_str");
|
||||
|
||||
println($repeated_str eq 'hihihihihi');
|
||||
|
||||
println($str ne 'hi');
|
||||
|
||||
$str =~ s/i/m/g;
|
||||
println($str);
|
||||
|
||||
|
||||
print 'A' .. 'Z', "\n";
|
||||
print 'a' .. 'z', "\n";
|
||||
print 'A' .. 'z', "\n";
|
||||
print 1 .. 20, "\n";
|
||||
print '&' .. '!', "\n";
|
||||
print 10 .. -10, "\n";
|
||||
print "$_\n" foreach 1 .. 10, "\n";
|
||||
|
||||
|
||||
my $scalar = 'hi';
|
||||
my @array = qw(one two three);
|
||||
my %hash = (
|
||||
hi => 1,
|
||||
ho => 2,
|
||||
he => 3,
|
||||
);
|
||||
|
||||
if ($scalar ~~ @array) {
|
||||
print "scalar matches array\n";
|
||||
}
|
||||
if ($scalar ~~ %hash) {
|
||||
print "scalar matches hash\n";
|
||||
}
|
||||
if (@array ~~ %hash) {
|
||||
print "array matches hash\n";
|
||||
}
|
34
PracticingPerl/cli/quoting.pl
Normal file
34
PracticingPerl/cli/quoting.pl
Normal file
@@ -0,0 +1,34 @@
|
||||
#!/usr/bin/env perl
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
|
||||
print qq{I said "They're the most delicious fruits!".\n};
|
||||
print q/I said "They're the most delicious fruits!"./ . "\n";
|
||||
|
||||
|
||||
print <<OUTPUT
|
||||
I said "They're the most delicious fruits!".
|
||||
OUTPUT
|
||||
;
|
||||
|
||||
my $one = 'mangoes';
|
||||
|
||||
print "I like $one.\n";
|
||||
print 'I like $one.' . "\n";
|
||||
print qq/I love $one.\n/;
|
||||
print q#I love $one.# . "\n";
|
||||
|
||||
print <<OUT
|
||||
I <3 $one
|
||||
OUT
|
||||
;
|
||||
|
||||
print <<'OUT'
|
||||
I <3 $one
|
||||
OUT
|
||||
;
|
||||
|
||||
|
||||
1;
|
||||
__END__
|
8
PracticingPerl/cli/raw_printing.pl
Normal file
8
PracticingPerl/cli/raw_printing.pl
Normal file
@@ -0,0 +1,8 @@
|
||||
#!/usr/bin/perl
|
||||
|
||||
my $forename = "John";
|
||||
my $message = "Hello ";
|
||||
|
||||
print $message;
|
||||
print $forename;
|
||||
print "\n";
|
30
PracticingPerl/cli/scalar_numbers.pl
Normal file
30
PracticingPerl/cli/scalar_numbers.pl
Normal file
@@ -0,0 +1,30 @@
|
||||
#!/usr/bin/perl
|
||||
|
||||
my $age = 30;
|
||||
|
||||
print "Hello $age year old.\n";
|
||||
|
||||
|
||||
|
||||
my $x = 10;
|
||||
my $y = $x + 1;
|
||||
|
||||
print "Using a number $x + 1 = $y.\n";
|
||||
|
||||
$x = "10";
|
||||
$y = $x + 1;
|
||||
|
||||
print "Using a string $x + 1 = $y.\n";
|
||||
|
||||
$x = "ten";
|
||||
$y = $x + 1;
|
||||
|
||||
print "Using an English word, $x + 1 = $y.\n";
|
||||
|
||||
$x = "2ten";
|
||||
$y = $x + 1;
|
||||
|
||||
print "Using a funny string, $x + 1 = $y.\n";
|
||||
|
||||
|
||||
1;
|
15
PracticingPerl/cli/stdio.pl
Normal file
15
PracticingPerl/cli/stdio.pl
Normal file
@@ -0,0 +1,15 @@
|
||||
#!/usr/bin/env perl
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
|
||||
print "What is your name?: ";
|
||||
|
||||
my $name = <STDIN>;
|
||||
$name =~ s/\n//;
|
||||
|
||||
print "Your name is $name\n";
|
||||
|
||||
|
||||
1;
|
||||
__END__
|
6
PracticingPerl/cli/string_operators.pl
Normal file
6
PracticingPerl/cli/string_operators.pl
Normal file
@@ -0,0 +1,6 @@
|
||||
#!/usr/bin/perl
|
||||
|
||||
print "Hello" . "World\n";
|
||||
print "Hello" . " " . "World\n";
|
||||
|
||||
print "Hello" x 5, "\n";
|
75
PracticingPerl/cli/variables.pl
Normal file
75
PracticingPerl/cli/variables.pl
Normal file
@@ -0,0 +1,75 @@
|
||||
#!/usr/bin/perl
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
my $firstname = "Jonathan";
|
||||
|
||||
print "Hello, $firstname\n";
|
||||
|
||||
$firstname = "John";
|
||||
|
||||
print "Goodbye, $firstname\n";
|
||||
|
||||
|
||||
my $variable = "\0";
|
||||
my $another_variable = 2;
|
||||
my @array_variable = qw(one two three);
|
||||
|
||||
$variable = 3;
|
||||
print "$variable\n";
|
||||
$variable = 3.1415926;
|
||||
print "$variable\n";
|
||||
$variable = 3.402823669209384634633e+38;
|
||||
print "$variable\n";
|
||||
$variable = $another_variable + 1;
|
||||
print "$variable\n";
|
||||
$variable = 'Can contain text';
|
||||
print "$variable\n";
|
||||
$variable = \$another_variable;
|
||||
print "$variable\n";
|
||||
$variable = \@array_variable;
|
||||
print "$variable\n";
|
||||
|
||||
|
||||
my @Array1 = (1, 2, 3);
|
||||
my @Array2 = (4, 5, 6);
|
||||
my @Array3 = (7, 8, 9);
|
||||
|
||||
@array_variable = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
|
||||
print "@array_variable\n";
|
||||
@array_variable = (1 .. 10);
|
||||
print "@array_variable\n";
|
||||
@array_variable = ('John', 'Paul', 'George', 'Ringo');
|
||||
print "@array_variable\n";
|
||||
@array_variable = qw/John Paul George Ringo/;
|
||||
print "@array_variable\n";
|
||||
@array_variable = qw/red blue 1 green 5/;
|
||||
print "@array_variable\n";
|
||||
@array_variable = (\@Array1, \@Array2, \@Array3);
|
||||
print "@array_variable\n";
|
||||
|
||||
my $idx = 2;
|
||||
|
||||
$array_variable[0] = 1;
|
||||
print "@array_variable\n";
|
||||
$array_variable[$idx] = 1;
|
||||
print "@array_variable\n";
|
||||
|
||||
my %hash = (
|
||||
'key1' => 'value1',
|
||||
'key2' => 'value2',
|
||||
'key3' => 'value3',
|
||||
);
|
||||
print %hash, "\n";
|
||||
|
||||
$hash{'key1'} = 'newval1';
|
||||
print %hash, "\n";
|
||||
|
||||
|
||||
sub fib {
|
||||
my $n = shift();
|
||||
return $n if $n < 2;
|
||||
return fib($n - 1) + fib($n - 2);
|
||||
}
|
||||
|
||||
print fib(14), "\n";
|
Reference in New Issue
Block a user