15. Write a program which illustrates the use of associative arrays in perl
#!/usr/bin/perl
%data = ('John Paul' => 45, 'Lisa' => 30, 'Kumar' => 40);
while (($key, $value) = each(%data)){
print $key.", ".$value."\n";
}
print "adding element";
print "\n";
# adding an element to the hashs
$data{'Akbar'} = 55;
while (($key, $value) = each(%data)){
print $key.", ".$value."\n";
}
print "deleting element";
print "\n";
# delete the same element from the hash
delete $data{'Akbar'};
while (($key, $value) = each(%data)){
print $key.", ".$value."\n";
}
OUTPUT:
$perl main.pl
Lisa, 30 John Paul, 45 Kumar, 40 adding element Kumar, 40 John Paul, 45 Lisa, 30 Akbar, 55 deleting element Kumar, 40 John Paul, 45 Lisa, 30
16. Write perl program takes set names along the command line and prints whether they are regular files or special files.
$len=@ARGV; for($i=0;$i<$len;$i++) { if(-e $ARGV[$i]) { if(-T $ARGV[$i]) { print"$ARGV[$i] is a text file\n"; } else { print"$ARGV[$i] is a special file\n"; } } else { print"$ARGV[$i] does not exists"; } }
OUTPUT:
C:\Users\crrengg\Desktop>perl 16.pl 1.txt
1.txt is a text file
C:\Users\crrengg\Desktop>perl 16.pl 15.png15.png is a special file17.Write a perl program to implement `password' program.my $stralt=''; my $encrypted=''; my $password=''; my $use = 'Usage: Please provide password for encrypt'; my @straltchars=('.', '/', 0..9, 'A'..'Z', 'a'..'z'); my $args=@ARGV; if ( $args < 1 || $args > 2 ) { print "$use\n"; exit; } $password=$ARGV[0]; if( $args == 1 ) { $stralt = join('',@straltchars[rand(64),rand(64)]); } else { $stralt=$ARGV[1]; } $encrypted=crypt($password,$stralt); print "$password --> $encrypted\n";OUTPUT:D:\perl>perl 17.pl pwd mmrpwd --> jph3wiYxv/3wA18. An example perl program to connect to a MySQl database table and executing simple commands.#!/usr/bin/perl -w use DBI; $user = "root"; $password = ""; my $dbh = DBI->connect ("DBI:mysql:jp", $user,$password) or die "Can't connect to database: $DBI::errstr\n"; print "CONNECTED TO THE DATABASE\n"; my $sth = $dbh->prepare("SELECT *FROM students"); $sth->execute(); print "\tQuery results\n================================================\n"; print("ROLLNUMBER NAME BRANCH\n=====================\n"); while (my @row = $sth->fetchrow_array()) { print "@row\n"; } warn "Problem in retrieving results", $sth->errstr( ), "\n" if $sth->err(); print "\n"; # end of program exit;
19.HTML:
<html>
<head>
<title>Contact Us</title>
</head>
<body>
<font face="Arial" size="10">
<table border="1">
<tr><th>Contact Us</th></tr>
<tr><td>
<form action="19.php" method="POST" enctype="multipart/form-data">YOUR NAME<br/>
<input type="text" name="vname" value="" size="30"/><br>RECIPIENT EMAIL-ID<br>
<input type="text" name="email" value="" size="30"/><br>MESSAGE<br>
<textarea name="message" rows="7" cols="30"></textarea><br><input type="submit" value="Send email"/>
</form>
<td></tr>
</table>
</body>
</html>
19.PHP:
<?php
if(isset($_REQUEST['vname']))
{
$vname=$_REQUEST['vname'];
$email=$_REQUEST['email'];
$message=$_REQUEST['message'];
$from="From: $vname<$email>\r\nReturn-path: $email"; $subject="Message sent using your contact form"; $m = mail($email, $subject, $message, $from); if($m) echo "<h1> Email Sent";
else
echo "Send Failed:";
echo " <a href='19.html'>[Back to Contact us]</a>";
}?>
No comments:
Post a Comment