Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7490875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:46:53+00:00 2026-05-29T15:46:53+00:00

I need to handle some HTTP URLs in a Perl program, but I have

  • 0

I need to handle some HTTP URLs in a Perl program, but I have doubts how should the URI class help me.

Particularly, I’d like the to use the URI class for resolving relative URLs and getting their components. However, the problems are:

  1. I need a function to work with both URI objects and URI strings as arguments (or ensure only one gets passed)

    sub foo_string_or_url {
      my $uri = URI->new(shift);
    

    is that the right approach? I don’t quite like it, because it stringifies the URI and creates new object unnecessarily.

  2. Extract the components

    my $host = $uri->host;
    

    This is also problematic, because not all URIs have host, particularly, if someone passes garbage to the function, this will die().

  3. Resolve a relative URL

    my $new_url = URI::URL->new($uri, $base)->abs;
    

    IIUC, without the ->abs, the result will still stringify to the relative URL (and will not work for HTTP::Requests), am I right? Also, is this guaranteed to return a URI?

How should I handle these problems? The possibilities are

  • Use ->isa('URI') and ->can("host") all the time
    • Seems error prone and ugly to me
  • Don’t use URI class at all and parse URLs using regexes
    • I’d still rather use a library solution than debug my own
  • Wrap URI operations in try { ... } catch { ... }
    • see the first point

Is there a sane, fool-proof way of using the URI classes? Something simple I haven’t thought of (in the list above)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T15:46:54+00:00Added an answer on May 29, 2026 at 3:46 pm

    I think your question can be summarised: parameter validation is tedious, what do I do about it?

    1. I don’t like it, either. This is a matter of differing opinion among developers, other say coercions are better than sliced bread, especially when automatically done by Moose. I argue that allowing only one type of simplifies the program. Also, YAGNI applies in the vast majority of cases. Reject wrong types, employ a helper module such as Params::Validate/MooseX::Method::Signatures/MooseX::Declare in order to avoid the manual checks as shown in your code samples.

    2. This is the desired behaviour. Exception handling mechanisms let you write custom code appropriate for each situation. If you think it’s not aesthetically pleasing, remove it and mind the consequences of letting exceptions go unchecked.

      use Try::Tiny;
      my $host;
      try {
          $host = $uri->host;
      } catch {
          warn "Could not determine host for $uri. Message was: $_. Retry/abort/ignore?\n";
          …
      };
      
    3. Yes and yes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some expect advice on how to handle the following:- I have a
I need to redirect some urls. Formerly I had http://mysite.com/foobar/?section_id=1234 These are no pointing
I need to handle HTTP URLs that begin with a certain domain with my
I am writing some Ruby code, not Rails, and I need to handle something
I have some code in my error handler I need to test against a
I have a jQuery grid with data with user data. I need to handle
I desperately need some help on this one. I've created a <script> that closely
I have a project where I am taking some particularly ugly live HTML and
I use DotnetOpenAuth, i need to pass some data with SetCallbackArgument, but the problem
I seem to be in need of some help (and by some, I mean

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.