Last updated on November 11th, 2024 at 02:12 pm

Websites sometimes need to send users to different places. For instance, in a Perl application, after a user logs in, if they are part of a certain group, they can be directed to the appropriate dashboard using Perl’s features.

Here is the code.

#!/usr/bin/perl

# Define a URL variable
my $my_location = 'https://webtutorials.dev/';

# Print the location as part of a header-like output
print "Location: $my_location\n\n";

Shebang Line #!/usr/bin/perl:

  • This line tells the system that this script should be run using the Perl interpreter located at /usr/bin/perl.

Variable Definition:

  • $my_location is assigned the URL 'https://webtutorials.dev/'.

Print Statement:

  • print "Location: $my_location\n\n"; outputs the Location: header followed by the URL, with two newline characters (\n\n) at the end.
  • In an HTTP response, this format (Location: <URL>) would serve as a redirection header, though here it’s simply printed as text.

Sample Output

$ curl -IL http:///mywebsite/cgi-bin/redirect.pl
HTTP/1.1 302 Found
Date: Fri, 18 Feb 2022 02:51:31 GMT
Server: Apache/2.4.41 (Ubuntu)
Location: https://webtutorials.dev/
Content-Type: text/html; charset=iso-8859-1

HTTP/2 200
date: Fri, 18 Feb 2022 02:51:31 GMT
content-type: text/html; charset=UTF-8
vary: Accept-Encoding
wpo-cache-status: not cached
wpo-cache-message: The request method was not GET (HEAD)
nel: {"success_fraction":0,"report_to":"cf-nel","max_age":604800}
server: cloudflare
cf-ray: 6df3f43fc9f98ce9-EWR
alt-svc: h3=":443"; ma=86400, h3-29=":443"; ma=86400