Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Stripping part of URL

by jeffa (Bishop)
on Mar 24, 2015 at 15:29 UTC ( [id://1121177]=note: print w/replies, xml ) Need Help??


in reply to Stripping part of URL

Just another way to do it:

use strict; use warnings; use URI; my $uri = URI->new( $ENV{REQUEST_URI} ); my $page = ($uri->path_segments)[-2];
The problem with this code is that the return value of URI::path_segments() changes if the URI contains a trailing slash or not. You could improve the robustness at the expense of added complexity by using map to squash the return of URI::path_segments() however:

my $page = (map $_ ? $_ : (), $uri->path_segments)[-1];
Kinda defeats the purpose of using methods from a module however. ;)

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)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1121177]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (1)
As of 2024-04-25 19:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found