Introduction    To    Perl   

Debugger:

     perl has a built-in debugging system.

     To use the debugger, all you have to do is add a -d to the
     first line of the program.

          #! /usr/local/bin/perl -d
          commands

     When you run the program, it will start in debug mode.  You
     then have many debugging commands you can use including:

          h         help on debugger
          s         step
          c         continue to next break
          c   continue until line 
          n         next (does not step into subroutines)
          l  list program statements in the 
          b   sets a breakpoint at line 
          p   prints  which is usually a variable

###########p1###########################################################
#p1 My first perl script

#!/opt/PUBperl/bin/perl

$myname = 'Ron Pursley';
$mycity = 'Waco, Texas';

print STDOUT "My name is $myname\n";
print STDOUT "I live in $mycity\n";

print STDOUT "\n\nEnd of example 1\n";


########p3############################################################
#!/opt/PUBperl/bin/perl

#Basic input example


print STDOUT "Input your name ";

$user=;

print STDOUT "\n\nThe name you input is $user\n";

print STDOUT "End of program $0 under PERL Version $]\n";


#########p5#############################################################
#!/opt/PUBperl/bin/perl
#Basic if example


print STDOUT "Enter a digit";

$digit=;


if ($digit == 0) {print STDOUT "\nYou entered a zero, that is $digit";}

print STDOUT "End of program $0 under PERL Version $]\n";


##############p6#########################################################
#!/opt/PUBperl/bin/perl
#Basic if then else example

print STDOUT "Enter a digit ";

$d=;

if ($d ge 0 && $d lt 10)
{
        print STDOUT "\nYou entered the number $d"; }
else {
        print STDOUT "\nYou did not enter a number 0-9";
        print STDOUT "\nYou entered a $d";
}

print STDOUT "\nEnd of program $0\n\n";


#############p7########################################################
#!/opt/PUBperl/bin/perl
#Basic while loop example


$i=1;

while ($i < 10) {
print STDOUT "\nThe current value of i is $i\n";}
continue {$i++;}

print STDOUT "\nEnd of program $0\n\n";

################p8#####################################################
#!/opt/PUBperl/bin/perl
#Basic foreach example

foreach $item (a,b,c,d)
{
print STDOUT "\nThe current value of item is $item\n";
}

print STDOUT "\nEnd of program $0\n\n";


######################p9################################################
#!/opt/PUBperl/bin/perl
#Basic do until loop example

do {
        $i=;
print STDOUT "\nThe key you hit was $i"; }
until $i eq "\n";


print STDOUT "\nEnd of program $0\n\n";


##################p10###################################################
#!/opt/PUBperl/bin/perl
#Basic system example


system("who");


print STDOUT "\nEnd of program $0\n\n";


########################p11##############################################
#!/opt/PUBperl/bin/perl
#Basic array example

$junk[0]='hi';
$junk[1]='there';
$junk[2]='yall';

print @junk;


print STDOUT "\nEnd of program $0\n\n";

#######################p12##############################################
#!/opt/PUBperl/bin/perl
#Basic array, split example


$i=system("who");
print $i;

@junk=split(/ /,$i);
print @junk;

print STDOUT "\nEnd of program $0\n\n";

#############p13#######################################################
#!/opt/PUBperl/bin/perl
#Basic read through file and process items example

$space=" ";
open(info, '/etc/passwd');

while () {
($login, $passwd, $uid, $gid, $cos, $home, $shell)=split(/:/);
print "$login $space";
}

print STDOUT "\nEnd of program $0\n\n";

################################p14###################################
#!/opt/PUBperl/bin/perl
#Basic array, join, split example

$junk[0]='hi';
$junk[1]='there';
$junk[2]='yall';

print @junk;
print "\n";
print join(' ', @junk);
print "\n";
print join('*', split(/ /,'here we go now'));
print "\n";

print STDOUT "\nEnd of program $0\n\n";

############p15#########################################################
#!/opt/PUBperl/bin/perl
#Basic Satus of file example
#p15 /etc/passwd

$file=$ARGV[0];

print "Print Status information for file $file\n";

($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
   $atime,$mtime,$ctime,$blksize,$blocks)=stat($file);

print "$dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
   $atime,$mtime,$ctime,$blksize,$blocks";


print STDOUT "\nEnd of program $0\n\n";

#################p16##################################################
#!/opt/PUBperl/bin/perl
#Basic system, split example

system("who>whofile");
system("cat whofile");

open(info, 'whofile');

