Last active 16 hours ago

kat's Avatar kat revised this gist 16 hours ago. Go to revision

1 file changed, 3 insertions, 5 deletions

urlref.pl

@@ -19,7 +19,6 @@ GetOptions(
19 19 "help" => \&usage,
20 20 ) or die "\nif you want to get a URL, supply a code: --get-code [CODE]\n";
21 21
22 -
23 22 sub encode {
24 23 my @chars = ("A".."Z","0".."9","_");
25 24
@@ -29,19 +28,18 @@ sub encode {
29 28 return $string;
30 29 }
31 30
32 -
33 31 sub addurl {
34 32 my $url = <STDIN>;
35 33 chomp $url;
36 - my $encodedurl = encode($url);
34 + my $code = encode($url);
37 35
38 36 if (!length $url == 0) {
39 - print("code is: $encodedurl\n");
37 + print("code is: $code\n");
40 38
41 39 my $filename = "links.txt";
42 40
43 41 open(my $fh, ">>", "$filename") or die "could not open $filename: $!";
44 - print $fh "$encodedurl $url\n";
42 + print $fh "$code $url\n";
45 43 close $fh;
46 44
47 45 print("saved URL to file: $filename\n");

kat's Avatar kat revised this gist 16 hours ago. Go to revision

1 file changed, 65 insertions

urlref.pl(file created)

@@ -0,0 +1,65 @@
1 + #!/usr/bin/env perl
2 +
3 + use 5.38.2;
4 + use strict;
5 + use warnings;
6 +
7 + use Getopt::Long qw(GetOptions);
8 +
9 + sub usage {
10 + print("Usage: urlref.pl [--get-code|--help]\n
11 + run without args to add URL\n");
12 + exit 0;
13 + }
14 +
15 + my $getcode;
16 +
17 + GetOptions(
18 + "get-code=s" => \$getcode,
19 + "help" => \&usage,
20 + ) or die "\nif you want to get a URL, supply a code: --get-code [CODE]\n";
21 +
22 +
23 + sub encode {
24 + my @chars = ("A".."Z","0".."9","_");
25 +
26 + my $string;
27 + $string .= $chars[int(rand(@chars))] for 1..3;
28 +
29 + return $string;
30 + }
31 +
32 +
33 + sub addurl {
34 + my $url = <STDIN>;
35 + chomp $url;
36 + my $encodedurl = encode($url);
37 +
38 + if (!length $url == 0) {
39 + print("code is: $encodedurl\n");
40 +
41 + my $filename = "links.txt";
42 +
43 + open(my $fh, ">>", "$filename") or die "could not open $filename: $!";
44 + print $fh "$encodedurl $url\n";
45 + close $fh;
46 +
47 + print("saved URL to file: $filename\n");
48 + } else {
49 + die("no URL given");
50 + }
51 + }
52 +
53 + if ($getcode) {
54 + my $filename = "links.txt";
55 +
56 + open(my $fh, "<", "$filename") or die "could not open $filename: $!";
57 + while(my $line = <$fh>) {
58 + if($line =~ /^$getcode\s/) {
59 + print("URL is: $line");
60 + }
61 + }
62 + close $fh;
63 + } else {
64 + addurl
65 + }
Newer Older