Download presentation
Presentation is loading. Please wait.
1
Perl Web Page – Just Enough
Pepper
2
Web site Set up the top of your script to indicate perl and plain text
#!/usr/bin/perl print "content-type:text/html; charset=utf-8\n\n"; You can put html tags which the browser will understand, or not Ensure that others can read and execute: chmod o+rx yourfile.pl Web address Must be under public_html
3
HelloWorld perl on internet
Print HTML code to the browser: #!/usr/bin/perl print "content-type:text/html; charset=utf-8\n\n"; print "hello world!";
4
HelloWorld perl on internet
Print HTML code to the browser more cleanly: #!/usr/bin/perl print "content-type:text/html; charset=utf-8\n\n"; print <<ENDHTML; <html> <head> <title>CGI Test</title> </head> <body> Hello World! <a href=" Here</a> </body> </html> ENDHTML
5
Taking in parms Use an html form to prompt for input
Change our script to use CGI Print out a form section of html that will run your own script <form method="post" action="myscript.pl"> <input type="test" name="input" /> <input type="submit" value="Submit" /> </form> Retrieve from the the search word from the form
6
HelloWorld perl Adding input
#!/usr/bin/perl print "content-type:text/html; charset=utf-8\n\n"; use strict; use CGI qw/:standard/; print <<ENDHTML; <html> <head> <title>CGI Test</title> </head> <body> <form method="post" action="myscript.pl"> <input type="test" name="input" /> <input type="submit" value="Submit "/> </form> Hello World! <a href=" Here</a> </body> </html> ENDHTML
7
Grab the input before it is cleared
create a variable to hold the input set it to the form field that is being resubmitted my $search = param("input")||""; Add an if statement to display the search word when it is found
8
Adding input value #!/usr/bin/perl print "content-type:text/html; charset=utf-8\n\n"; use strict; use CGI qw/:standard/; my $search = param("input")||""; print <<ENDHTML; <html> <head> <title>CGI Test</title> </head> <body> <form method="post" action="myscript.pl"> <input type="test" name="input" /> <input type="submit" value="Submit "/> </form> Hello World! <a href=" Here</a> ENDHTML if ($search) { print qq(<br />the search word is $search<br />); } print qq(</body> </html>)
9
Use a function to hold more content
Create a function with sub functionname { } Call a function with functionname()
10
Using Functions #!/usr/bin/perl print "content-type:text/html; charset=utf-8\n\n"; use strict; use CGI qw/:standard/; my $search = param("input")||""; print <<ENDHTML; <html> <head> <title>CGI Test</title> </head> <body> <form method="post" action="myscript.pl"> <input type="test" name="input" /> <input type="submit" value="Submit "/> </form> Hello World! <a href=" Here</a> ENDHTML if ($search) { print qq(<br />the search word is $search<br />); } searchdisplay(); print qq(</body> </html>); sub searchdisplay { print qq(This is the search display and the search word is $search);
11
Link to Display a file place the .txt file in the same folder as your page use relative path use link <a href="the link">words to click</a> Ex: #!/usr/bin/perl print "content-type:text/html; charset=utf-8\n\n"; print <<ENDHTML; <html> <head> <title>CGI Test2</title> </head> <body> <a href="myfile.txt">show myfile.txt</a> </body></html> ENDHTML
12
Read a file in the script
place the file in the same folder pwd to see full path and use that # employees whose names end in 'son' print "content-type:text/html; charset=utf-8\n\n"; print qq(<HTML><BODY> ); open(EMPLOYEES, "/home/pe16132/public_html/perl/employees.txt") || die "Can't open employees $!"; print "Names that end in 'son'<br><br>"; #>>> Loop to read and process the employee data while ($line = <EMPLOYEES>) { chomp($line); #>>> Split the input line into its four parts ($name, $age, $dept, $salary) = split(/,/, $line); if ($name =~ /.*son$/){ print "$name ends in son <br>"; # note that br replaces \n } print qq(</BODY></HTML>);
13
Script changes to retrieve results
print "content-type:text/html; charset=utf-8\n\n"; use strict; use CGI qw/:standard/; my $search = param("input")||""; print qq(<!DOCTYPE html><head></head><body> <form method="post" action="testinput4.pl"> <label for="input">What is the search word: </label><input type="text" name="input" value="" /> <input type="submit" value="Post Input" /></form>); if ($search) { print qq(<br />the search word is $search<br />); yourpage(); } print qq(</body></html>); sub yourpage { print "do whatever you want in your page here <br />"; print "and you can use $search <br />"; print "\n does nothing "; print "and you need to but br inside angle brackets to get the tne next line <br />";
14
Summary Web Create a perl script that outputs html code
First line: print "content-type:text/html; charset=utf-8\n\n"; Use CGI and form to make the input box instead of <STDIN> Make links to text files using <a href="yourfile">txt</a> Make use of functions to organize Read files with full file path Place script in public_html Set permissions (chmod o+rx your script.pl) Run as dir>/<yourscript>
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.