lapsan has asked for the wisdom of the Perl Monks concerning the following question:

Hi. I'm a newbie. I need help. :)

I have a script that reads the first line in a file (a department name) and stores it in $department[0]. When $deptartment[0] is passed via GET it appears in the URL as follows:

www.example.com/script.pl?department=Customer Service

I want to be able to use what is passed (Customer Service) and display it as such in HTML (i.e.

Customer Service

). However, it doesn't do that, it only displays the first word of the department name (i.e.

Customer

). Is it possible to read both words? Do I have to pass both words seperately? Thanks!

Replies are listed 'Best First'.
Re: Arrays: More than one word
by btrott (Parson) on Apr 12, 2000 at 23:25 UTC
    This really doesn't have to do with arrays but rather with URLs and how they work. In order to pass along a space in a URL, you have to encode it; then your CGI script (or whatever) needs to decode it back into a space. If you're using CGI.pm (you probably should be), this decoding happens automatically. The *encoding*, on the other hand, is something you have to take care of yourself. It's easy, though:
    use URI::Escape; $department[0] = "Customer Service"; my $url = "http://www.example.com/script.pl?department=" . uri_escape($department[0]);
    If you don't have URI::Escape, just use this to encode spaces:
    $department[0] =~ tr/ /+/;
    A space can be encoded as either a '+' or '%20'; I'm not sure if the former is actually in the spec or not, but it works.
Re: Arrays: More than one word
by doran (Deacon) on Apr 13, 2000 at 03:26 UTC
    If you're using CGI.pm, it has URI (as well as HTML) encoding and decoding included, though you have to request them by name. From Master Stein's page:
    
    escape(), unescape()
    
         use CGI qw/escape unescape/;
         $q = escape('This $string contains ~wonderful~ characters');
         $u = unescape($q);
    
    
    http://stein.cshl.org/WWW/software/CGI/cgi_docs.html
RE: Arrays: More than one word
by Anonymous Monk on Apr 13, 2000 at 16:59 UTC
    you need to use this www.example.com/script.pl?department=Customer+Service , the '+' denotes a space in a url, (you will need to parse this). If you need some help on parsing feel free to drop me a line, (I'm a bit of a Perl newbie too ;). Nathan Vidican webmaster@wmptl.com Windsor Match Plate & Tool Ltd. http://www.wmptl.com/
RE: Arrays: More than one word
by Anonymous Monk on Apr 13, 2000 at 19:42 UTC
    Strictly speaking, URLs should never have spaces in them; it causes no end of problems. CGI submissions convert spaces to plus signs. Just go with the flow and do a tr/+/ /g before you display the grabbed name.