Home: Perl Programming Help: mod_perl:
Extra desktop/mobile selector is appearing while browsing



ndhaundiyal
New User

Aug 11, 2014, 2:24 AM


Views: 94599
Extra desktop/mobile selector is appearing while browsing

Hi, i have a perl module that detects the device and adds an extra desktop/mobile selector accordingly (test.html -> test.desktop.html). Now, whenever I am moving from one page to another and extra selector is appending (test.desktop.html -> test2.desktop.desktop.html) and because of which I am getting error due to non-existing pages. I want my code to be modified so that if a selector is already present it must not append it again. Given below is the portion doing the above-mentioned task :-


Code
if ($model_group) { 
splice @uri_elements, 1, 0, $model_group;
my $new_uri = join('.', @uri_elements);
debug("New URI: ", $new_uri);
$r->uri($new_uri);
}



Zhris
Enthusiast

Aug 14, 2014, 12:09 PM


Views: 94476
Re: [ndhaundiyal] Extra desktop/mobile selector is appearing while browsing

One way could be to put a condition around the splice.


Code
splice @uri_elements, 1, 0, $model_group unless @uri_elements[1] eq $model_group;


This ensures the splice isn't performed if the second uri element is equal to the model group. A regexp may be more suitable if you need to check against multiple model groups:


Code
splice @uri_elements, 1, 0, $model_group unless @uri_elements[1] =~ /^(mobile|desktop)$/;


Lastly, the condition could be better positioned in your code, but I didn't want to hinder the rejoining of the uri elements / updating the uri, inevitably I am uncertain of your intentions beyond this specific problem.

Chris


(This post was edited by Zhris on Aug 14, 2014, 12:23 PM)


ndhaundiyal
New User

Aug 20, 2014, 9:17 PM


Views: 93880
Re: [Zhris] Extra desktop/mobile selector is appearing while browsing

@Zhris : I modified the slice statement but that doesn't seems to be working for me. Please let me know if you need further inputs from my end to assist me better on this?


BillKSmith
Veteran

Aug 21, 2014, 6:33 AM


Views: 93789
Re: [ndhaundiyal] Extra desktop/mobile selector is appearing while browsing

Perhaps you have to check all the @uri_elements,


Code
use List::Moreutils qw(none); 
#
#
if (none {$_ eq $model_group} @uri_elements) {
splice @uri_elements, 1, 0, $model_group;
}

Good Luck,
Bill