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

155 lines
1.8 KiB
Perl
Raw Normal View History

#!/usr/bin/env perl
use strict;
use warnings;
2011-01-05 03:12:56 +00:00
use 5.0100;
2011-01-05 02:26:55 +00:00
sub println {
local $, = "";
print +(@_ ? @_ : $_), $/;
2011-01-05 02:26:55 +00:00
}
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>;
2011-01-05 02:32:44 +00:00
print '$c = ';
my $c = <STDIN>;
my $ncookies = 0;
sub printcookies {
println("I like cookies " . $ncookies++);
2011-01-05 02:32:44 +00:00
}
2011-01-05 02:26:55 +00:00
if ($a == 5 && $b == 2) {
2011-01-05 02:32:44 +00:00
printcookies();
2011-01-05 02:26:55 +00:00
}
if ($a == 5 && $b == 2 || $c == 2) {
2011-01-05 02:32:44 +00:00
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();
2011-01-05 02:26:55 +00:00
}
2011-01-05 03:06:45 +00:00
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";
2011-01-05 03:12:56 +00:00
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";
}