Added PostgreSQL producer.
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator.pm
CommitLineData
16dc9970 1package SQL::Translator;
2
b346d8f1 3# ----------------------------------------------------------------------
49e1eb70 4# $Id: Translator.pm,v 1.10 2002-11-20 04:03:03 kycl4rk Exp $
b346d8f1 5# ----------------------------------------------------------------------
077ebf34 6# Copyright (C) 2002 Ken Y. Clark <kycl4rk@users.sourceforge.net>,
7# darren chamberlain <darren@cpan.org>
1fd8c91f 8#
077ebf34 9# This program is free software; you can redistribute it and/or
10# modify it under the terms of the GNU General Public License as
11# published by the Free Software Foundation; version 2.
ca10f295 12#
077ebf34 13# This program is distributed in the hope that it will be useful, but
14# WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16# General Public License for more details.
ca10f295 17#
077ebf34 18# You should have received a copy of the GNU General Public License
19# along with this program; if not, write to the Free Software
20# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21# 02111-1307 USA
ca10f295 22# -------------------------------------------------------------------
23
24=head1 NAME
25
26SQL::Translator - convert schema from one database to another
27
28=head1 SYNOPSIS
29
30 use SQL::Translator;
31 my $translator = SQL::Translator->new;
49e1eb70 32 my $output = $translator->translate(
33 from => "MySQL",
34 to => "Oracle",
35 filename => $file,
36 ) or die $translator->error;
ca10f295 37 print $output;
38
39=head1 DESCRIPTION
40
41This module attempts to simplify the task of converting one database
7a8e1f51 42create syntax to another through the use of Parsers (which understand
43the sourced format) and Producers (which understand the destination
44format). The idea is that any Parser can be used with any Producer in
45the conversion process. So, if you wanted PostgreSQL-to-Oracle, you
46would use the PostgreSQL parser and the Oracle producer.
ca10f295 47
48=cut
16dc9970 49
50use strict;
49e1eb70 51use vars qw( $VERSION $DEFAULT_SUB $DEBUG $ERROR );
52use base 'Class::Base';
c2d3a526 53
49e1eb70 54$VERSION = sprintf "%d.%02d", q$Revision: 1.10 $ =~ /(\d+)\.(\d+)/;
55$DEBUG = 0 unless defined $DEBUG;
56$ERROR = "";
c2d3a526 57
58use Carp qw(carp);
16dc9970 59
b346d8f1 60# ----------------------------------------------------------------------
61# The default behavior is to "pass through" values (note that the
62# SQL::Translator instance is the first value ($_[0]), and the stuff
63# to be parsed is the second value ($_[1])
64# ----------------------------------------------------------------------
65$DEFAULT_SUB = sub { $_[1] } unless defined $DEFAULT_SUB;
16dc9970 66
ca10f295 67=head1 CONSTRUCTOR
16dc9970 68
077ebf34 69The constructor is called B<new>, and accepts a optional hash of options.
ca10f295 70Valid options are:
16dc9970 71
ca10f295 72=over 4
73
74=item parser (aka from)
75
9398955f 76=item parser_args
77
ca10f295 78=item producer (aka to)
79
9398955f 80=item producer_args
81
82=item filename (aka file)
83
84=item data
85
86=item debug
ca10f295 87
88=back
89
90All options are, well, optional; these attributes can be set via
b346d8f1 91instance methods. Internally, they are; no (non-syntactical)
92advantage is gained by passing options to the constructor.
ca10f295 93
94=cut
95
b346d8f1 96# ----------------------------------------------------------------------
c2d3a526 97# init([ARGS])
b346d8f1 98# The constructor.
dfb4c915 99#
b346d8f1 100# new takes an optional hash of arguments. These arguments may
101# include a parser, specified with the keys "parser" or "from",
102# and a producer, specified with the keys "producer" or "to".
dfb4c915 103#
b346d8f1 104# The values that can be passed as the parser or producer are
105# given directly to the parser or producer methods, respectively.
106# See the appropriate method description below for details about
107# what each expects/accepts.
b346d8f1 108# ----------------------------------------------------------------------
c2d3a526 109sub init {
49e1eb70 110 my ( $self, $config ) = @_;
1fd8c91f 111
49e1eb70 112 #
b346d8f1 113 # Set the parser and producer.
ca10f295 114 #
b346d8f1 115 # If a 'parser' or 'from' parameter is passed in, use that as the
116 # parser; if a 'producer' or 'to' parameter is passed in, use that
117 # as the producer; both default to $DEFAULT_SUB.
49e1eb70 118 #
119 $self->parser ($config->{'parser'} || $config->{'from'} || $DEFAULT_SUB);
c2d3a526 120 $self->producer($config->{'producer'} || $config->{'to'} || $DEFAULT_SUB);
ca10f295 121
49e1eb70 122 #
e2158c40 123 # Set the parser_args and producer_args
49e1eb70 124 #
125 for my $pargs ( qw[ parser_args producer_args ] ) {
126 $self->$pargs( $config->{$pargs} ) if defined $config->{ $pargs };
e2158c40 127 }
128
49e1eb70 129 #
9398955f 130 # Set the data source, if 'filename' or 'file' is provided.
49e1eb70 131 #
c2d3a526 132 $config->{'filename'} ||= $config->{'file'} || "";
49e1eb70 133 $self->filename( $config->{'filename'} ) if $config->{'filename'};
9398955f 134
49e1eb70 135 #
136 # Finally, if there is a 'data' parameter, use that in
137 # preference to filename and file
138 #
139 if ( my $data = $config->{'data'} ) {
140 $self->data( $data );
9398955f 141 }
142
49e1eb70 143 $self->{'debug'} = defined $config->{'debug'} ? $config->{'debug'} : $DEBUG;
ca10f295 144
145 return $self;
dfb4c915 146}
1fd8c91f 147
ca10f295 148=head1 METHODS
149
ca10f295 150=head2 B<producer>
151
152The B<producer> method is an accessor/mutator, used to retrieve or
153define what subroutine is called to produce the output. A subroutine
b346d8f1 154defined as a producer will be invoked as a function (not a method) and
155passed 2 parameters: its container SQL::Translator instance and a
156data structure. It is expected that the function transform the data
157structure to a string. The SQL::Transformer instance is provided for
158informational purposes; for example, the type of the parser can be
159retrieved using the B<parser_type> method, and the B<error> and
160B<debug> methods can be called when needed.
161
162When defining a producer, one of several things can be passed
163in: A module name (e.g., My::Groovy::Producer), a module name
164relative to the SQL::Translator::Producer namespace (e.g., MySQL), a
165module name and function combination (My::Groovy::Producer::transmogrify),
166or a reference to an anonymous subroutine. If a full module name is
167passed in (for the purposes of this method, a string containing "::"
168is considered to be a module name), it is treated as a package, and a
169function called "produce" will be invoked: $modulename::produce. If
170$modulename cannot be loaded, the final portion is stripped off and
171treated as a function. In other words, if there is no file named
172My/Groovy/Producer/transmogrify.pm, SQL::Translator will attempt to load
173My/Groovy/Producer.pm and use transmogrify as the name of the function,
174instead of the default "produce".
ca10f295 175
176 my $tr = SQL::Translator->new;
177
077ebf34 178 # This will invoke My::Groovy::Producer::produce($tr, $data)
ca10f295 179 $tr->producer("My::Groovy::Producer");
180
077ebf34 181 # This will invoke SQL::Translator::Producer::Sybase::produce($tr, $data)
ca10f295 182 $tr->producer("Sybase");
183
b346d8f1 184 # This will invoke My::Groovy::Producer::transmogrify($tr, $data),
185 # assuming that My::Groovy::Producer::transmogrify is not a module
186 # on disk.
e2158c40 187 $tr->producer("My::Groovy::Producer::transmogrify");
b346d8f1 188
077ebf34 189 # This will invoke the referenced subroutine directly, as
190 # $subref->($tr, $data);
ca10f295 191 $tr->producer(\&my_producer);
192
077ebf34 193There is also a method named B<producer_type>, which is a string
194containing the classname to which the above B<produce> function
195belongs. In the case of anonymous subroutines, this method returns
196the string "CODE".
197
e2158c40 198Finally, there is a method named B<producer_args>, which is both an
199accessor and a mutator. Arbitrary data may be stored in name => value
200pairs for the producer subroutine to access:
201
202 sub My::Random::producer {
203 my ($tr, $data) = @_;
204 my $pr_args = $tr->producer_args();
205
206 # $pr_args is a hashref.
207
208Extra data passed to the B<producer> method is passed to
209B<producer_args>:
210
211 $tr->producer("xSV", delimiter => ',\s*');
212
213 # In SQL::Translator::Producer::xSV:
214 my $args = $tr->producer_args;
7a8e1f51 215 my $delimiter = $args->{'delimiter'}; # value is ,\s*
e2158c40 216
ca10f295 217=cut
ca10f295 218
7a8e1f51 219# producer and producer_type
ca10f295 220sub producer {
1fd8c91f 221 my $self = shift;
b346d8f1 222
7a8e1f51 223 # producer as a mutator
ca10f295 224 if (@_) {
225 my $producer = shift;
b346d8f1 226
7a8e1f51 227 # Passed a module name (string containing "::")
ca10f295 228 if ($producer =~ /::/) {
077ebf34 229 my $func_name;
b346d8f1 230
7a8e1f51 231 # Module name was passed directly
b346d8f1 232 # We try to load the name; if it doesn't load, there's
233 # a possibility that it has a function name attached to
234 # it.
077ebf34 235 if (load($producer)) {
236 $func_name = "produce";
7a8e1f51 237 }
b346d8f1 238
7a8e1f51 239 # Module::function was passed
b346d8f1 240 else {
241 # Passed Module::Name::function; try to recover
077ebf34 242 my @func_parts = split /::/, $producer;
243 $func_name = pop @func_parts;
244 $producer = join "::", @func_parts;
b346d8f1 245
246 # If this doesn't work, then we have a legitimate
247 # problem.
077ebf34 248 load($producer) or die "Can't load $producer: $@";
7a8e1f51 249 }
077ebf34 250
7a8e1f51 251 # get code reference and assign
077ebf34 252 $self->{'producer'} = \&{ "$producer\::$func_name" };
253 $self->{'producer_type'} = $producer;
49e1eb70 254 $self->debug("Got producer: $producer\::$func_name\n");
7a8e1f51 255 }
b346d8f1 256
7a8e1f51 257 # passed an anonymous subroutine reference
b346d8f1 258 elsif (isa($producer, 'CODE')) {
ca10f295 259 $self->{'producer'} = $producer;
077ebf34 260 $self->{'producer_type'} = "CODE";
49e1eb70 261 $self->debug("Got producer: code ref\n");
7a8e1f51 262 }
b346d8f1 263
7a8e1f51 264 # passed a string containing no "::"; relative package name
b346d8f1 265 else {
ca10f295 266 my $Pp = sprintf "SQL::Translator::Producer::$producer";
267 load($Pp) or die "Can't load $Pp: $@";
077ebf34 268 $self->{'producer'} = \&{ "$Pp\::produce" };
269 $self->{'producer_type'} = $Pp;
49e1eb70 270 $self->debug("Got producer: $Pp\n");
7a8e1f51 271 }
b346d8f1 272
ca10f295 273 # At this point, $self->{'producer'} contains a subroutine
b346d8f1 274 # reference that is ready to run
e2158c40 275
7a8e1f51 276 # Anything left? If so, it's producer_args
277 $self->producer_args(@_) if (@_);
278 }
b346d8f1 279
ca10f295 280 return $self->{'producer'};
281};
077ebf34 282
7a8e1f51 283# ----------------------------------------------------------------------
284# producer_type
285#
e2158c40 286# producer_type is an accessor that allows producer subs to get
287# information about their origin. This is poptentially important;
288# since all producer subs are called as subroutine refernces, there is
289# no way for a producer to find out which package the sub lives in
290# originally, for example.
7a8e1f51 291# ----------------------------------------------------------------------
292sub producer_type { $_[0]->{'producer_type'} }
e2158c40 293
7a8e1f51 294# ----------------------------------------------------------------------
295# producer_args
296#
e2158c40 297# Arbitrary name => value pairs of paramters can be passed to a
298# producer using this method.
7a8e1f51 299# ----------------------------------------------------------------------
e2158c40 300sub producer_args {
301 my $self = shift;
302 if (@_) {
303 my $args = isa($_[0], 'HASH') ? shift : { @_ };
304 $self->{'producer_args'} = $args;
305 }
306 $self->{'producer_args'};
7a8e1f51 307}
ca10f295 308
309=head2 B<parser>
310
311The B<parser> method defines or retrieves a subroutine that will be
312called to perform the parsing. The basic idea is the same as that of
313B<producer> (see above), except the default subroutine name is
077ebf34 314"parse", and will be invoked as $module_name::parse($tr, $data).
315Also, the parser subroutine will be passed a string containing the
316entirety of the data to be parsed (or possibly a reference to a string?).
ca10f295 317
318 # Invokes SQL::Translator::Parser::MySQL::parse()
319 $tr->parser("MySQL");
320
321 # Invokes My::Groovy::Parser::parse()
322 $tr->parser("My::Groovy::Parser");
323
324 # Invoke an anonymous subroutine directly
325 $tr->parser(sub {
077ebf34 326 my $dumper = Data::Dumper->new([ $_[1] ], [ "SQL" ]);
ca10f295 327 $dumper->Purity(1)->Terse(1)->Deepcopy(1);
328 return $dumper->Dump;
329 });
330
e2158c40 331There is also B<parser_type> and B<parser_args>, which perform
332analogously to B<producer_type> and B<producer_args>
333
ca10f295 334=cut
335
ca10f295 336sub parser {
337 my $self = shift;
b346d8f1 338
7a8e1f51 339 # parser as a mutator
ca10f295 340 if (@_) {
341 my $parser = shift;
b346d8f1 342
7a8e1f51 343 # Passed a module name (string containing "::")
ca10f295 344 if ($parser =~ /::/) {
b346d8f1 345 my $func_name;
346
7a8e1f51 347 # Module name was passed directly
b346d8f1 348 # We try to load the name; if it doesn't load, there's
349 # a possibility that it has a function name attached to
350 # it.
351 if (load($parser)) {
352 $func_name = "parse";
7a8e1f51 353 }
b346d8f1 354
7a8e1f51 355 # Module::function was passed
b346d8f1 356 else {
357 # Passed Module::Name::function; try to recover
358 my @func_parts = split /::/, $parser;
359 $func_name = pop @func_parts;
360 $parser = join "::", @func_parts;
361
362 # If this doesn't work, then we have a legitimate
363 # problem.
364 load($parser) or die "Can't load $parser: $@";
7a8e1f51 365 }
b346d8f1 366
7a8e1f51 367 # get code reference and assign
b346d8f1 368 $self->{'parser'} = \&{ "$parser\::$func_name" };
077ebf34 369 $self->{'parser_type'} = $parser;
49e1eb70 370 $self->debug("Got parser: $parser\::$func_name\n");
7a8e1f51 371 }
b346d8f1 372
7a8e1f51 373 # passed an anonymous subroutine reference
49e1eb70 374 elsif ( isa( $parser, 'CODE' ) ) {
375 $self->{'parser'} = $parser;
077ebf34 376 $self->{'parser_type'} = "CODE";
49e1eb70 377 $self->debug("Got parser: code ref\n");
7a8e1f51 378 }
b346d8f1 379
7a8e1f51 380 # passed a string containing no "::"; relative package name
b346d8f1 381 else {
49e1eb70 382 my $Pp = "SQL::Translator::Parser::$parser";
383 load( $Pp ) or die "Can't load $Pp: $@";
384 $self->{'parser'} = \&{ "$Pp\::parse" };
077ebf34 385 $self->{'parser_type'} = $Pp;
49e1eb70 386 $self->debug("Got parser: $Pp\n");
7a8e1f51 387 }
b346d8f1 388
49e1eb70 389 #
b346d8f1 390 # At this point, $self->{'parser'} contains a subroutine
391 # reference that is ready to run
49e1eb70 392 #
393 $self->parser_args( @_ ) if (@_);
7a8e1f51 394 }
b346d8f1 395
ca10f295 396 return $self->{'parser'};
16dc9970 397}
1fd8c91f 398
077ebf34 399sub parser_type { $_[0]->{'parser_type'} }
e2158c40 400
7a8e1f51 401# parser_args
e2158c40 402sub parser_args {
403 my $self = shift;
404 if (@_) {
405 my $args = isa($_[0], 'HASH') ? shift : { @_ };
406 $self->{'parser_args'} = $args;
407 }
408 $self->{'parser_args'};
7a8e1f51 409}
16dc9970 410
ca10f295 411=head2 B<translate>
412
413The B<translate> method calls the subroutines referenced by the
414B<parser> and B<producer> data members (described above). It accepts
415as arguments a number of things, in key => value format, including
416(potentially) a parser and a producer (they are passed directly to the
417B<parser> and B<producer> methods).
418
419Here is how the parameter list to B<translate> is parsed:
420
421=over
422
423=item *
424
4251 argument means it's the data to be parsed; which could be a string
b346d8f1 426(filename) or a refernce to a scalar (a string stored in memory), or a
427reference to a hash, which is parsed as being more than one argument
428(see next section).
ca10f295 429
430 # Parse the file /path/to/datafile
431 my $output = $tr->translate("/path/to/datafile");
432
b346d8f1 433 # Parse the data contained in the string $data
ca10f295 434 my $output = $tr->translate(\$data);
435
436=item *
437
077ebf34 438More than 1 argument means its a hash of things, and it might be
439setting a parser, producer, or datasource (this key is named
b346d8f1 440"filename" or "file" if it's a file, or "data" for a SCALAR reference.
ca10f295 441
442 # As above, parse /path/to/datafile, but with different producers
443 for my $prod ("MySQL", "XML", "Sybase") {
444 print $tr->translate(
445 producer => $prod,
446 filename => "/path/to/datafile",
447 );
448 }
449
450 # The filename hash key could also be:
ca10f295 451 datasource => \$data,
452
453You get the idea.
454
455=back
456
9398955f 457=head2 B<filename>, B<data>
458
459Using the B<filename> method, the filename of the data to be parsed
460can be set. This method can be used in conjunction with the B<data>
461method, below. If both the B<filename> and B<data> methods are
462invoked as mutators, the data set in the B<data> method is used.
463
464 $tr->filename("/my/data/files/create.sql");
465
466or:
467
468 my $create_script = do {
469 local $/;
470 open CREATE, "/my/data/files/create.sql" or die $!;
471 <CREATE>;
472 };
473 $tr->data(\$create_script);
474
475B<filename> takes a string, which is interpreted as a filename.
38254289 476B<data> takes a reference to a string, which is used as the data to be
9398955f 477parsed. If a filename is set, then that file is opened and read when
478the B<translate> method is called, as long as the data instance
479variable is not set.
480
ca10f295 481=cut
482
7a8e1f51 483# filename - get or set the filename
9398955f 484sub filename {
485 my $self = shift;
486 if (@_) {
7a8e1f51 487 my $filename = shift;
488 if (-d $filename) {
489 my $msg = "Cannot use directory '$filename' as input source";
c2d3a526 490 return $self->error($msg);
7a8e1f51 491 } elsif (-f _ && -r _) {
492 $self->{'filename'} = $filename;
49e1eb70 493 $self->debug("Got filename: '$self->{'filename'}'\n");
7a8e1f51 494 } else {
495 my $msg = "Cannot use '$filename' as input source: ".
496 "file does not exist or is not readable.";
c2d3a526 497 return $self->error($msg);
7a8e1f51 498 }
9398955f 499 }
7a8e1f51 500
9398955f 501 $self->{'filename'};
7a8e1f51 502}
9398955f 503
7a8e1f51 504# data - get or set the data
9398955f 505# if $self->{'data'} is not set, but $self->{'filename'} is, then
506# $self->{'filename'} is opened and read, whith the results put into
507# $self->{'data'}.
508sub data {
509 my $self = shift;
510
7a8e1f51 511 # Set $self->{'data'} to $_[0], if it is provided.
9398955f 512 if (@_) {
513 my $data = shift;
514 if (isa($data, "SCALAR")) {
515 $self->{'data'} = $data;
516 }
517 elsif (! ref $data) {
518 $self->{'data'} = \$data;
519 }
520 }
9398955f 521
7a8e1f51 522 # If we have a filename but no data yet, populate.
9398955f 523 if (not $self->{'data'} and my $filename = $self->filename) {
49e1eb70 524 $self->debug("Opening '$filename' to get contents.\n");
9398955f 525 local *FH;
526 local $/;
527 my $data;
528
529 unless (open FH, $filename) {
49e1eb70 530 return $self->error("Can't read file '$filename': $!");
9398955f 531 }
532
533 $data = <FH>;
534 $self->{'data'} = \$data;
535
536 unless (close FH) {
49e1eb70 537 return $self->error("Can't close file '$filename': $!");
9398955f 538 }
539 }
9398955f 540
541 return $self->{'data'};
7a8e1f51 542}
9398955f 543
7a8e1f51 544# translate
16dc9970 545sub translate {
ca10f295 546 my $self = shift;
7a8e1f51 547 my ($args, $parser, $parser_type, $producer, $producer_type);
548 my ($parser_output, $producer_output);
ca10f295 549
7a8e1f51 550 # Parse arguments
9398955f 551 if (@_ == 1) {
7a8e1f51 552 # Passed a reference to a hash?
ca10f295 553 if (isa($_[0], 'HASH')) {
7a8e1f51 554 # yep, a hashref
49e1eb70 555 $self->debug("translate: Got a hashref\n");
ca10f295 556 $args = $_[0];
557 }
9398955f 558
7a8e1f51 559 # Passed a reference to a string containing the data
ca10f295 560 elsif (isa($_[0], 'SCALAR')) {
9398955f 561 # passed a ref to a string
49e1eb70 562 $self->debug("translate: Got a SCALAR reference (string)\n");
9398955f 563 $self->data($_[0]);
ca10f295 564 }
9398955f 565
7a8e1f51 566 # Not a reference; treat it as a filename
b346d8f1 567 elsif (! ref $_[0]) {
ca10f295 568 # Not a ref, it's a filename
49e1eb70 569 $self->debug("translate: Got a filename\n");
9398955f 570 $self->filename($_[0]);
ca10f295 571 }
9398955f 572
7a8e1f51 573 # Passed something else entirely.
b346d8f1 574 else {
575 # We're not impressed. Take your empty string and leave.
38254289 576 # return "";
577
7a8e1f51 578 # Actually, if data, parser, and producer are set, then we
579 # can continue. Too bad, because I like my comment
580 # (above)...
38254289 581 return "" unless ($self->data &&
582 $self->producer &&
583 $self->parser);
b346d8f1 584 }
16dc9970 585 }
586 else {
b346d8f1 587 # You must pass in a hash, or you get nothing.
588 return "" if @_ % 2;
ca10f295 589 $args = { @_ };
7a8e1f51 590 }
16dc9970 591
9398955f 592 # ----------------------------------------------------------------------
593 # Can specify the data to be transformed using "filename", "file",
7a8e1f51 594 # "data", or "datasource".
9398955f 595 # ----------------------------------------------------------------------
7a8e1f51 596 if (my $filename = ($args->{'filename'} || $args->{'file'})) {
9398955f 597 $self->filename($filename);
598 }
ca10f295 599
7a8e1f51 600 if (my $data = ($self->{'data'} || $self->{'datasource'})) {
9398955f 601 $self->data($data);
16dc9970 602 }
ca10f295 603
9398955f 604 # ----------------------------------------------------------------
605 # Get the data.
606 # ----------------------------------------------------------------
607 my $data = $self->data;
7a8e1f51 608 unless (length $$data) {
c2d3a526 609 return $self->error("Empty data file!");
9398955f 610 }
077ebf34 611
9398955f 612 # ----------------------------------------------------------------
ca10f295 613 # Local reference to the parser subroutine
9398955f 614 # ----------------------------------------------------------------
ca10f295 615 if ($parser = ($args->{'parser'} || $args->{'from'})) {
616 $self->parser($parser);
16dc9970 617 }
7a8e1f51 618 $parser = $self->parser;
619 $parser_type = $self->parser_type;
16dc9970 620
9398955f 621 # ----------------------------------------------------------------
ca10f295 622 # Local reference to the producer subroutine
9398955f 623 # ----------------------------------------------------------------
ca10f295 624 if ($producer = ($args->{'producer'} || $args->{'to'})) {
625 $self->producer($producer);
16dc9970 626 }
7a8e1f51 627 $producer = $self->producer;
628 $producer_type = $self->producer_type;
16dc9970 629
9398955f 630 # ----------------------------------------------------------------
7a8e1f51 631 # Execute the parser, then execute the producer with that output.
632 # Allowances are made for each piece to die, or fail to compile,
633 # since the referenced subroutines could be almost anything. In
634 # the future, each of these might happen in a Safe environment,
635 # depending on how paranoid we want to be.
9398955f 636 # ----------------------------------------------------------------
7a8e1f51 637 eval { $parser_output = $parser->($self, $$data) };
638 if ($@ || ! $parser_output) {
639 my $msg = sprintf "translate: Error with parser '%s': %s",
640 $parser_type, ($@) ? $@ : " no results";
c2d3a526 641 return $self->error($msg);
7a8e1f51 642 }
643
644 eval { $producer_output = $producer->($self, $parser_output) };
645 if ($@ || ! $producer_output) {
646 my $msg = sprintf "translate: Error with producer '%s': %s",
647 $producer_type, ($@) ? $@ : " no results";
c2d3a526 648 return $self->error($msg);
7a8e1f51 649 }
650
651 return $producer_output;
16dc9970 652}
ca10f295 653
ca10f295 654sub load {
655 my $module = do { my $m = shift; $m =~ s[::][/]g; "$m.pm" };
656 return 1 if $INC{$module};
657
658 eval { require $module };
659
660 return if ($@);
661 return 1;
1fd8c91f 662}
16dc9970 663
c2d3a526 664sub isa { UNIVERSAL::isa($_[0], $_[1]) }
665
16dc9970 6661;
667
668#-----------------------------------------------------
669# Rescue the drowning and tie your shoestrings.
670# Henry David Thoreau
671#-----------------------------------------------------
672
49e1eb70 673=pod
674
7a8e1f51 675=head1 AUTHORS
16dc9970 676
ca10f295 677Ken Y. Clark, E<lt>kclark@logsoft.comE<gt>,
678darren chamberlain E<lt>darren@cpan.orgE<gt>
dfb4c915 679
ca10f295 680=head1 COPYRIGHT
16dc9970 681
ca10f295 682This program is free software; you can redistribute it and/or modify
683it under the terms of the GNU General Public License as published by
684the Free Software Foundation; version 2.
dfb4c915 685
ca10f295 686This program is distributed in the hope that it will be useful, but
687WITHOUT ANY WARRANTY; without even the implied warranty of
688MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
689General Public License for more details.
16dc9970 690
ca10f295 691You should have received a copy of the GNU General Public License
692along with this program; if not, write to the Free Software
693Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
694USA
16dc9970 695
696=head1 SEE ALSO
697
ca10f295 698L<perl>, L<Parse::RecDescent>
16dc9970 699
700=cut