Merge remote-tracking (subtree) branch 'PracticingPerl/master'
This commit is contained in:
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__
|
Reference in New Issue
Block a user