Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Can the "#" character be passed via $ENV{QUERY_STRING} ?

by Hagbone (Monk)
on Feb 24, 2005 at 23:18 UTC ( #434301=perlquestion: print w/replies, xml ) Need Help??

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

Oh Wise Ones ...

I've searched here and elsewhere to no avail .... I'm wondering if there is way to include the "#" (pound) character in a QUERY_STRING in a way that it will be passed to the script.

In other words, I have a link that is structured:

<a href="">CLICK</a>
And prints the QUERY_STRING using:
print "=> $ENV{'QUERY_STRING'}";
The script prints up to the "#" character and then stops:


I've managed to get things working by altering the "#" character in the link and then substituting once the script reads in the QUERY_STRING, but the nagging issue of whether there is a way to pass the "#" character in a QUERY_STRING remains, and I'm hoping for some insight (and also hoping that there isn't an obvious solution that I've embarrassingly overlooked).

Replies are listed 'Best First'.
Re: Can the "#" character be passed via $ENV{QUERY_STRING} ?
by ikegami (Patriarch) on Feb 24, 2005 at 23:22 UTC

    The character is reserved to delimit the anchor part of the URL. Web client must not send the anchor as part of an HTTP request, which is why you don't see it. You can include the character in a URL by encoding it. The encoded value is "%23" (or use URI::Escape to do it on the fly). CGI automatically decodes it in your script.

Re: Can the "#" character be passed via $ENV{QUERY_STRING} ?
by exussum0 (Vicar) on Feb 24, 2005 at 23:24 UTC
    That is because the # is a URI delimieter for HTTP URIs. You need to url encode it. URI::Escape can do it.

    Give me strength for today..
    I will not talk it away.. Just for a moment..
    It will burn through the clouds.. and shine down on me.

Re: Can the "#" character be passed via $ENV{QUERY_STRING} ?
by Tanktalus (Canon) on Feb 24, 2005 at 23:21 UTC

    In a word? No. The # character delimits to the client-side browser where the remote part of the URL stops, and where the anchor to look for starts.

    The only way to get that info is to add it before the hash mark, as well as having it after the hash mark:

    <a href="">
    I really don't think that this should be useful - I'm having a hard time figuring out why you'd want to do this anyway.

Re: Can the "#" character be passed via $ENV{QUERY_STRING} ?
by cowboy (Friar) on Feb 24, 2005 at 23:26 UTC
    The # sign, is used for navigation within the page. It is not passed by the browser. In your example, it is saying, load:, then look for an anchor called second. See:
    <a href="#second">second</a> ... very long page ... <a name="second">Link above should bring the browser here</a>
Re: Can the "#" character be passed via $ENV{QUERY_STRING} ?
by Hagbone (Monk) on Feb 24, 2005 at 23:34 UTC
    >You need to url encode it

    Forgot to include that I've tried using %23 in place of the "#" character, but the enocding appears to be interpreted literally, and the the QUERY_STRING print "%23".

    I believe I understand the anchor issue .... just trying to find if there's an easy way out that would allow me to include the # character ;) ... laziness as a virtue kinda thing.

      Of course %23 appears literally in QUERY_STRING. The recommendation was to use CGI to read your query, and CGI automatically decodes these fields. Doing CGI without using CGI or CGI::Simple is simply asking for problems, and this is probably among the least of those problems.

Re: Can the "#" character be passed via $ENV{QUERY_STRING} ?
by Hagbone (Monk) on Feb 25, 2005 at 00:07 UTC
    Apologies, Tanktalus, about the literal interpretation issue .... the reason I've pursued this item is that I've got this very, very lightweight script that only needs the raw QUERY_STRING info to do it's thing (no parsing of values, etc.), and the only hitch is that the info being passed needs to include the # character. Firing up CGI to deal with this one item is making me feel resource greedy.

    I guess I'll stick with including the few lines that substitute "#" for an alternate character passed in the Q_S.

    Appears I'm at the point of beating a dead horse .... just thought there might be some way to do the deed with even less code or resources.

      You don't need to load all of CGI to do this if you are taking the whole QUERY_STRING as a single value. You can use URI:Escape directly. It is an extremely lightweight module. You'll want to use uri_unescape, like so:
      use URI::Escape qw(uri_unescape); my $query_string = uri_unescape $ENV{QUERY_STRING};
        Just to note, uri_unescape is functionally:  s/%(\d\d)/chr $1/eg; if you don't even want to load the module.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://434301]
Approved by Tanktalus
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2023-02-04 17:53 GMT
Find Nodes?
    Voting Booth?
    I prefer not to run the latest version of Perl because:

    Results (31 votes). Check out past polls.