Re: Updates URL.pm file

Andreas Koenig (andreas@mathilda.techne.co.uk)
Mon, 20 Mar 95 10:25:46 GMT


From: Martijn Koster <m.koster@nexor.co.uk>
> Like it... I have appended a few diffs that:

Like it, too, wanted to make some suggestions, turned them into a
patch, too. Take it as a suggestion, please.

What I do is:
- add a version number
- turn localpath into a method (I'm nervous, when I see modules that
  mix methods and functions, it's error-prone)
- change the documentation for C<base>, because I felt it was
  a bit confusing to have C<base> among the constructors.
- fixed a bug inlocalpath, that didn't do the right thing for relative
  paths
- turned the regex with many "|" into a string comparison for
  efficiency.
- removed a redundant "?" in the _read_netloc subroutine


> I vote for WWW::URL::http etc, with WWW::LWP::Request etc for the 
> lwp library

Coming to vote, I say, just fine by me anything.


>> Should URL be stored in encoded or decoded format and related issues.

> That needs sorting out. Back to the spec...


I think, that has nothing to do with the specs. I'd suggest any URL is
stored in unescaped format. We are in perl, aren't we? 8 bit clean,
able to store \000, any escaped format is likely to confuse the user
(me). Of course, I wish we had a escape pethod on URLs, that returns
for any given URL the escaped string. It has been mentioned, that it's
not possible to turn a stringified URL into an escaped one with just a
regular expression. But now all objects know, which class of URL they
belong to, and each element can be escaped by an escape method in the
respective class. Even URL->escape('http://foo.com/gisle.gif') could
be done, and would be nice to have.

My 2p, thank you for the nice work,
andreas

--- URL.pm.noversion	Mon Mar 20 09:55:13 1995
+++ URL.pm	Mon Mar 20 10:05:13 1995
@@ -4,7 +4,8 @@
 
 $rcsid = '$Id: URL.pm,v 1.6 1995/03/17 20:10:34 timbo Exp timbo $';
 $rcsid = $rcsid; # shut up -w
-
+$Version = 1.00;
+$Version = $Version;
 #####################################################################
 
 =head1 NAME
@@ -19,8 +20,7 @@
 
     $url1 = new URL 'http://www.nr.no/gisle.gif';
 
-    $url2 = new URL 'gisle.gif';
-    $url2->base('http://www.nr.no/');
+    $url2 = new URL 'gisle.gif', 'http://www.nr.no/';
 
     $url3 = $url2->abs;	# get absolute url using base
 
@@ -28,9 +28,9 @@
 
     $url5 = $url4->clone;
 
-    $pwd  = URL::localpath();
-    $dir  = URL::localpath('/tmp');
-    $file = URL::localpath('/etc/motd');
+    $pwd  = localpath URL;
+    $dir  = localpath URL '/tmp';
+    $file = localpath URL '/etc/motd';
 
     # Printing
 
@@ -38,6 +38,7 @@
 
     # Retrieving and storing common elements
 
+    $base   = $url->base;
     $scheme = $url->scheme;
     $user     = $url->user;
     $password = $url->password;
@@ -48,6 +49,7 @@
     $query  = $url->query;
     $frag   = $url->frag;
 
+    $url->base('http://www.nr.no/');
     $url->scheme('http');
     $url->host('www.w3.org');
     $url->port('80');
@@ -217,9 +219,9 @@
 #
 sub localpath
 {
-    my($path) = shift;
-    ($path = fastcwd()) =~ s:/?$:/: unless defined $path;
-    new URL "file://localhost$path";
+    my($self, $file) = @_;
+    ($path = fastcwd()) =~ s:/?$:/: unless $file && $file =~ m:^/:;
+    new URL "file://localhost$path$file";
 }
 
 
@@ -303,7 +305,10 @@
         if ($element eq 'netloc') {
             $self->_read_netloc();
         }
-        elsif ($element =~ m/^(user)|(password)|(host)|(port)$/) {
+        elsif ($element eq 'user' || 
+	       $element eq 'password' ||
+	       $element eq 'host' ||
+	       $element eq 'port' ) {
             $self->_write_netloc();
         }
     }
@@ -384,7 +389,7 @@
 sub _read_netloc {	# netloc -> user, password, host, post
     my($self) = @_;
     my $nl = $self->{'netloc'} || '';
-    $self->{'user'}     = $1 if $nl =~ s/^([^:@]*):?([^@]*)?@//;
+    $self->{'user'}     = $1 if $nl =~ s/^([^:@]*):?([^@]*)@//;
     $self->{'password'} = $2 if $2;
     $self->{'host'}     = $1 if $nl =~ s/^([^:]*):?(\d*)?//;
     $self->{'port'}     = $2 if $2;