in reply to Url2Link 0.1 GUI/TK

Thank you for contributing code, but it's not very Perl-ish. Let's make some changes, eh? (And what is up with multiple closing braces on the last line --- did you not a receive a syntax error like i did?)

First, your check for the "Links" directory is a bit awkward, we shouldn't have to bail if the directory already exists:

my $dir = 'Links'; if (-d $dir) { chdir $dir or die "can't chdir: $!"; } else { mkdir $dir or die "can't mkdir: $!"; }
but why even do that? All you need to do is create the dir if id doesn't exist, and then use that dir name when you write to files. This prevents having to chdir (something i try to avoid). Next, why waste RAM with an array when you loop on the file handle?
open FILEIN, "urls.txt" or die "Could not open file $!"; while(<FILEIN>) { chomp; ... }
As you will see in a little while however, my version will need to 'slurp' the the whole file into a scalar. Also, this code screams out to me "Use a Getopt module!!", but i'll leave that as an exercise. ;)

Next, pull in the URI::Find, Config::IniHash and File::Basename CPAN modules to ease our burden. At this point, however, the skeleton of your script changes, so here is my version complete:

use strict; use warnings; use URI::Find; use Config::IniHash; use File::Basename; use vars qw( @FOUND ); my $dir = 'Links'; my $file = 'urls.txt'; unless (-d $dir) { mkdir $dir or die "can't mkdir: $!"; } open FILEIN, $file or die "can't open $file: $!"; my $urls = do {local $/; <FILEIN>}; my $finder = URI::Find->new(\&found); $finder->find(\$urls); for (@FOUND) { my $hash = { DEFAULT => { BASEURL => $_ }, InternetShortcut => { URL => $_, Modified => 0 }, }; my $file = basename($_,'.*'); $file =~ s/(\.\w+)+/\.url/; WriteINI("$dir/\u$file", $hash); } sub found { my($uri, $orig_uri) = @_; push @FOUND,$orig_uri; }

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

Replies are listed 'Best First'.
Re^2: url2link
by Aristotle (Chancellor) on Jan 13, 2003 at 10:17 UTC

    The basename + substitution business doesn't seem to make any sense. The doc says basename quotes any metacharacters in the suffix(es), so that line is looking for a literal dot star at the end of the filename - rather unlikely, even if we have a URL to begin with. The substitution will replace the first repeated sequence of one dot followed by any number of word characters by .url (whose dot needs no quoting since it's on the right hand side of the substitution anyway). That sequence does not necessarily have to be what terminates the string. Taking a ponderous guess at the purpose of the hooha, I bid URI.

    And for some more Perlishness, use the diamond operator instead of explicitly opening a file. With lots fewer temporaries:

    use strict; use warnings; use URI; use URI::Find; use Config::IniHash; use File::Basename; use File::Spec::Functions qw(catfile); my $dir = 'Links'; unless (-d $dir) { mkdir $dir or die "can't mkdir($dir): $!"; } URI::Find->new(\do {local $/; <> })->find(sub { my($uri, $orig_uri) = @_; WriteINI(catfile($dir, uc URI->new($uri)->host), { DEFAULT => { BASEURL => $uri }, InternetShortcut => { URL => $uri, Modified => 0 }, }); });

    Makeshifts last the longest.

      "The basename + substitution business doesn't seem to make any sense."

      I thought it made sense ... i just thought it to be a but i now see that it is a horrible way to do it! ;) I knew there had to be a better way, and what you have is soooo much better. Thanks Aristotle.

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)
      
        "jeffa says regarding Url2Link 0.1 GUI/TK ... we like CPAN around here" So you are not allow to be creative in here? Meaning come up with your core code?
Re: (jeffa) Re: url2link
by m_dv (Initiate) on Jan 12, 2003 at 22:39 UTC
    Thank you for the comments and suggestions, however I was already starting to work on a similar version that uses the TK module. I would look to your code carefully to examined it in more details later. Thank you.