...making Linux just a little more fun!

<-- prev | next -->

Preparing For My Interviews Part 1: MySQL and Perl

By Mark Nielsen

Preparing For My Interviews Part 1: MySQL and Perl

By Mark Nielsen

  1. Introduction
  2. Multiple Inheritance module Class::Inheritance
  3. A quick Python script to play my mp3's for Real Player.
  4. MySQL Certification
  5. Preparation for Advanced MySQL Certification
  6. Installing MySQL 4.1
  7. My Python script to execute sample code for Certification
  8. Using MySQL 4.1
  9. Next Month: Clustering, Master/Slave, Stored Procedures in MySQL
  10. Conclusion

Introduction

I got laid off, so I am doing a couple of things to ready myself for interviews:
  1. A lot of Perl interviews involve questions about multiple inheritance. Even though nobody ever uses multiple inheritance, interviewers always like to ask questions about it. Thus, I decided to make a module to answer any and all questions about multiple inheritance for a given class in Perl. If anybody asks me questions about multiple inheritance, I will just show them the Perl module I wrote. This Perl module will probably be called Class::Inheritance if it gets accepted into CPAN. I already started the process for it.
  2. I needed a quick script to play my mp3s in random order.
  3. I am studying for the MySQL Professional Certification, so I decided to do two things regarding the exam:
    1. Make a bash script to install MySQL over and over again. In the future, all I have to do is just change the version number and the script should install new versions automatically. I always like to download and install software instead of using RPMs; I want to know how they built the stuff.
    2. Write a Python script to execute a bunch of commands that represent similar things to the ones asked during the MySQL Professional Exam. This helps me to remember the commands, and hopefully will give other people a good idea of the type of commands they need to practice to learn MySQL.
Thus, this article is centered around my experience studying for MySQL certification and what I did to help me out during Perl interviews.

(UPDATE: I ended up passing the MySQL Professional Exam. The examples I made really helped out a lot. Also, I ended up placing my Class::Inheritance module in CPAN under the 06_Data_Type_Utilities/Class category. It's somewhat crude, but it's a good start. I plan on redoing it completely because of some ugly code.)


Multiple Inheritance module Class::Inheritance

After going through a few interviews, I decided to write a module to answer any and all questions about multiple inheritance given a class and method. If someone asks me anything about multiple inheritance, I will just show them the module I wrote for CPAN. This module probably has very little use outside of interviews; however, I do plan on adding a bunch of methods to the module to make it so you can debug multiple inheritance issues easily and maybe even manipulate the inheritance environment.

This module is just in its baby stages. In the future, it should be at https://cpan.perl.com.

How does multiple inheritance work in Perl? Well, when you use inheritance in Perl, you need to create a package. A package is more a less a bunch of functions put together to form a "class" (or multiple classes). A class is more or less a name for a bunch of functions (and sometimes variables). Your class is what is used to create objects in Perl.

So what is inheritance? Let's say you create a package called "package1" and it has a bunch of functions (methods). You want your second package "package2" to have all the functions of the first package but you don't want to rewrite them all. When you use inheritance, that is exactly what you do: you grab all the functions (methods) from the first package and absorb them into the second package without having to rewrite the functions. You do this by specifying the name of the first package in the "@ISA" array.

So what is multiple inheritance? Well, it's what you get when you get your functions from more than one package.

So when you inherit the functions (methods) from multiple packages, how does Perl choose the method if there's more than one with the same name? It grabs the function (method) from the first package in the "@ISA" list which has that function and stops there. Thus, how you list your packages in the "@ISA" list will determine which packages get looked at first to find a function.

By the way, what is a method? A function that is part of a class.

Where is this @ISA thingy defined? It is defined in each package. Each package has its own @ISA list. It is effectively empty if you don't do anything to it. If you define it to contain any number of package names, your package will inherit methods from those packages.

If I have a huge chain of packages inheriting from one another, will the last package get all the functions (methods) from its parents, grandparents, great grandparents, etc? Yes, but it will "climb" up the family tree and stop at the first relative that has the function (method).

For the files listed below, we have two chains relative to package "package6". Package6 contains two packages it inherits from: package5 and package5_2. Each of these packages inherits from package4, which inherits from package3, which inherits from package2, which inherits from package1, which inherits from CGI.

The whole goal of the script "Inherit_Test.pl" is to show the family tree of the package "package6" and to show where package6 gets the method "param" from. You can modify it to suit your own needs.

Download all the files below and then execute "perl Inherit_Test.pl package6 param". You can change "package6" to any package name and you can change "param" to any function name. It will error out properly if no package or function exists.

Try the following:

perl Inherit_Test.pl package6 param
perl Inherit_Test.pl package4 param

The expected results for "perl Inherit_Test.pl package4 param":

We assume the filename for the package is in 'package4.pm'.  Looking at the
function (method) 'param' in class 'package4', we learn that function
'param' comes from class 'CGI'.

Parent Tree is: package3 package2 package1 CGI 

Original sources (defined) for 'param' are: CGI

The expected results for "perl Inherit_Test.pl package6 param":

We assume the filename for the package is in 'package6.pm'.
Looking at function (method) 'param' in class 'package6'.

Function 'param' comes from the class 'package5'.

Parent Tree is: package5 package4 package3 package2 package1 CGI package5_2 package4 package3 package2 package1 CGI 

Original sources (defined) for 'param' are: package5 CGI package5_2 CGI
Here are a list of files you need to save.
  1. Class/Inheritance.pm
  2. Inherit_Test.pl
  3. package1.pm
  4. package2.pm
  5. package3.pm
  6. package4.pm
  7. package5.pm
  8. package5_2.pm
  9. package6.pm

