r6 - 09 Sep 2008 - 13:34:10 - BruceJamesYou are here: TWiki >  Custard Web  > PerlSnips

Perl Snips

Debug output of regular expressions

perl -Mre=debugcolor -e 'q/aaa/ =~ /a/'

Set DBI debug on

Useful for debugging Class::DBI or DBIx::Class implementations.

export DBI_TRACE=1

Quick soap server

Creates a class called Simple::SOAP::Server, which contains a method 'alive' which can be called remotely. The SOAP handler is the class itself.

package Simple::SOAP::Server;
use strict;
use warnings;

# CPAN Modules
use SOAP::Lite;
use SOAP::Transport::HTTP;

sub new {
        my $class=shift;
        my $this={};
        bless( $this, $class );

        my $host=shift || '';
        my $port=shift || '8000';

        warn( "Starting SOAP Server on $host:$port...\n" );
        # Configure the server daemon
        my $server = new SOAP::Transport::HTTP::Daemon( LocalAddr =>    $host,
                                                        LocalPort =>    $port,
                                                        dispatch_to =>  $class
        );
        # Notes.
        # uri doesn't appear to be necessary in the server, but is in the client
        # dispatch_to must match class given in the URI
        # ie. uri='http://Server/Simple/SOAP/Server' class=Simple::SOAP::Server

        $server->handle();

        return $this;
}

sub alive {
        my $this=shift;
        return "I'm alive!";
}

Quick soap client

use SOAP::Lite;
use SOAP::Transport::HTTP;

my $host='Server';
my $port='8000';
my $client = new SOAP::Lite(    uri => 'http://Server/Simple/SOAP/Server',
                                proxy => "http://$host:$port/"
);
if ($client) {
	my $result =  $client->alive();
	if ($result) {
          		print $result->result()."\n";
      } else {
          	 	warn( "The SOAP server gave no response().\n" );
      }
} else {
   	warn( "There is a problem connecting to the remote process.\n" );
}

Hash slices

my %hash;
my @keys = ( 'one','two','three' );
my @values = ( 1,2,3 );

# Initialise the hash from the keys and values arrays.
@hash{ @keys }=@values;

# Retrieve the values of elements 'one' and 'three' into an array.
@keys = ( 'one','three' );
my @array = @hash{ @keys };

Setting up a mod_perl2 handler in apache2 httpd.conf

	# Perl Module Handler.
	#
	PerlOptions +GlobalRequest
	PerlSwitches -I/usr/local/lib
	PerlModule Apache2;
	PerlModule WEB::AppHandler
	
		# The web app, or web service handler
		SetHandler modperl
		PerlResponseHandler WEB::AppHandler
	

Date & Time parsing with DateTime?

Parse an ISO8601 datetime string

Like: "2008-01-28T12:18:00Z"

 
  my $datetime = DateTime::Format::ISO8601->parse_datetime( "2008-01-28T12:18:00Z" );

Parse MySQL? datetime


  my $datetime = DateTime::Format::MySQL->parse_datetime( '' );

Multidimensional hashes

This feature was introduced in Perl 4 as a means to emulate multidimensional arrays. It is probably better to use a hash of hashes nowadays although this sometimes still has its uses. For instance with tied hashes to BDB tables.

Emulate a multidimensional hash by specifying more than one key separated by commas between the curly braces. These keys will be joined together using the value of $; (default 0x1C ) as a separator. eg.

    $message{ $key, $locale } = $message_string;

-- BruceJames - 22 Feb 2007

Edit | Attach | Printable | Raw View | Backlinks: Web, All Webs | History: r6 < r5 < r4 < r3 < r2 | More topic actions
 
Custard
This site is powered by the TWiki collaboration platformCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback