Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

URL $ENV variables

by bodmin (Acolyte)
on Aug 04, 2003 at 08:26 UTC ( #280596=perlquestion: print w/replies, xml ) Need Help??

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

Hi,

If my url is http://www.perlmonks.com/index.pl?node=Seekers, I am trying to get my script name, i.e. index.pl.

I use $ENV{REQUEST_URI} to get the current url.

I have some params attached to the url which I access via $ENV{QUERY_STRING}.

So in order to get index.pl (as in the example above) I use the following:

$attach1 = $ENV{REQUEST_URI}; if($attach1 =~ /\\/){ my @filename_parts = split /\\/,$attach1; my $length = scalar(@filename_parts); $attach1 = $filename_parts[--$length]; } if($attach1 =~ /\//){ my @filename_parts = split /\//,$attach1; my $length = scalar(@filename_parts); $attach1 = $filename_parts[--$length]; }

The above gives me index.pl?node=Seekers, half way there.

To strip off ?node=Seekers I use the following:

$paramstr = "\?".$ENV{QUERY_STRING}; $attach1 =~ s/$paramstr/;

And that is where my code breaks. How do I strip off the query string so I am left with index.pl?

Replies are listed 'Best First'.
Re: URL $ENV variables
by sgifford (Prior) on Aug 04, 2003 at 08:43 UTC
    I think you simply want:
    $attach1 =~ s/\?.*$//;
    But you seem to be doing this in an awfully complicated way, and your parsing code is fragile (it will fail on:
    http://www.perlmonks.com/index.pl?node=Smokers/Jokers
    , for example) Have you considered the URI module?

    Update: I see why you'd want to use $ENV{QUERY_STRING}. The problem with your code is that the double-quotes are eating the backslash---by the time the regex sees the question mark it's unbackslashed, and so doesn't make any sense. Single quotes won't do that (at least not with a question mark). You're also missing the replacement string on your regex. This code works for me:

    $paramstr = '\?'.$ENV{QUERY_STRING}; $attach1 =~ s/$paramstr//;
    But it fails if there are slashes in the query part of the string.
Re: URL $ENV variables
by antirice (Priest) on Aug 04, 2003 at 08:29 UTC

    Check out $0. For more information, perldoc perlvar.

    antirice    
    The first rule of Perl club is - use Perl
    The
    ith rule of Perl club is - follow rule i - 1 for i > 1

      I tried that but couldn't figure it out. there is no reference as to which $ENV variables there are in perl.

        Check out this script for what environment variables are set. It's nice for debugging, but don't keep it on your server.

        #!/usr/bin/perl -w use CGI qw(header start_html end_html); print header(),start_html(); print "$_ = $ENV{$_}<br>" foreach (keys %ENV); print "\$0 is $0"; print end_html();

        Of course, you could just use CGI and use its url() or script_name() methods. For more on the syntax for the url() method, check out this link.

        antirice    
        The first rule of Perl club is - use Perl
        The
        ith rule of Perl club is - follow rule i - 1 for i > 1

        That's because $ENV is decided by the enviornment the script is running in. See antirice's post above this. Note that $ENV is different depending upon your server OS, and whatever else happens to be floating around on the system. Also note that there is a lot more stuff in $ENV when running a script on a webserver, then just running it on the command line. Printing all the keys in the enviornment you will use is basically the only way to be sure you know what's available.
Re: URL $ENV variables
by snowcrash (Friar) on Aug 04, 2003 at 08:38 UTC
    Your code probably breaks because there's a syntax error in your substitution, you are missing on slash at the end. It should rather be $attach1 =~ s/$paramstr//;

    Anyway, I'd make use of the CGI modulue and its url() method.
    use CGI; my $q = new CGI; my $url = $q->url(-absolute => 1);

    regards, snowcrash
Re: URL $ENV variables
by jsprat (Curate) on Aug 04, 2003 at 08:47 UTC
    Is this is a standard CGI? If so, have a look at $ENV{SCRIPT_FILENAME} and $ENV{SCRIPT_NAME}. SCRIPT_FILENAME gives the absolute path to the running script and SCRIPT_NAME gives the virtual path to the script, both sans query string.

    If you want to see all environment variables available to your script, create a script that loops through all the keys of %ENV and prints all key/value pairs.

    HTH

Re: URL $ENV variables
by naChoZ (Curate) on Aug 04, 2003 at 12:54 UTC
    You can do it with the URI module pretty easily:

    #!/usr/local/bin/perl -w use strict; use URI; my $url = 'http://www.example.com/foo/bar.pl?baz'; my $u1 = URI->new($url); my @seg = $u1->path_segments; print "file: $seg[-1] \n";

    --
    "I just read perlman:perlboot," said Tom, objectively.
    naChoZ

Re: URL $ENV variables
by chanio (Priest) on Aug 05, 2003 at 02:00 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://280596]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2020-10-25 09:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (249 votes). Check out past polls.

    Notices?