-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.pl
60 lines (48 loc) · 1.47 KB
/
main.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/perl
use strict;
use warnings;
use File::Basename;
if (($#ARGV + 1) != 1)
{
print STDERR "USAGE: perl main.pl FILENAME\n";
exit 1;
}
open(INPUTFILE,'<',$ARGV[0]) or die "can't open $ARGV[0] : $!\n";
my $new_file_path;
my $input_unit;
my $output_unit;
my $multiplier;
sub check_unit_name {
my $unit = $_[0];
unless ( $unit =~ m/^[a-z]+$/ ) { return; }
return 1;
}
sub check_mul_num {
my $mult = $_[0];
unless ( $mult =~ m/^\d+(\.\d+)?$/ ) { return; }
return 1;
}
print("Print unit name which is in input-css-file\n");
$input_unit = <STDIN>;
chomp($input_unit);
unless ( check_unit_name $input_unit ) { print("Invalid unit name. Closing\n"); exit 2; }
print("Print unit name which will be in output-css-file\n");
$output_unit = <STDIN>;
chomp($output_unit);
unless ( check_unit_name $output_unit ) { print("Invalid unit name. Closing\n"); exit 2; }
print('Print how many ', $output_unit, ' are now in one ', $input_unit, "\n");
$multiplier = <STDIN>;
chomp($multiplier);
unless ( check_mul_num $multiplier ) { print("Invalid multiplier name. Closing\n"); exit 2; }
print("Print new file path\n");
$new_file_path = <STDIN>;
chomp($new_file_path);
if ( -e $new_file_path ) { print("File $new_file_path already exists. Closing\n"); exit 3; }
open(OUTPUTFILE,'>',$new_file_path) or die "can't open $new_file_path : $!\n";
foreach ( <INPUTFILE> )
{
s/([.0-9]+)(\s*)$input_unit/(int($1*$multiplier*1000)\/1000).$output_unit/eg;
print OUTPUTFILE;
}
close OUTPUTFILE;
close INPUTFILE;