while () {
   ($login, $term, $mon, $day, $time, $connect)=split;
print "$login $term $mon $day $time $connect";
print "\n";
}
system("rm whofile");

print STDOUT "\nEnd of program $0\n\n";


##########################p17#############################################
#!/opt/PUBperl/bin/perl
#Basic system, split example

system("last>lastfile");

open(info, lastfile);

while () {
   ($login)=split;
print "$login";
print "\n";
}
system("rm lastfile");

print STDOUT "\nEnd of program $0\n\n";


###################p18###################################################
#!/opt/PUBperl/bin/perl
#Basic parameter test example
#p18 this is a test

print "@ARGV";

print STDOUT "\nEnd of program $0\n\n";


#######################p18b#############################################
#!/opt/PUBperl/bin/perl
#Basic parameter test example

for (@ARGV) {
print "$ARGV[$x] is the value of Argument $x\n";
$x++;
}

$args=++$#ARGV;
print "\nTotal Arguments presented was $args\n";
print STDOUT "\nEnd of program $0\n\n";

#############p19#######################################################
#!/opt/PUBperl/bin/perl
#Count number files, directories, and links

system("ls -l > lsfile");

open(stuff, lsfile);

while () {

($info)=split;
if (substr($info,0,1) eq "-") { $filecnt++; }
if (substr($info,0,1) eq "d") { $dircnt++; }
if (substr($info,0,1) eq "l") { $linkcnt++; }
}
print "Number of files is $filecnt\n";
print "Number of directories is $dircnt\n";
print "Number of links is $linkcnt\n";

system("rm lsfile");

print STDOUT "\nEnd of program $0\n\n";

################p19b####################################################
#!/opt/PUBperl/bin/perl
#Count number files, directories, and links

system("echo * > entries");

open(stuff, entries);

@info=split(/ /,);

while($x<=$#info) {
if( -f $info[$x] ) { $filecnt++; }
if( -d $info[$x] ) { $dircnt++; }
if( -l $info[$x] ) { $linkcnt++; }
$x++;
}

print "Number of files is $filecnt\n";
print "Number of directories is $dircnt\n";
print "Number of links is $linkcnt\n";
system("rm entries");

print STDOUT "\nEnd of program $0\n\n";

################p19c###############################################
#!/opt/PUBperl/bin/perl
#Count number files, directories, and links

while(<*>) {
if( -f ) { $filecnt++; }
if( -d ) { $dircnt++; }
if( -l ) { $linkcnt++; }
}

print "Number of files is $filecnt\n";
print "Number of directories is $dircnt\n";
print "Number of links is $linkcnt\n";

print STDOUT "\nEnd of program $0\n\n";


###################################p20##################################
#!/opt/PUBperl/bin/perl
#Prints multiplication table

foreach $n (1..12) {

        foreach $j (1..12) {
        $k=$j*$n;
        print "$k ";
        if ($j eq 12) {print "\n";}     
        }
}

print STDOUT "\nEnd of program $0\n\n";


##########################p21##########################################
#!/opt/PUBperl/bin/perl
#Basic If Elsif Key entered test example

print "Hit a key ";
$key=;

while($key ne "\n") {
if      ($key ge "A" && $key le "Z") {
        print "big\n";  }
elsif   ($key ge "a" && $key le "z") {
        print "little\n"; }
elsif   ($key ge "0" && $key le "9") {
        print "number\n"; }
else    
        {print "key entered not little, big, or number\n";}

print "Hit a key ";
$key=;
}

print STDOUT "\nEnd of program $0\n\n";

############################numberlogs#################################
#!/opt/PUBperl/bin/perl
#
#Count number of logins for user determined from last information
#User specified by 1st parm
#numberlogs joe

system("last>lastfile");

open(info, lastfile);

while () {
        ($login)=split;
        if ("$login" eq "$ARGV[0]") { $logcnt++ };
}

print "@ARGV[0] has logged in $logcnt times";
system("rm lastfile");

print STDOUT "\nEnd of program $0\n\n";

#############numberlogs2############################################
#!/opt/PUBperl/bin/perl
#
#Count number of logins for user determined from last information
#Multiple persons specified via parms
#numberlogs2 joe mary sue

system("last>lastfile");

open(info, lastfile);

while () {
@lognames[$count++]=split;
}

foreach $user (@ARGV) {
$usercnt=0;
$work = -1;

while($work++ < $#lognames){
if ("$user" eq "$lognames[$work]") { $usercnt++ };
}

print "$user has logged in $usercnt times\n";
}

