$count=10;
while ( $count!=0)
{
  print "$count...\n";
  --$count;
}
print "done!\n";

=====================================================


while ($_ = < ARGV >)
{
  print $_;
}

#same as unix:cat file1 file2

===============================================================


$pattern=shift(@ARGV);
while (< >)
{
 if (/$pattern/)   or if ($_ = ~/$pattern/)
   {print;}
}

-run it using: perl programname 'one.*one' file1
(prints lines containing tow copies of word "one")

====================================================


while(< >)
{
  chop;
  if($_ ne " ")
    {  print $_, "\n";}
}

or

while(< >)
{
  print unless $_ eq "\n";
}

(print non_blank lines)

====================================================


$pattern=shift(@ARGV);
while (< >)
{
 if (/$pattern/)   or if ($_ = ~/$pattern/)
   {print;}
}

invoked by:

perl prgm '(\w+)\s*=\s*\1' *.c

what about :

while(< >)
{
  $_= ~s/(\S+)\s+(\S+)/$2$1/;
  print;
}

=================================================================


while(< >)
{
  chop;
  if($_ ne " ")
    {  print $_, "\n";}
}

or

while(< >)
{
  chop;
  print $_,"\n" if $_ ne " ";
}

or

while(< >)
{
  print  unless /^$/;
}

or

while(< >)
{
 s/^\n$// ;
 print;
}

=====================================================


parse an email message:
find title and date:

Subject:title
Date: 19 Mar 97 4:30:26 CT

while(< >)
{
  if(/^Subject:(.*)/)    or  /^Subject:(.*)/&& ($subject=$1);
    { $subject=$1;}
  
  @array=/^Date:\s*(\d+)\s+(\w+)\s+(\d+)\s+(\d+):(\d+):(\d+)\s+(.*)$/&&
         ($mday=$1,
	  $month=$2,
	  $year=$3,
	  $hour=$4,
	  $minute=$5,
	  $second=$6,
	  $timezone=$7)
}

===========================================================


#keep track of dependencies among files by extracting info 
#from "#include" statements.

foreach $file(@ARGV)
{
  open(FILE, $file) || warn "can't open $file:$!\n";
  while(< FILE >)
  {
    if(/^#include\s+[" < ]([^" > ]*)[" <]/)
      {
	$included = $1;
	$includes{$file}.=$included.' ';
       }
   }
}

foreach $target(sort keys(%includes))
{
  $dependencies = $includes{$target};
  foreach $dependency(split(//,$dependencies))
   {
     print "$target:$dependency \n";
   }
}

===================================================


#!/usr/bin/perl
while(< >)
{
  print if /\bone\b/;
}

or

#!/usr/bin/perl -n
print if /\bone\b/;

or at command line:

perl -n 'print if /\bone\b/' file1

perl -P '' file1

============================================


#!/usr/bin/perl -P
s/\boen\b/one/g;

if prgm file is named correct

call it using:

correct file1 > file1.new

========================================

#!/usr/bin/perl -Pi.old
s/\boen\b/one/g;

call it using:

correct file1

========================================

#!/usr/bin/perl
$/="";
$*=1;
while(< >)
{
  s/-\n//g;
  print if /\btimely\b/;
}
=========================================

foreach  $i (0..$#ARGV)
{
  print $ARGV[$i];
  if ($i==$#ARGV)
  { 
    print "\n";
  }
  else
  {
    print "";}
}

Run program with command line arguments: perl programName arg1 arg2

What does it do?

===============================

Initializing arrays:

@array1=('one', 'two','three');
@array2=(1,2,3,4);
@array3=($a,$b,$c);

($a,$b,$c)=(0..2);

($a,$b)=split(/:/, <FILE>);   does what?

=========================================

what does following do, if invoke it by: perl prgmName *  


while (@ARGV)
{
  $file=shift @ARGV;
  push (@textfiles, $file) if -T $file;
}

print join (' ', @textfiles), "\n";

=========================================

Array Operators:
(what do they do?)


$a= shift(@abc);

$a= pop (@abc);

push (@abc, $b);

unshift(@abc, $a);

NOTE: $#array is the last index of array @array

Associative arrays:

%schedule=('monday','golf','tuesday','tennis','wednesday','swimming');

elements: $schedule{'tuesday'}

array slice: @schedule{'tuesday','wednesday'}
-----------------------------------------

scalar=expression; (the expression evaluates in scalar context)

array=expression; (the expression evaluates in array context)

Examples:

$n=@array;  is same as: $n=$#array+1;

@array=();     makes array empty(null list)
 is same as:
$#array=$[-1;  ($[ is a special variable which holds array base-usually 0)

---------------------------------------
LISTS
(list of values seperated by commas)

Notes:

1. In a non-array context, the value of a list is the value of last 
element:

@foo=('a', 'b', $a);
$foo=('a','b',$a);  is same as: $foo=$a;

2. In a non-array context, the value of an array is the length of the 
array:

@foo=('a','b',$a);
$foo=@foo;      same as: $foo=3;

3. When list is evaluated, each element is evaluated in an array 
context and resultin value interpolated into a list:

(@a,@b,&subroutine)


---------------------------------------

Given:

@vegetables=('potato', 'pepper', 'onion');

then what are values of following?

slicing: @vegetables[0,2]

sorting: sort @vegetables

reverse: reverse @vegetables
         reverse 2..4

select by pattern: grep(/io/, @vegetables)

=========================================

echo program:

foreach $i (@ARGV)
{
  print $space, $i;
  $space=' ';
}
print "\n";


OR:

foreach $i (0..$#ARGV)
{
  $ARGV[$i].=' ';
}
$ARGV[$#ARGV]=~s/$/\n/;
print @ARGV;

=========================================

What does following do, if invoked by:

perl prgmName '(\w+)\s*=\s*\1'  *.c

program:

$pattern=shift @ARGV;
while(<>)
{
  if (/$pattern/)
    {print;}
}

=========================================


What does following do, if invoked by:

perl prgmName  *.c

program:

while (<>)
{
  s/(\S+)\s+(\S+)/$2$1/;
  print;
}

=========================================

subroutines

&tryagain if !/children (\d+)/;
&tryagain if $1<=0;
if $1 &ge 5 {print "oh,boy!";}

sub tryagain
{
  print "incorrect input-try again\n";
}


&pickupline('beautiful','intelligent');
sub pickupline
{
  print "hi there,$_[0] and $_[1] girl \n";
}

arguments stored in @_

================================================

open(FIND,"find,-print|") || die "can't run find:$!\n";

FILE:
while($filename = < FIND >)
{
  chop $filename;
  next FILE unless -T $filename;
  if(!open(TEXTFILE, $filename)
  {
    print STDERR "can't open $filename \n";
    next FILE;
  }
  while(< TEXTFILE >)
  {
   foreach $word(@ARGV)
   {
     if(index($_,$word) &ge 0)
     {
	print $filename,"\n";
	next FILE;
     }
   }
 }
}

====================================================