Changes

Tips and Tricks With Perl

1,633 bytes added, 15:50, 13 April 2008
The following lines were added (+) and removed (-):
_______________________________________________________________________________ _______________________________________________________________________________----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.----Make the PC speaker "bell" sound echo "^G";The carrot-G cannot be typed literal, do the following:  Press CTRL-V then CTRL-G ----You can execute a line of Perl code from the command shell (from sh) without creating a Perl program script.  Example:  perl -e 'print "Hello World!\n"'From sh to perl back to sh via backticks  perl -e 'print `echo @ARGV`' a b c----Show the time and date in this format: 12:15 04/12/2008  <nowiki>my $datestring = prettydate(); </nowiki>  <nowiki>print $datestring; </nowiki>  <nowiki></nowiki>  <nowiki># usage: $string = prettydate( [$time_t] ); </nowiki>  <nowiki># omit parameter for current time/date </nowiki>  <nowiki>sub prettydate { </nowiki>  <nowiki>  @_ = localtime(shift || time); </nowiki>  <nowiki>  return(sprintf("%02d:%02d %02d/%02d/%04d", @_[2,1], $_[4]+1, $_[3], $_[5]+1900)); </nowiki>  <nowiki>}</nowiki>----Different ways to clear the terminal screen print "\033[2J"; print "\e[2J"; print `clear` , "\n"; system(($^O eq 'MSWin32') ? 'cls' : 'clear'); use Term::Screen; my $terminal = new Term::Screen; $terminal->clrscr(); use Curses; initscr(); refresh(); print " " x 80*25; print "\n" x 60;&nbsp;&nbsp;
Bureaucrat, administrator
16,195
edits