Site hosted by Angelfire.com: Build your free website today!
# Opening and reading a file's contents
# Opening c:\perl\script\passwords file.  Use / instead on \ b/c Perl was originally
# written for use with UNIX
open(DAT, "c:/perl/script/passwords") || ("Could not open file!");
# This stores all the data from the file into an array
@file_data=<DAT>;
# Closes the file
close(DAT);

# This reads each line in the array and assigns them to variables
# line1 = $passwd   line2 = $homedir
foreach $info(@file_data)
{
($passwd,$homedir)=split(/\|/,$info);
print $passwd;
print $homedir, "\n";
}
# This gets the length of the variable $passwd
$passwd_length = length($passwd);
#This prints the length of $passwd
print $passwd_length, "\n";

# This print each character of the $passwd variable.
# Using the if statements to find out how many variables to use based on how many characters
# are in the $passwd variable.
if($passwd_length=3)
{
$first=substr($passwd, 0, 1);
print $first, "\n";
$second=substr($passwd, 1, 1);
print $second, "\n";
$third=substr($passwd, 2, 1);
print $third, "\n";
}

if($passwd_length=4)
{
$first=substr($passwd, 0, 1);
print $first, "\n";
$second=substr($passwd, 1, 1);
print $second, "\n";
$third=substr($passwd, 2, 1);
print $third, "\n";
$fourth=substr($passwd, 3, 1);
print $fourth, "\n";
}

if($passwd_length=5)
{
$first=substr($passwd, 0, 1);
print $first, "\n";
$second=substr($passwd, 1, 1);
print $second, "\n";
$third=substr($passwd, 2, 1);
print $third, "\n";
$fourth=substr($passwd, 3, 1);
print $fourth, "\n";
$fifth=substr($passwd, 4, 1);
print $fifth, "\n";
}

if($passwd_length=6)
{
$first=substr($passwd, 0, 1);
print $first, "\n";
$second=substr($passwd, 1, 1);
print $second, "\n";
$third=substr($passwd, 2, 1);
print $third, "\n";
$fourth=substr($passwd, 3, 1);
print $fourth, "\n";
$fifth=substr($passwd, 4, 1);
print $fifth, "\n";
$sixth=substr($passwd, 5, 1);
print $sixth, "\n";
}

if($passwd_length=7)
{
$first=substr($passwd, 0, 1);
print $first, "\n";
$second=substr($passwd, 1, 1);
print $second, "\n";
$third=substr($passwd, 2, 1);
print $third, "\n";
$fourth=substr($passwd, 3, 1);
print $fourth, "\n";
$fifth=substr($passwd, 4, 1);
print $fifth, "\n";
$sixth=substr($passwd, 5, 1);
print $sixth, "\n";
$seventh=substr($passwd, 6, 1);
print $seventh, "\n";
}

if($passwd_length=8)
{
$first=substr($passwd, 0, 1);
print $first, "\n";
$second=substr($passwd, 1, 1);
print $second, "\n";
$third=substr($passwd, 2, 1);
print $third, "\n";
$fourth=substr($passwd, 3, 1);
print $fourth, "\n";
$fifth=substr($passwd, 4, 1);
print $fifth, "\n";
$sixth=substr($passwd, 5, 1);
print $sixth, "\n";
$seventh=substr($passwd, 6, 1);
print $seventh, "\n";
$eigth=substr($passwd, 7, 1);
print $eigth, "\n";
}

if($passwd_length=9)
{
$first=substr($passwd, 0, 1);
print $first, "\n";
$second=substr($passwd, 1, 1);
print $second, "\n";
$third=substr($passwd, 2, 1);
print $third, "\n";
$fourth=substr($passwd, 3, 1);
print $fourth, "\n";
$fifth=substr($passwd, 4, 1);
print $fifth, "\n";
$sixth=substr($passwd, 5, 1);
print $sixth, "\n";
$seventh=substr($passwd, 6, 1);
print $seventh, "\n";
$eigth=substr($passwd, 7, 1);
print $eigth, "\n";
$ninth=substr($passwd, 8, 1);
print $ninth, "\n";
}

if($passwd_length=10)
{
$first=substr($passwd, 0, 1);
print $first, "\n";
$second=substr($passwd, 1, 1);
print $second, "\n";
$third=substr($passwd, 2, 1);
print $third, "\n";
$fourth=substr($passwd, 3, 1);
print $fourth, "\n";
$fifth=substr($passwd, 4, 1);
print $fifth, "\n";
$sixth=substr($passwd, 5, 1);
print $sixth, "\n";
$seventh=substr($passwd, 6, 1);
print $seventh, "\n";
$eigth=substr($passwd, 7, 1);
print $eigth, "\n";
$ninth=substr($passwd, 8, 1);
print $ninth, "\n";
$tenth=substr($passwd, 9, 1);
print $tenth, "\n";
}
 
 
 

# Appending a file

$username="root";

open(DAT,">>c:/perl/script/test1.pl") || die("Could not open file!");
print DAT "$username\n";
close(DAT);

$sitename="Mysite";
$siteurl="http://www.mysite.com";
$description="My website";
$sitedata="c:/perl/script/test1.pl";

# Opening a file for appending adds the >> in the open function.
open(DAT,">>$sitedata") || die("Cannot Open File");
# This prints variables to the open file.
print DAT "$sitename\|$siteurl\|$description\n";
close(DAT);
 

# Open and read the contents of the appended file
# This will read the contents of the same file we appended above and prints the contents
# to the screen.  You should notice the extra lines that were added using append.

open(DAT,"c:/perl/script/test1.pl") || die("Could not open file!");
@append_file=<DAT>;
close(DAT);

if($#append_file=1)
{
foreach $line(@append_file)
{
($line1)=split(/\|/,$line);
print $line1;
}
}

elsif($#append_file=2)
{
foreach $line(@append_file)
{
($line1, $line2)=split(/\|/,$line);
print $line1;
print $line2;
}
}

elsif($#append_file=3)
{
foreach $line(@append_file)
{
($line1, $line2, $line3)=split(/\|/,$line);
print $line1;
print $line2;
print $line3;
}
}

elsif($#append_file=4)
{
foreach $line(@append_file)
{
($line1, $line2, $line3, $line4)=split(/\|/,$line);
print $line1;
print $line2;
print $line3;
print $line4;
}
}

else
{
print "Too many lines!";
}