Tampilkan postingan dengan label Perl. Tampilkan semua postingan
Tampilkan postingan dengan label Perl. Tampilkan semua postingan

Rabu, 19 Mei 2010

Perl DBI module

I am using Perl DBI module. How do I turn off error printing?

Surprisingly this simple question is not well answered on the web, so here you are In DBI's connect function you can specify options. The ones that affect error handling are RaiseError and PrintError. To disable printing error on the screen, you set them both to 0:

RaiseError => 0,
PrintError => 0


Then when an error occurs during any DBI operation, you won't see any error on screen. When an error occurs, the appropriate error message will also be populated in $DBI::errstr. So you can use the following code to know whether an error has occurred:

if($DBI::errstr) {
# an error has occurred; do something here like print out the sql statement that has just been executed


So that you can do something without printing the generic error message. If you get a handle out of executing some operation, then you can also use that handle to detect an error:

$dbh = DBI->connect( "dbi:Oracle:archaeo", "username", "password", %attr);
if($dbh) {
### connect success!
} else {
### connect failed..
}


Hope it helps!

Senin, 25 Januari 2010

Perl Global Variables

How do you define global variables in Perl?

I want to define a variable in one perl file and be able to access it from another perl file in the easiest way possible. First of all I find it shocking that I couldn't find much support for this question on Google, so I decided to post the answer myself. Suppose in global.pl you'd like to define the variable (or variables), and in access.pl you'd like to access the variables defined in global.pl. In global.pl define the variable. Do not use 'my' keyword, as it restricts the scope to current file. This means that you should not have 'use strict' in global.pl. In access.pl put "require 'global.pl'". That's it.

For example:

In global.pl you have:

$MY_GLOBAL_VARIABLE1 = 1;
$MY_GLOBAL_VARIABLE2 = 'a string';


In access.pl you have:

print "My global variable 1 is $MY_GLOBAL_VARIABLE1 and my global variable 2 is $MY_GLOBAL_VARIABLE2";

Easy right? Hope it helps
 
support by: infomediaku.com