Perl Crash Course: Gettin’ jiggy wit it

So far we’ve seen how Perl’s basic data structures work and also how to put them all together. Now it’s time to see what built-in functions and techniques Perl provides us to work with scalars, arrays, and hashes.

General functions

print [FILE HANDLE] list of data

Used to print data. If not passed a FILE HANDLE, it will print directly to STDOUT. Interpolation rules apply. Some interesting details about print are:

print @array;  # prints all elements of @array with 
               # no spaces between them
print "@array";  # prints the elements WITH spaces between them
print $var1, $var2, @array;  # prints values with no spaces between them
print FH "some data";  # prints the string "some data" inside file represented by FH (see open below).
# Note the lack of comma between the FH and the data to be printed.

open (FILE HANDLE, mode, filename) or open (FILE HANDLE, “modefilename”)

Opens a file or pipe for reading/writing/appending. Normally filehandles tend to be constants in uppercase, but it’s good practice to use variables to hold your filehandles. That way you can pass them between functions without occupying the main namespace. There are 2 styles of open – old style with 2 parameters and new style with 3.

Continue Reading…

Perl: Installing MQSeries CPAN module on Windows XP

Installing MQSeries module on Windows XP is a piece of cake, as long as you get the right tools before you even try.

Update: October 29, 2018 I haven’t worked on Windows for the past 8 years when we switched to Macs company wide so I haven’t really been keeping up with the MS world. Fortunately, a kind soul reached out to tell me that Visual Studio Express has been discontinued in favor of Visual Studio Community (thanks, Laura!). I’ve updated the download link to get C++ below. Feel free to read more about VSC in this post that Laura wrote.

Update: July 2, 2009 I had to install the module on a new computer running Windows XP and it looks like I had missed a few steps in the original how-to below. It’s been updated with the manual editing steps. From step 3 down, it’s all new.

This is what you need:

  1. MQSeries installed (get the 90-day trial version here. You will need to register, but there’s no charge for that)
  2. Microsoft Visual C++ (it’s free, and you can get it here)
  3. Perl (I use ActiveState)

Steps to get it installed:

  1. Open a command prompt (Start->Run->cmd.exe)
  2. (Extremely important!!)Set up your build environment by running vcvarsall.bat. Mine is under C:Program FilesMicrosoft Visual Studio 9.0VC
    An alternative to this step is to open a Visual Studio 2008 Command Prompt (Start->All Programs->Microsoft C++ 2008 Express Edition->Visual Studio Tools->Visual Studio 2008 Command Prompt)
  3. Make sure your environment variables are set with MQ data: INCLUDE=pathtotoolscinclude directory, LIB=pathtotoolslib directory (typing set will show you your env vars)
  4. Install pre-requisite Params::Validate by running perl -MCPAN -e “install Params::Validate”
  5. Download MQSeries manually by running perl -MCPAN -e “get MQSeries”
  6. cd into the directory where you have your cpan (mine is c:Perlcpanbuild) and enter MQSeries-x.xx-* (where * is a series of random chars if you’re using the latest CPAN)
  7. With a decent text editor (I’m using Notepad++ and also like Crimson Editor and Programmer’s Notepad 2), edit CONFIG file: uncomment MQMTOP = … and replace the path with the path to your MQ Tools directory. It’s OK to use long directory and file names (e.g c:Program FilesIBMWebsphere MQTools)
  8. Now cd into the utils directory, open parse_headers file and comment out or delete the line near the top where it says “my $include = ‘/opt/mqm/inc’;”. The reason for this is that my overwrites the $include variable previously populated by parse_config file.
  9. Save your changes and in the base directory for the MQSeries build, run perl Makefile.PL. It might complain about some libs not being found, but that wasn’t a show stopper for me.
  10. Run nmake. It came with your MS Visual C++ install and should be in your PATH.
  11. Run nmake test. It’ll fail, since you didn’t set any valid data in the CONFIG file. If you have any valid data such as QM and Queues to test it with, go ahead and set them in the CONFIG file and run nmake test again. If not, that’s OK.
  12. If nmake test was the only place where it failed, then you’re good to run nmake install.

That’s it – Perl MQSeries module should now be installed.

Perl: Connecting to an Oracle database WITHOUT having an installed client

I started this post with the intention of having it be a quickie, just showing people how to connect to Oracle with Perl/DBI without having to have Oracle client or even Perl installed, but it’s not that simple.  So I decided to rewrite it with detailed instructions.

Here is a list of what I used:

Machine: Sun Solaris SPARC on build and target machines
Oracle: Oracle 10g client (build machine only)
Perl: v. 5.10.0 (build machine only)
Perl modules: DBI, DBD::Oracle (1.22), PAR (1.008), pp (1.025), PerlIO (1.04) (build machine only)

Continue Reading…

DBD::Oracle + Cygwin: Undefined reference error during make

Yesterday I found myself in a position of having to re-master my computer – hence having to reinstall most of the applications including my trusty Cygwin – which always becomes somewhat of a headache when I reach the point of installing DBD::Oracle in it.

This time I got a undefined symbol error. A quick look in Google showed me that Erik Squires had the same exact problem. Lots of searching later, I find that Google can’t find a handy solution anywhere… So I send Erik an email asking about the solution and to my surprise, he answers only 1 (!!) minute after I click send. Talk about a fast reply!!

Hats off to Erik! You will find the solution here:

Hint: the oci.def that he mentions in his post is inside your DBD::Oracle build directory. Don’t get it confused with ocidef.h file in your $ORACLE_HOME/oci/include directory (that’s where I looked first).

I’m adding the error message below with a big SOLUTION header for any Googler out there having the same problem. No need to keep reading if the text above was enough to solve your problem.

Continue Reading…

Future postings on MySQL

On my previous post, I talked about MySQL functionality “insert into … on duplicate key update” which I found really cool. It has since been the most popular of my messages (although nobody leaves any comments), so I guess I’ll start talking more about MySQL… Don’t forget to keep an eye out for fresh Perl updates as well though – the Perl Crash Course is slowly coming out of the oven.

Cheers to all.

Perl Crash Course: References and Complex Data Structures (CDS)

References and Complex Data Structures (CDS)

Now that we have a pretty good understanding of the 3 types of data in Perl, it’s time we bump it up a notch and put them all together. Perl is most famous for its text manipulation using regular expressions, but it should also be noted by the ability to create multilevel data structures, regardless of them being scalars, arrays, or hashes.

Continue Reading…

Perl Crash Course: Variables and Data Structures

In this chapter, we will see the three types of variables available in Perl: Scalars, Arrays, and Hashes. We will also work with lists, slices, and Complex Data Structures. However, before we go on, you will need to know a few basic commands so you understand the examples.

Code comments: In Perl, you can add comments to the code by using the character #. Whatever is to the right of a # will not be parsed (with at least one exception: the $#array structure which we will see later in the Arrays section). You may find it annoying to not have /*  */ structure for multi-line comments, but you’ll be happy to know that you can emulate multi-line comments using POD, which we’ll see later on.

Printing: Perl prints to the screen through the command print, which takes a list of parameters as input.

Assigning: To assign values to a variable, use  = (the equals operator).

Statement separator: Every statement in Perl ends with ; (semi-colon). Failure to end a statement with ; will result in a syntax error when trying to run your script.

Math: Mathematical operators are +, , *, /, and %, for addition, subtraction, multiplication, division, and modulus – respectively. The modulus operator returns the remainder of a division.

Now on to the scalars…

« Getting it installed on… | TOC | Scalars »