#!/usr/bin/perl #-w use DBI; $address = "some.address"; $database = "some.db"; $username = "some_username"; $password = "some_pass"; $table = "some_table"; $mysqlcommand = ''; #getting the vaariables from stdin print ("\nZadanie Written By KRAS| "); print ":)\n\n"; print "You have the following available commands:\n"; print "print - use it for calculations too\n" ; print "read - reads a string, read all - be carefull with that\n"; print "set - defines a variable\n"; print "call - call a soubroutine\n"; print "return - returns from a souroutine, use it only within a subroutine\n"; print "stop - exits the program\n"; print "help - shows some help\n"; &databaseconnect; &mainprog; sub mainprog { print ("KRASI:> "); $commandline = ; chop ($commandline); @command = split(/ /, $commandline); print "\n\n"; #tuk trea da se dawa default sotinost ako nishto ne e wywedeno $c1=$command[0]; $c2=$command[1]; $c3=$command[2]; $c4=$command[3]; #nachalna proverka dali wywedenite kmandi sa werni #samo kakto az si znam if ( $c1 eq 'stop'|| $c1 eq 'read' || $c1 eq 'help'|| $c1 eq 'set'|| $c1 eq 'print'|| $c1 eq 'call') {} else { print "\nKRASI:>command not found\n"; &mainprog}; if ( $c1 eq 'set' ) { $mysqlcommand = "insert into $table values ('$c2', '$c3' )" ; if (!$c2) { print "\nKRASI:>There is an error in your statement,please use the help command\n"; &mainprog;} if (!$c3) { print "\nKRASI:>There is an error in your statement,please use the help command\n"; &mainprog;} &insertvariable; &mainprog; } if ( $c1 eq 'read' ) { if ($c2 eq 'all') { $mysqlcommand = "select * from $table";} else { $mysqlcommand = "select * from $table where ime='$c2'"}; &printvariable; &mainprog; } if ( $c1 eq 'help' ) { &helpme; &mainprog; } if ( $c1 eq 'stop' ) { &exitme;} if ( $c1 eq 'print' ) { &printme; &mainprog;} if ( $c1 eq 'call' ) { &callme1;} } #end of mainporgo #print $mysqlcommand; sub sqldie { ($die_str) = @_; print "KRASI:>$die_str\n"; print "KRASI:>An error had occured, please verify your command\n"; &mainprog; } sub printvariable { $cursor=$dbh->prepare("$mysqlcommand") || sqldie("Prepare failed."); $cursor->execute || sqldie("Execute Failed!".$cursor->errstr); if ($c2 eq 'all' && $cursor->rows == 0) { print "KRASI:>Values table is empty\n";} else { if ($cursor->rows == 0) {print "KRASI:>No value defined by that name\n";} else { while(@data = $cursor->fetchrow_array) { print "\nKRASI:>The value of $data[0] is $data[1]\n";} } } $cursor->finish; } sub exitme { print "\nexiting.........\nThank you for using Zadanie\nWritten By KRAS|\n\n"; $dbh->disconnect; exit; } sub insertvariable { if ($c3 eq 'random') { $c3 = rand (1000); $c3 = sprintf("%0d \n", $c3); $cursor=$dbh->prepare("insert into $table values ('$c2', '$c3' )") || sqldie("Prepare failed."); $cursor->execute || sqldie("Execute Failed!".$cursor->errstr); $cursor->finish; print "value of $c2 is set to $c3\n\n"; &mainprog; } else {print "KRASI:>The variable you want to set is Not an integer will be set to zero\n" unless $c3=~ /^[+-]?\d+$/}; if (length($c2) >= 32) { print "KRASI:>The variable can not be longer than 32 charachters\n"; &mainprog; } $cursor=$dbh->prepare("$mysqlcommand") || sqldie("Prepare failed."); $cursor->execute || sqldie("Execute Failed!".$cursor->errstr); if ($c3=~ /^[+-]?\d+$/) { print "value of $c2 is set to $c3\n\n"; } $cursor->finish; } sub helpme { print "\n"; print "The available commands are used like that:\n"; print "read x, read all\n"; print "print 5 + 5, print random to print a random number\n"; print "help\n"; print "call submenu\n"; print "return - use it only within a subroutne"; print "stop\n"; } sub databaseconnect { #load the driver for mysql $drh=DBI->install_driver('mysql') || sqlquit("Could not load mysql driver\n"); $dbh=$drh->connect("$database:$address","$username","$password") || sqlquit("Failed to connect to mysql server\n"); } sub printme { #this is for the print command if (!$c2 || !$c4) { print "\nKRASI:>There is an error in your statement,please use the help command\n"; &mainprog;} if ( $c3 eq '+'|| $c3 eq '-' || $c3 eq '*'|| $c3 eq '/' || $c2 eq 'random') {} else { print "\nKRASI:>you have entered a wrong print expression\n"; &mainprog}; if ($c2 eq 'random') { $c2 = rand (1000); $c2 = sprintf("%0d \n", $c2); print "KRASI:>random variable1 is set to $c2"; } if ($c4 eq 'random') { $c4 = rand (1000); $c4 = sprintf("%0d \n", $c4); print "KRASI:>random variable2 is set to $c4"; } #check to see if the entered variable is not set and use it that way $cursor=$dbh->prepare("select * from $table where ime='$c2'") || sqldie("Prepare failed."); $cursor->execute || sqldie("Execute Failed!".$cursor->errstr); if ($cursor->rows == 0) { #proverka dali chisloto koeto w wywedeno e int print "KRASI:>The variable1 you have entered is Not an integer or is not defined, or it was randomly generated\n" unless $c2=~ /^[+-]?\d+$/; } else { @data = $cursor->fetchrow_array; $c2=$data[1]; } $cursor->finish; $cursor=$dbh->prepare("select * from $table where ime='$c4'") || sqldie("Prepare failed."); $cursor->execute || sqldie("Execute Failed!".$cursor->errstr); if ($cursor->rows == 0) { #proverka dali chisloto koeto w wywedeno e int print "KRASI:>The variable2 you have entered is Not an integer or is not defined, or it was randomly generated\n" unless $c4=~ /^[+-]?\d+$/; } else { @data = $cursor->fetchrow_array; $c4=$data[1]; } $cursor->finish; if ($c3 eq '+') {print "\nKRASI:>"; print ($c2 + $c4); print "\n";} if ($c3 eq '-') {print "\nKRASI:>"; print ($c2 - $c4); print "\n";} if ($c3 eq '*') {print "\nKRASI:>"; print ($c2 * $c4); print "\n";} if ($c3 eq '/') {print "\nKRASI:>"; print ($c2 / $c4); print "\n";} } sub callme1 { if ($c2 eq 'help') {} else { print "KRASI:>No such subroutine\n"; &mainprog}; print "\nKRASI-help:>"; $helpline = ; chop ($helpline); @help = split(/ /, $helpline); print "\n\n"; $h1=$help[0]; if ($h1 eq 'return') { print "\nKRASI-help:>Returning from a subroutine\n"; &mainprog}; if ($h1 eq 'call') { print "\ncall is used to to call a subrotine inside tha main program use it like that:call name_of_subroutine,whe you are finished using the subroutine juste type return at command line to get back into the main screen of Zadanie"; &callme1;} if ($h1 eq 'help') { print "\nhelp gives the command line usage.you can use call help to call the help subotutine where more help is availabe by just typing the command u r interested in"; &callme1;} if ($h1 eq 'print') { print "\nEvaluates or just prints an expression. print 5+5 will give you the sum of 5 and 5"; &callme1;} if ($h1 eq 'read') { print "\nIt reads"; &callme1;} if ($h1 eq 'set') { print "\nIt sets"; &callme1;} if ($h1 eq 'stop') { print "\nIt stops"; &callme1;} print "\nKRASI-help:>No such help topic"; &callme1; } sub sqlquit { ($diereason) = @_; print "KRASI:>$diereason\n"; print "KRASI:>SQL error had occured, can not continue\n"; print "KRASI:>exitting......\n"; exit(1); } #end of perl script written by KRAS|