How to Change the Title Bar in Perl Script

How to Change the Title Bar in Perl Script thumbnail
The Perl programming language offers powerful features for dynamically altering web pages.

The Perl programming language is one of the most-used interpreted programming languages for the creation of dynamic Web pages. Using Perl, programmers can alter Web page user interfaces in many ways, for example, by changing specific HTML tags such as the “<title>” tag. Perl scripts run on the web server, so they require that data is posted to the web server in order to dynamically alter page elements. Fortunately, Perl provides built-in functions that make the retrieval of posted information easy.

Things You'll Need

  • text editor
  • Web server with Perl installed and properly configured
Show More

Instructions

    • 1

      Open a text editor and create a file named "perlTitle.pl". Add the “shebang” line to the file on the first line. The "shebang" specifies the Perl interpreter location in the event that it cannot be found. The "shebang" line will be different based on the specific Perl installation. An example is shown below:

      #!/usr/local/bin/perl

    • 2

      Indicate that perlTitle.pl should output a Web page ("text/html") by printing the “Content-type” value “text/html” (include the "\n\n" characters). To print output, use the Perl “print” command and print an open and a close “<html>” tag to the Web page.

      print "Content-type:text/html\n\n”;
      print “<html>”;
      print “</html>";

    • 3

      Retrieve any values in the query string. The “query string” values are any values submitted with the web page. In this example, the value in the query string will be the title used to change the title bar. Store the query string values in a variable called “$keyValue” declared between perlTitle.pl's "<html>" and "</html>" tags:

      $keyValue = ($ENV{'QUERY_STRING'});

    • 4

      Use the Perl “split” function to split the query string into two values ($key and $value), placing the split immediately below the "$keyValue" declaration. In this example, $key is the title string’s name (“title”) and $value is the title string’s value. Use the “=” character to split the “$keyValue” string:

      ($name, $value) = split(/=/, $keyValue);

    • 5

      Use the Perl “print” command to print the “$value” variable to the Web page. Enclose the variable's value between open "<title>" and close "</title>" HTML tags:

      print "<title>$value</title>";

    • 6

      Use the Perl “print” command to print a “<form>” tag to the Web page. Give the form the method type “get” and the action value “perlTitle.html”. Print a closing “</form>” tag to complete the form that the user will submit:

      print "<form method='get' action='perlTitle.pl'>";
      print “</form>”;

    • 7

      Use the Perl “print” command to print an HTML “<input>” field to the Web page. Give the input field the “type” value “text”, the “name” value “title” and close the “</input>” tag. Use the Perl “print” command to print a second HTML “<input>” to the web page with a “type” value “submit”. Place the two "print" commands between the "<form>" and "</form>" tags and save and close perlTitle.pl. After step 7, perlTitle.pl will appear as shown below:

      #!/usr/local/bin/perl
      print "Content-type:text/html\n\n";
      print "<html>";
      $keyValue = ($ENV{'QUERY_STRING'});
      ($name, $value) = split(/=/, $keyValue);
      print "<title>$value</title>";
      print "<form method='get' action='perlTitle.pl'>";
      print "<input type='text' name='title'></input>";
      print "<input type='submit'>";
      print "</form>";
      print "</html>";

    • 8

      Open perlTitle.pl in a web browser. Type a value in the input field, click the submit button, and verify that the Web page’s title updates correctly.

Tips & Warnings

  • Form information can be sent to Perl scripts using the “post” method. This method does not include the form’s data as arguments in the URL, and is more secure than the “get” method.

  • Perl has a variety of powerful text manipulation capabilities and a rapid development cycle.

  • CPAN offers thousands of Perl modules that allow developers to accomplish nearly any task without duplicating code.

  • Consider adding a default value to input fields that take user input in the event that the user does not enter any information.

  • Most browser attributes are based on user preferences and system settings and either cannot or should not be changed dynamically.

Related Searches:

References

Resources

  • Photo Credit Zedcor Wholly Owned/PhotoObjects.net/Getty Images

Comments

Related Ads

Featured