develooper Front page | perl.libwww | Postings from November 2002

Re: URI Bug?

Thread Previous | Thread Next
From:
Sean M. Burke
Date:
November 24, 2002 23:56
Subject:
Re: URI Bug?
Message ID:
5.1.0.14.1.20021125003621.0260f020@mail.spinn.net
At 14:56 2002-11-24 -0800, Randal L. Schwartz wrote:
>>> I'd be happy to learn of a better way to do this, and I'm sure my list
>>> is not complete anyway.
>Gisle> I suggest either:
>Gisle>     $url->host(ip_address($url->host)) if $url->can("host");
>Why bother with "can"?  Don't they all inherit from a common base class?
>Just put a "host" in the common base class that returns itself.

I've been thinking about that for a while (and I bet Gisle's been
thinking about it for a long while longer).  There's arguments both
ways.

I don't know The Answer to whether non-applicable URI-subclass methods
should be provided as no-ops. But what follows is a compromise; call it
URI/Forgiving.pm, and calls to such non-applicable methods is a no-op,
but generates a warning if $^W is true (as set by perl -w).
It's a hack, but maybe someone will find it useful.  What do folks think?

require 5;
package URI::Forgiving;
use URI  ();
use Carp ();
use strict;
#use warnings;

use vars qw(%Ignorables $VERSION);
$VERSION = 1.01;
foreach my $methodname (qw(
  abs authority canonical host host_port password path path_query
  path_segments port query query_form query_keywords rel userinfo user
)) {
  $Ignorables{$methodname} = 1 unless exists $Ignorables{$methodname};
}

{
package URI;
use vars qw($AUTOLOAD);

sub AUTOLOAD {
  my $method_name = substr($AUTOLOAD, rindex($AUTOLOAD, '::')+2);
  return if $method_name eq "DESTROY";

  my $self = $_[0];
  if( !ref $self ) {
    Carp::croak( sprintf
      q{Can't locate class method "%s" via package "%s"},
      $method_name, $self
    );
  } elsif( $URI::Forgiving::Ignorables{$method_name} ) {
    Carp::carp( sprintf
      qq{Ignoring call to method "%s" via "%s"'s class %s},
       $method_name, $self->as_string, ref($self),
    )
     if $^W;
  } else {
    Carp::croak( sprintf
       q{Can't locate object method "%s" via package "%s" (URI: "%s")},
       $method_name, ref($self), $self->as_string,
    );
  }
  return;
}

}

1;
__END__



--
Sean M. Burke    http://search.cpan.org/author/sburke/


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About