Loading...

A kickstart with D

posted on Mon, May 26th, 2008

For a long time I used PHP for most of my projects and programming needs. This included web based application like content management systems, custom database frontends and various small fun projects to gather some information from the web. I even programmed daemons with PHP 5, which run on the unix shell. I started with PHP in 1998 while studying computer science. Since then I feel and felt quite comfortable with PHP. It well suited my programming needs and skills. The capabilities of PHP grew over the last 10 years and make it a very stable and productive programming/scripting language.

But for a recent project I need to process large amounts of data and therefore I was on the search for an alternative to PHP, which I could pick up very easily and which performance better. I took a look at D, a new programming language which is a strongly typed, natively compiled computer language. It has a syntax similar to C, C++ and Java and at the same time combines the high performance from natively compiled languages with the rapid developement capabilities of scripting languages like Perl, Python or PHP. After studying the D website and a couple other D web pages I decided to give it a try.

I will document the steps I've run into as a PHP developer trying to get started with D. The first in the series is how to install the compiler and builder environment to start with some basic programming tasks.

Basic Installation

The easiest way to install D is to use the Tango compilation. It includes the compiler and the Tango general-purpose companion library for the most basic tasks. The compiler as well as the library are open source.

  1. Download the Tango package, which you find here:
    http://www.dsource.org/projects/tango/wiki/DmdDownloads.
  2. The next step is to copy the content of the tar.gz file to: /usr/local/dmd
  3. In order to use the dmd compiler from everywhere you need to either create a new .bash_profile or to add the following lines to the .bash_profile file:

    PATH=$PATH:/usr/local/dmd/bin
    export PATH


    Alternatively you can create symlinks for all programs within the /usr/local/dmd/bin directory in the /usr/bin/ directory.
  4. Now you should download and install the DSSS (the D Shared Software System), which helps you to build your programs and helps installing, configuring and acquiring additional D software. It is comparable to Perl's CPAN and PHP' PEAR system.
    You can download DSSS from:
    http://svn.dsource.org/projects/dsss/downloads/.
  5. Copy the content of the tar.gz file to /usr/local/dmd.
  6. In order to use DSSS with Tango you need to customize the following file:
    usr/local/dmd/etc/rebuild/default

    Replace

    profile=dmd-posix

    with

    profile=dmd-posix-tango
  7. That's it. You should now be able to write your own D code and compile it to a program.


MySQL Support

In order to connect to mysql you need to install the DDBI (D DBI), a database independent interface for the D programming language.

  1. First you need to download the latest D DBI sources from the SVN server. I couldn't get in running with the download on the web page.
    Go to the directory where you store or your installation files or your home directory and type in:

    svn co http://svn.dsource.org/projects/ddbi/trunk

    This will get all the necessary files from the SVN repository.
  2. The only thing you need to do to install the D DBI is to copy the complete directory into the lib/import directory of your D directory. If you followed the installation steps above this would be /usr/local/dmd/lib/import/.

    cp /install/D/ddbi/trunk /usr/local/dmd/lib/import
  3. That's basically it. The only think you need to to now is to tell DSSS where your mysql client library is. Normally you use the following line:

    dsss build -version=dbi_mysql -L/usr/local/mysql/lib/mysql/mysqlclient.so.15.0.0

    If you use a MySQL version newer then 5.0 you need to add another version info:

    dsss build -version=MySQL_51 -version=dbi_mysql -L/usr/local/mysql/lib/mysql/mysqlclient.so.15.0.0


MySQL Example

  1. Create a project directory where you want to store your code
  2. Create a DSSS config file with this project directory, with the following content:

    name=mysql [mysql.d] target=mysql_test

  3. Create a source file with the following sample code:



  4. If the file is save run dsss build within the project directory:

    dsss build -version=dbi_mysql -L/usr/local/mysql/lib/mysql/mysqlclient.so.15.0.0
  5. If compilation is finished you should find an executable file called mysql_test within your project directory.


 
 

Comments

 
 

Leave a Comment



Remember personal details

Tell me about follow-up comments?