Difference between revisions of "Tips and Tricks With Perl"

From Free Knowledge Base- The DUCK Project: information for everyone
Jump to: navigation, search
m
Line 1: Line 1:
_______________________________________________________________________________
 
 
   
 
   
 
Put up a bunch of HTML without so many print statements...
 
Put up a bunch of HTML without so many print statements...
Line 11: Line 10:
 
                        
 
                        
 
:*note: The final end_html must begin in text column 1, fully left justified.
 
:*note: The final end_html must begin in text column 1, fully left justified.
 
_______________________________________________________________________________
 
  
 +
----
 +
 +
Perl has three predefined filehandles:
 +
* STDOUT - for standard output
 +
* STDIN - for standard input
 +
* STDERR - for error messages
 +
Thus, print and printf functions by default use STDOUT filehandle to print to. Knowing the filehandle for standard input we can read everything user types just by using STDIN like usual file:
 +
print "Please enter the file name: ";
 +
chomp( $fname = <STDIN> );
 +
printf(STDERR "You entered '%s'\n", $fname);
 +
The last line prints the confirmation to the standard output for error messages, which is by default the same as standard output.
 +
 +
----
 +
 +
&nbsp;
 +
 +
&nbsp;
  
  

Revision as of 09:59, 12 April 2008

Put up a bunch of HTML without so many print statements...

   print <<end_html;
     <center>
     <h2>Text Heading</h2>
     </center>
 end_html

                     
  • note: The final end_html must begin in text column 1, fully left justified.

Perl has three predefined filehandles:

  • STDOUT - for standard output
  • STDIN - for standard input
  • STDERR - for error messages

Thus, print and printf functions by default use STDOUT filehandle to print to. Knowing the filehandle for standard input we can read everything user types just by using STDIN like usual file:

print "Please enter the file name: ";
chomp( $fname = <STDIN> );
printf(STDERR "You entered '%s'\n", $fname); 

The last line prints the confirmation to the standard output for error messages, which is by default the same as standard output.