box-o-sand/PracticingPerl/cli/operators.pl

155 lines
1.8 KiB
Perl

#!/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";
}