Perl Tutorials

some random perl nuggets

Posted on October 19, 2010. Filed under: Perl Tutorials |

http://www.webreference.com/programming/perl/subroutines/

http://www.troubleshooters.com/codecorn/littperl/perlreg.htm

http://www.washington.edu/perl5man/pod/perlref.html

http://www.stonehenge.com/merlyn/LinuxMag/col10.html

http://www.troubleshooters.com/codecorn/littperl/perlfile.htm

my($text) = "/etc/sysconfig/network-scripts/ifcfg-eth0";
my($directory, $filename) = $text =~ m/(.*\/)(.*)$/;
print "D=$directory, F=$filename\n";
Read Full Post | Make a Comment ( None so far )

Perl Tutorials -Part 91 – Explicit returns

Posted on June 7, 2010. Filed under: Perl Tutorials |

#perl example of subroute that returns a value
#'$$' means expect two scale variables as parameters
sub compareNumbers ($$)
{
   $first = $_[0];
   $second = $_[1];
   if ($first > $second)
   {
     return "$first is bigger than $second";
   }
   elsif ($first < $second)
   {
     return "$first is smaller than $second";
   }
   else
   {
     return "they are probably equal";
   }
}

print compareNumbers 3, 1;
3 is bigger than 1
Read Full Post | Make a Comment ( None so far )

Perl Tutorials -Part 89- Arguments

Posted on June 7, 2010. Filed under: Perl Tutorials |

#perl example of subroute arguments
sub makeCuppa
{  if($_[1])
   {
     $colour = 'white';
   }
   else
   {
     $colour = 'black';
   }
     print "Here is a cup of $colour $_[0] with $_[2] sugar.\n";
}

makeCuppa('tea', 0, 1);
makeCuppa('coffee', 1, 5);

Here is a cup of black tea with 1 sugar.
Here is a cup of white coffee with 5 sugar.
Read Full Post | Make a Comment ( None so far )

Perl Tutorials -Part 88- Subroutines

Posted on June 7, 2010. Filed under: Perl Tutorials |

#perl example of subroutes
sub makeCuppa
{
   #@_ is the default args
   foreach(@_)
   {
      print "I was given this argument: $_\n";
   }
}

makeCuppa('tea', 0, 1);


I was given this argument: tea
I was given this argument: 0
I was given this argument: 1
Read Full Post | Make a Comment ( None so far )

Perl Tutorials -Part 76- Reading user input from the command

Posted on June 7, 2010. Filed under: Perl Tutorials |





do

{

$fileName = ”;

do

{

if ($fileName ne ”)

{

print “Sorry – invalid filename. \n”;

}

# prompt the user for the file

print “which file?”;

$fileName = <>;

chomp $fileName;

}

while (!open WORKING, $fileName);

# pull in the contents of the file

@stats = stat $fileName;

read WORKING, $contents, $stats[7];

# show the contents to the user

print $contents;

print “\nAnother file (y/n)?”;

$another = <>;

}

while($another !~ /[nN]/);

Read Full Post | Make a Comment ( None so far )

Perl Tutorials -Part 75- Changing the default output filehan

Posted on June 7, 2010. Filed under: Perl Tutorials |

#perl example of changing the default output filehandle
open (THEFILE, '>t.pl');
#return default filehandle to THEFILE
select THEFILE;
#print text to the file t.pl
print 'some text';
#return default filehandle to standard output
select STDOUT;
#print text to command line
print 'other text';
other text
Read Full Post | Make a Comment ( None so far )

Perl Tutorials -Part 74- Reading a file’s contents at once

Posted on June 7, 2010. Filed under: Perl Tutorials |

#perl example of Getting information about a file with stat function
$fileName = 't.pl';
@stats = stat $fileName;
open INPUT, $fileName;
#read take three args, handle to read from, variable read to, # of bytes to read
read INPUT, $contents, $stats[7];
print $contents;
1
2
3
4
5
6
7
#perl example of Getting information about a file with stat function
$fileName = 't.pl';
@stats = stat $fileName;
open INPUT, $fileName;
#read take three args, handle to read from, variable read to, # of bytes to read
read INPUT, $contents, $stats[7];
print $contents;
Read Full Post | Make a Comment ( None so far )

Perl Tutorials -Part 71- Writing to a file

Posted on June 7, 2010. Filed under: Perl Tutorials |

#perl example of writing to file

open IN, 't.pl';
foreach (<IN>)
{
    $contents .= $_;
}
close IN;
$contents = '#BEGIN...\n' . $contents . '#END...';

# '>' replace content, '>>' append to the end of content
if(open THEFILE, '>t.pl')
{
   print OUT $contents;
   close OUT;
}
else
{
   print "Could not write to file.";

}
Read Full Post | Make a Comment ( None so far )

Perl Tutorials -Part 70- Reading from a file

Posted on June 7, 2010. Filed under: Perl Tutorials |

#perl example of reading file
opendir THEDIR, '.';
while ($fileName = readdir THEDIR)
{
   print "$fileName \n";
}
closedir THEDIR;

print "*******************\n";

open THEFILE, 't.pl';
foreach (<THEFILE>)
{
   print;
}
close THEFILE;
























.
..
lock
t.pl
usr
lib
*******************
#perl example of reading file
opendir THEDIR, '.';
while ($fileName = readdir THEDIR)
{
   print "$fileName \n";
}
closedir THEDIR;

print "*******************\n";

open THEFILE, 't.pl';
foreach (<THEFILE>)
{
   print;
}
close THEFILE;

Read Full Post | Make a Comment ( None so far )

Perl Tutorials -Part 82- Changing the current working direct

Posted on June 7, 2010. Filed under: Perl Tutorials |

#perl example of changing directory (chdir function)
opendir THEDIR, '.';
while ($fileName = readdir THEDIR)
{
   print "$fileName \n";
}

print "*************************\n";
#change directory to parent directory
chdir '..';
while ($fileName = readdir THEDIR)
{
   print "$fileName \n";
}

closedir THEDIR;








.
..
lock
t.pl
usr
lib
*************************

Disallowed system call: SYS_chdir
Read Full Post | Make a Comment ( None so far )

« Previous Entries

Liked it here?
Why not try sites on the blogroll...