A quick Python script to play my mp3's for Real Player

I only wrote this script because my website, https://myopenradio.com, got moved to a much better computer and rather than figuring out if the website was working, I just wanted to make a quick Python script which would let me play my mp3s in a random order. The file "/usr/local/RealPlayer8/realplay.bat" does two things:
  1. Executes my Python script to create a "/tmp/temp1.smil" file which is a listing of my mp3s in a order random order.
  2. Then it executes RealPlayer and points it to that file.
This script shows you how to use regular expressions, how to get a list of files in a directory, how to filter the list of files using the regular expression, and how to loop through a list in a random order. Also, you could combine it with known URLs of NPR radio shows or other media streams and mix them in with your mp3s; that's how you can create your own radio. Your radio could play a list of songs, then some shows, and then more songs, etc. Perhaps, if you can stream it legally to customers, you could include ads.
  1. # NEW FILE:
    ### Save this as /usr/local/RealPlayer8/realplay.bat
    /usr/local/RealPlayer8/realplay.py
    /usr/local/RealPlayer8/realplay /tmp/temp1.smil
    
  2. realplay.py

MySQL Certification

There are two different levels of MySQL Certification. The Core certification covers general SQL and as well as MySQL specific stuff. For a senior DB person, it doesn't take much studying to pass. However, for the MySQL Professional Certification, you really do want to study all the commands, even the ones that you will never use, because the exam is going to cover them. Studying for the exam gives you a nice overview of MySQL and makes you take notice of stuff you would never have bothered with.

Getting MySQL-certified now is good because there aren't that many people listed. After you pass an exam, it might take a couple of weeks before you get publicly listed. After you pass your exam, you need to log in and let people view which exams you have passed. You find the list of the MySQL Professionals here.


Preparation for the Advanced MySQL Certification

I highly recommend you get the study guide from MySQL Press; it's a good book. Usually certification books contain a lot of garbage, but this one is actually very useful (in my opinion).

After you have passed the Core Certification and you are ready for the Professional Certification, you should execute the scripts I list below. The "Compile_MySQL.bat" script is just a bash script. It will blow away any previous installation in case you want to run the script multiple times. The "Post_Mysql.py" script will create a log of all the commands it executes so that you can go back and do them one-by-one on your own. It would take too long to explain what each command does, so I strongly advise you to read the online MySQL Documentation or the study guide to understand what each command does.


Installing MySQL 4.1

The purpose of this script is to show you how you can install MySQL easily. Basically, in the future, all you have to do is change some of the "export" variables in the script when a new version of MySQL comes out, and it should install the new versions just fine. A similar approach can actually be taken to install any piece of software, like Apache, Perl, Python, Zope, etc. I usually make an installation script, and later when I need to upgrade, I backup all my data and software, modify the script slightly, and then execute it. Often, if you don't write down all your commands as a script, it becomes very painful to figure out how you installed the software last time.

The script should execute as long as you don't have any missing software. First, download the 3 config files and then download this script. Execute the script as follows:
bash Compile_MySQL.bat

  1. my.cnf
  2. Start_MySQL
  3. Stop_MySQL
  4. Compile_MySQL.bat

My Python script to execute sample code for Certification

The purpose of this script to execute commands after a fresh installation to test a list of MySQL commands for the MySQL Professional Exam. I really didn't explain each command, but it's all clarified in the Study Guide and the online documentation I've recommended above.

This script will alter the MySQL environment. It will run a series of example commands and restart the MySQL service which will require you to use the new passwords when you try and connect to MySQL in the future. I suggest you change the passwords for all accounts after you get done with this script.

After you run this script, you can connect as:
mysql -u root -p'this is a dumb password, please change.'

Execute as follows:
python Post_Mysql.py /usr/local/mysql4.1

using Post_Mysql.py


Next Month: Clustering, Master/Slave, Stored Procedures in MySQL

Title says is all, except for the fact I will be using MySQL 5.0 to test the stored procedures.

Conclusion

Class::Inheritance is a pretty cool module (please visit CPAN for the latest version.) The one listed for this article was just a primer I used to get myself started. By the time I got finished with this article, I added a lot more stuff and fixed a few bugs. I am still unhappy with the module and think there is a lot of work to do to make it more accurate and efficient. If you study the module and figure out how it works, you are set for any multiple inheritance question an interviewer will throw at you. I plan on adding a lot more methods to the module to do a bunch of more stuff.

About the Python script to play mp3s, I hope this little script helps people understand how to use Python. Python is a very cool programming language and is my language of choice. It has a lot of potential.

About the MySQL scripts: I probably should have explained the MySQL commands a little bit more in detail, but if you buy the book or read the online documentation, you should be able to understand it. If you manage to execute my scripts correctly, look at the log files in the Output directory from where you run the Post_Mysql.py script. It should have a couple of files with all the commands it executed so that you can go through them one by one. Ideally, you should execute the commands one by one and see what they do. You should also follow along in the study guide for MySQL (MySQL Press) as you do so.

 


[BIO] Mark Nielsen was enjoying his work at cnet.com as a MySQL DBA, but is moving to Google as a MySQL DBA. During his spare time, he uses Python heavily for mathematical and web projects.

Copyright © 2004, Mark Nielsen. Released under the Open Publication license unless otherwise noted in the body of the article. Linux Gazette is not produced, sponsored, or endorsed by its prior host, SSC, Inc.

Published in Issue 108 of Linux Gazette, November 2004

<-- prev | next -->
Tux