X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FSQL%2FTranslator.pm;h=7cf41796d943491f1e537a7d14cd89f28bf43062;hb=e2158c408a30ce43e277e68d586130a89126c733;hp=00c2dea88e7c975a63d1f7c99fd91150967e78f9;hpb=046f18e5498f3d690e7857d8f97fa54e17da2356;p=dbsrgits%2FSQL-Translator.git diff --git a/lib/SQL/Translator.pm b/lib/SQL/Translator.pm index 00c2dea..7cf4179 100644 --- a/lib/SQL/Translator.pm +++ b/lib/SQL/Translator.pm @@ -1,7 +1,7 @@ package SQL::Translator; # ---------------------------------------------------------------------- -# $Id: Translator.pm,v 1.4 2002-03-21 18:50:53 dlc Exp $ +# $Id: Translator.pm,v 1.5 2002-03-26 12:46:54 dlc Exp $ # ---------------------------------------------------------------------- # Copyright (C) 2002 Ken Y. Clark , # darren chamberlain @@ -57,7 +57,7 @@ contributing their parsers or producers back to the project. use strict; use vars qw($VERSION $DEFAULT_SUB $DEBUG); -$VERSION = sprintf "%d.%02d", q$Revision: 1.4 $ =~ /(\d+)\.(\d+)/; +$VERSION = sprintf "%d.%02d", q$Revision: 1.5 $ =~ /(\d+)\.(\d+)/; $DEBUG = 1 unless defined $DEBUG; # ---------------------------------------------------------------------- @@ -126,6 +126,13 @@ sub new { $self->producer($args->{'producer'} || $args->{'to'} || $DEFAULT_SUB); # ------------------------------------------------------------------ + # Set the parser_args and producer_args + # ------------------------------------------------------------------ + for my $pargs (qw(parser_args producer_args)) { + $self->$pargs($args->{$pargs}) if defined $args->{$pargs}; + } + + # ------------------------------------------------------------------ # Clear the error # ------------------------------------------------------------------ $self->error_out(""); @@ -173,7 +180,7 @@ instead of the default "produce". # This will invoke My::Groovy::Producer::transmogrify($tr, $data), # assuming that My::Groovy::Producer::transmogrify is not a module # on disk. - # $tr->producer("My::Groovy::Producer::transmogrify); + $tr->producer("My::Groovy::Producer::transmogrify"); # This will invoke the referenced subroutine directly, as # $subref->($tr, $data); @@ -184,6 +191,25 @@ containing the classname to which the above B function belongs. In the case of anonymous subroutines, this method returns the string "CODE". +Finally, there is a method named B, which is both an +accessor and a mutator. Arbitrary data may be stored in name => value +pairs for the producer subroutine to access: + + sub My::Random::producer { + my ($tr, $data) = @_; + my $pr_args = $tr->producer_args(); + + # $pr_args is a hashref. + +Extra data passed to the B method is passed to +B: + + $tr->producer("xSV", delimiter => ',\s*'); + + # In SQL::Translator::Producer::xSV: + my $args = $tr->producer_args; + my $delimiter = $args->{'delimiter'}; # value is => ,\s* + =cut # {{{ producer and producer_type @@ -221,7 +247,7 @@ sub producer { # {{{ get code reference and assign $self->{'producer'} = \&{ "$producer\::$func_name" }; $self->{'producer_type'} = $producer; - $self->debug("Got 'producer': $producer\::$func_name"); + $self->debug("Got producer: $producer\::$func_name"); # }}} } # }}} @@ -243,12 +269,34 @@ sub producer { # At this point, $self->{'producer'} contains a subroutine # reference that is ready to run + + # {{{ Anything left? If so, it's producer_args + $self->produser_args(@_) if (@_); + # }}} } # }}} return $self->{'producer'}; }; -sub producer_type { $_[0]->{'producer_type'} } +# {{{ producer_type +# producer_type is an accessor that allows producer subs to get +# information about their origin. This is poptentially important; +# since all producer subs are called as subroutine refernces, there is +# no way for a producer to find out which package the sub lives in +# originally, for example. +sub producer_type { $_[0]->{'producer_type'} } # }}} + +# {{{ producer_args +# Arbitrary name => value pairs of paramters can be passed to a +# producer using this method. +sub producer_args { + my $self = shift; + if (@_) { + my $args = isa($_[0], 'HASH') ? shift : { @_ }; + $self->{'producer_args'} = $args; + } + $self->{'producer_args'}; +} # }}} # }}} =head2 B @@ -273,9 +321,12 @@ entirety of the data to be parsed (or possibly a reference to a string?). return $dumper->Dump; }); +There is also B and B, which perform +analogously to B and B + =cut -# {{{ parser and parser_type +# {{{ parser, parser_type, and parser_args sub parser { my $self = shift; @@ -332,13 +383,24 @@ sub parser { # At this point, $self->{'parser'} contains a subroutine # reference that is ready to run - } # }}} + $self->parser_args(@_) if (@_); + } # }}} return $self->{'parser'}; } sub parser_type { $_[0]->{'parser_type'} } + +# {{{ parser_args +sub parser_args { + my $self = shift; + if (@_) { + my $args = isa($_[0], 'HASH') ? shift : { @_ }; + $self->{'parser_args'} = $args; + } + $self->{'parser_args'}; +} # }}} # }}} =head2 B