how to include URL in perl code

  • Hiei
  • Beginner
  • Beginner
  • Hiei
  • Posts: 51

Post 3+ Months Ago

in my code i want the user to be able to specify a name (ozzu for example) and they will be redirected to this site, or any other site thats in the program. i tried saying something like this.

Code: [ Select ]
if ($site eq ozzu) { http://www.ozzu.com/index.php );

but doesn't work, i googled it and found nothing that would tell me the code of how to do this. thanks for your help[/url]
  • this213
  • Guru
  • Guru
  • User avatar
  • Posts: 1260
  • Loc: ./

Post 3+ Months Ago

unless all of the sites you're trying to link to have some similarity in their names, you're going to have to create some system of keeping track of which names to translate into which address. The easiest way would probably be to create a file as such (we'll call it sites.dat):
Code: [ Select ]
ozzu|http://www.ozzu.com/index.php
cnn|http://www.cnn.com
google|http://www.google.com
  1. ozzu|http://www.ozzu.com/index.php
  2. cnn|http://www.cnn.com
  3. google|http://www.google.com

Then in you code, just do:
Code: [ Select ]
open(FILE,"sites.dat");
@sitelist = <FILE>;
close FILE;
chomp @sitelist;
foreach $el (@sitelist){
    my($title,$location) = split(/\|/,$el);
    if($title eq $site){
        $urlToUse = $location;
    }
}

# Rest of your code goes here
  1. open(FILE,"sites.dat");
  2. @sitelist = <FILE>;
  3. close FILE;
  4. chomp @sitelist;
  5. foreach $el (@sitelist){
  6.     my($title,$location) = split(/\|/,$el);
  7.     if($title eq $site){
  8.         $urlToUse = $location;
  9.     }
  10. }
  11. # Rest of your code goes here

If you don't have a huge list of sites, or don't plan to, you can also just assign the titles to urls in a hash, as such:
Code: [ Select ]
%urlhash = (
    'ozzu' => 'www.ozzu.com/index.php',
    'cnn' => 'www.cnn.com',
    'google' => 'www.google.com');

# then to find which one you want
foreach $el (keys %urlhash){
    if($el eq $site){
        $urlToUse = "http://$urlhash{$el}";
    }
}
  1. %urlhash = (
  2.     'ozzu' => 'www.ozzu.com/index.php',
  3.     'cnn' => 'www.cnn.com',
  4.     'google' => 'www.google.com');
  5. # then to find which one you want
  6. foreach $el (keys %urlhash){
  7.     if($el eq $site){
  8.         $urlToUse = "http://$urlhash{$el}";
  9.     }
  10. }


hth
This
  • Sabu
  • Beginner
  • Beginner
  • Sabu
  • Posts: 47

Post 3+ Months Ago

I'd be inclined to read it as "how do I actually go about redirecting the user?"

Your examples there are very good as far as I can tell (I don't know much perl) but where's the part to take the variable $location and move the user to that link?

So I put my google skills to the test and it coughed up

Code: [ Select ]
print redirect(-location=>'http://exact-url-here');


Which seems to have to be printed before anything else on the page. Excluding headers. Perhaps someone here could verify this whoe linking thing for me, and I'll go back to PHP
  • this213
  • Guru
  • Guru
  • User avatar
  • Posts: 1260
  • Loc: ./

Post 3+ Months Ago

Code: [ Select ]
print qq~<script>javascript:void(document.location="$location")</script>~;


Sorry, wasn't aware that's what you were looking for.
  • Hiei
  • Beginner
  • Beginner
  • Hiei
  • Posts: 51

Post 3+ Months Ago

thanks for your help but i have still have a couple questions.
is this213's code the one i am looking for? if so then i have a couple of questions, is "$location" the site name which i want to put in? Also what is <scipt> do in this? thanks again for your help
Code: [ Select ]
print qq~<script>javascript:void(document.location="$location")</script>~;
  • this213
  • Guru
  • Guru
  • User avatar
  • Posts: 1260
  • Loc: ./

Post 3+ Months Ago

in this instance I was assuming $site to contain the title of the site to be converted to a url, so if your form has an element named "site", you would:
Code: [ Select ]
use CGI;
$site = $form->param('site');
  1. use CGI;
  2. $site = $form->param('site');

to set it.

The <script> tag in this case changes the location of the web page you're viewing - in other words, it redirects. How you work this into your code is up to you - if you even use it at all, but its an option. You could probably streamline this a bit more by adding the sites into an combobox list, so the user woulld just see the title and select it - then a javascript to redirect according to what was picked. Again, just another idea.

Yet another idea would be to not use perl at all to accomplish this, you could just make a form like so that would work:
Code: [ Select ]
<form name="thesite">
<select name="sites">
<option value="http://www.ribosi.com">Ribosi
<option value="http://www.yzix.org">YZIX
<option value="http://www.ozzu.com">Ozzu
<option value="http://www.cnn.com">CNN
</select>
<input type="button" name="select" value="Submit" onclick="javascript:void(document.location=document.thesite.sites.value);">
</form>
  1. <form name="thesite">
  2. <select name="sites">
  3. <option value="http://www.ribosi.com">Ribosi
  4. <option value="http://www.yzix.org">YZIX
  5. <option value="http://www.ozzu.com">Ozzu
  6. <option value="http://www.cnn.com">CNN
  7. </select>
  8. <input type="button" name="select" value="Submit" onclick="javascript:void(document.location=document.thesite.sites.value);">
  9. </form>


Then too, you could also have perl generate that option list from your data file or hash (from my other post)

Post Information

  • Total Posts in this topic: 6 posts
  • Users browsing this forum: No registered users and 107 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.