system("rm lastfile");

print STDOUT "\nEnd of program $0\n\n";

################reverse2#############################################
#!/opt/PUBperl/bin/perl
#
#Reverse the letters of 1st parm
#reverse2 'how do you do'

@letters=split(//,$ARGV[0]);
print "@letters";

$last=$#letters;
$work=-1;

while($work++ < $#letters){
$reverse[$work]=$letters[$last--];
}

print "\n@reverse";

print STDOUT "\nEnd of program $0\n\n";

###################alphafile#########################################
#!/opt/PUBperl/bin/perl
#Count number files starting with a particular letter
#Associate array example 1

while(<*>) {
if( -f ){
        $filecnt++;
        @alpha[$x++]=substr($_,0,1);
}
}

foreach $char (@alpha) {
$count{$char}=$count{$char}+1;
}

foreach $file (keys %count) {
        print "Total files starting with $file was $count{$file}\n";
}
print "Total files starting with all letters was $filecnt\n";

print STDOUT "\nEnd of program $0\n\n";

#################lastlogins############################################
#!/opt/PUBperl/bin/perl
#Count number times users login via last info
#Associate array example 2

system("last>lastfile");
open(stuff,lastfile);

while() {
        $logcnt++;
        ($work)=split;
        $logins[$x++]=$work;
}

foreach $lognames (@logins) {
$count{$lognames}=$count{$lognames}+1;
}

foreach $logname (keys %count) {
        print "Total logins for $logname was $count{$logname}\n";
}

print "Total logins was \t$logcnt\n";
system("rm lastfile");

print STDOUT "\nEnd of program $0\n\n";

######################### findtext ###############################
#!/opt/PUBperl/bin/perl
#
# This script lists text files in your current directory
# and underlying directories containing strings presented on the
# command line as parameters

open(INFO, "find . -print |");

ONE:
while($file = ) {
        chop $file;
        next ONE unless -T $file;
        open(TEXT, $file);

        while() {
           foreach $word (@ARGV) {
                if (index($_, $word) >= 0) {
                   print $file, "\n";
                   print "$_\n";
                   next ONE;
                }
           }
        }
}

print  "\nEnd of program $0\n\n";






13. Difference between Perl4 and Perl5


     There exist Perl4 and Perl5. What's the difference between them? Basically, they are two different versions of the language. Perl5 has  been modularized, object oriented, tweaked, trimmed, and optimized.

     There are some new features shown in Perl5:

          Enhanced usability

          Simplied grammar

          The new yacc grammar is 1/2 of the old one. The reserved words are cut by 2/3. 

          Lexical scoping

          Arbitratrily nested data structures

          Modularity and reusability

          Object-oriented programming

          A package can function as class. Filehandles are treated as objects. 

          Embeddible and extensible

          Perl can be easily embedded in C/C++ applications. It can also either call or be called by your routines through a documentation  interface. The XS preprocessor is provided to make it easy to glue your C/C++ routine into Perl. Dynamic loading of modules is  supported. 

          POSIX compliant

          A new module -- POSIX module provides access to all available POSIX routines and definitions. 

          Package constructors and destructors

          Multiple simultaneous DBM implementations

          Subroutine definitions may be autoloaded

          Regular expression enhancements

     You can refer to Metronet Perl5 Info in WWW sites for Perl for more new features of Perl5. 

4. WWW sites for Perl

     You can check the following WWW sites for more information:

        1.University of Florida's Perl Archive 
        2.Metronet Perl5 Info 
        3.Metronet Gopher Server 
        4.NEXOR Ltd Perl Page 
        5.Northwestern University 
        6.A Wais index of Comp.Lang.Perl 
        7.An Index of Perl/HTML archives 
        8.Neil Bowers's Perl page 
        9.Robert Seymour's Perl page 
       10.The Taming of the Camel -- An Overview of Perl 5.0 by Larry Wall 
       11.Larry Wall's witticisms 
       12.Yahoo's Perl page 
       13.The Perl FTP Archive (University of Florida) 

15. References

       1. Larry Wall & Randal L. Schwartz, "The Camel Book -- Programming Perl", O'Reilly &
          Associates 
       2. Randal L. Schwartz, "The Llama Book -- Learning Perl", O'Reilly & Associates 
       3.Ellie Quigley, "Perl by Example", New Jersey, Prentice Hall, 1995 
       4.comp.lang.perl FAQ 


Back To Main Page: