[Courses] [Perl] Part 6: The "m//" Operator

Katie Bechtold katie at katie-and-rob.org
Thu Aug 14 10:00:29 EST 2003


On Fri, Aug 08, 2003 at 12:48:50PM +0200, Dan Richter wrote:
> Write a Perl program that uses "m//" to split a URI into protocol, authority
> and path, or outputs a message saying that you gave it a URI that it can't
> parse.

Here's my answer (written before I looked at any of the offered
answers):

#!/usr/bin/perl -w                                                                                                     
use strict;   

print "Input a URI: ";
chomp(my $uri = <STDIN>);

if ( $uri =~ /([^:]+):\/\/(([^\/]+))(\/.*)/ ) {                                                                          
     
    my $protocol = $1;  
    my $authority = $3;
    my $path = $4;

    print "Protocol: " . $protocol . "\n";
    print "Authority: " . $authority . "\n"; 
    print "Path: " . $path . "\n";
}     
else {
    print "Invalid URI.\n";
}

-- 
Katie Bechtold         http://katie-and-rob.org/

Backward conditioning:
	Putting saliva in a dog's mouth in an attempt to make a bell ring.


More information about the Courses mailing list