1 package SQL::Translator::Utils;
3 # ----------------------------------------------------------------------
4 # $Id: Utils.pm,v 1.12 2004-02-09 23:04:26 kycl4rk Exp $
5 # ----------------------------------------------------------------------
6 # Copyright (C) 2002-4 SQLFairy Authors
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License as
10 # published by the Free Software Foundation; version 2.
12 # This program is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 # -------------------------------------------------------------------
24 use base qw(Exporter);
25 use vars qw($VERSION $DEFAULT_COMMENT @EXPORT_OK);
29 $VERSION = sprintf "%d.%02d", q$Revision: 1.12 $ =~ /(\d+)\.(\d+)/;
30 $DEFAULT_COMMENT = '-- ';
32 debug normalize_name header_comment parse_list_arg $DEFAULT_COMMENT
35 # ----------------------------------------------------------------------
38 # Will send debugging messages to STDERR, if the caller's $DEBUG global
41 # This debug() function has a neat feature: Occurances of the strings
42 # PKG, LINE, and SUB in each message will be replaced with elements
45 # debug("PKG: Bad things happened on line LINE!");
49 # [SQL::Translator: Bad things happened on line 643]
51 # If called from Translator.pm, on line 643.
52 # ----------------------------------------------------------------------
54 my ($pkg, $file, $line, $sub) = caller(0);
57 return unless ${"$pkg\::DEBUG"};
65 $x =~ s/\bPKG\b/$pkg/g;
66 $x =~ s/\bLINE\b/$line/g;
67 $x =~ s/\bSUB\b/$sub/g;
68 #warn '[' . $x . "]\n";
69 print STDERR '[' . $x . "]\n";
73 # ----------------------------------------------------------------------
75 my $name = shift or return '';
77 # The name can only begin with a-zA-Z_; if there's anything
79 $name =~ s/^([^a-zA-Z_])/_$1/;
81 # anything other than a-zA-Z0-9_ in the non-first position
82 # needs to be turned into _
83 $name =~ tr/[a-zA-Z0-9_]/_/c;
85 # All duplicated _ need to be squashed into one.
94 # ----------------------------------------------------------------------
96 my $producer = shift || caller;
97 my $comment_char = shift;
98 my $now = scalar localtime;
100 $comment_char = $DEFAULT_COMMENT
101 unless defined $comment_char;
103 my $header_comment =<<"HEADER_COMMENT";
105 ${comment_char}Created by $producer
106 ${comment_char}Created on $now
110 # Any additional stuff passed in
111 for my $additional_comment (@_) {
112 $header_comment .= "${comment_char}${additional_comment}\n";
115 return $header_comment;
118 # ----------------------------------------------------------------------
121 # Meant to accept a list, an array reference, or a string of
122 # comma-separated values. Retuns an array reference of the
123 # arguments. Modified to also handle a list of references.
124 # ----------------------------------------------------------------------
126 my $list = UNIVERSAL::isa( $_[0], 'ARRAY' ) ? shift : [ @_ ];
129 # This protects stringification of references.
131 if ( @$list && ref $list->[0] ) {
135 # This processes string-like arguments.
139 map { s/^\s+|\s+$//g; $_ }
141 grep { defined && length } @$list
148 # ----------------------------------------------------------------------
154 SQL::Translator::Utils - SQL::Translator Utility functions
158 use SQL::Translator::Utils qw(debug);
159 debug("PKG: Bad things happened");
163 C<SQL::Translator::Utils> contains utility functions designed to be
164 used from the other modules within the C<SQL::Translator> modules.
166 Nothing is exported by default.
168 =head1 EXPORTED FUNCTIONS AND CONSTANTS
172 C<debug> takes 0 or more messages, which will be sent to STDERR using
173 C<warn>. Occurances of the strings I<PKG>, I<SUB>, and I<LINE>
174 will be replaced by the calling package, subroutine, and line number,
175 respectively, as reported by C<caller(1)>.
177 For example, from within C<foo> in F<SQL/Translator.pm>, at line 666:
179 debug("PKG: Error reading file at SUB/LINE");
183 [SQL::Translator: Error reading file at foo/666]
185 The entire message is enclosed within C<[> and C<]> for visual clarity
186 when STDERR is intermixed with STDOUT.
188 =head2 normalize_name
190 C<normalize_name> takes a string and ensures that it is suitable for
191 use as an identifier. This means: ensure that it starts with a letter
192 or underscore, and that the rest of the string consists of only
193 letters, numbers, and underscores. A string that begins with
194 something other than [a-zA-Z] will be prefixer with an underscore, and
195 all other characters in the string will be replaced with underscores.
196 Finally, a trailing underscore will be removed, because that's ugly.
198 normalize_name("Hello, world");
204 A more useful example, from the C<SQL::Translator::Parser::Excel> test
207 normalize_name("silly field (with random characters)");
211 silly_field_with_random_characters
213 =head2 header_comment
215 Create the header comment. Takes 1 mandatory argument (the producer
216 classname), an optional comment character (defaults to $DEFAULT_COMMENT),
217 and 0 or more additional comments, which will be appended to the header,
218 prefixed with the comment character. If additional comments are provided,
219 then a comment string must be provided ($DEFAULT_COMMENT is exported for
220 this use). For example, this:
222 package My::Producer;
224 use SQL::Translator::Utils qw(header_comment $DEFAULT_COMMENT);
226 print header_comment(__PACKAGE__,
233 -- Created by My::Prodcuer
234 -- Created on Fri Apr 25 06:56:02 2003
239 Note the gratuitous spacing.
241 =head2 parse_list_arg
243 Takes a string, list or arrayref (all of which could contain
244 comma-separated values) and returns an array reference of the values.
245 All of the following will return equivalent values:
247 parse_list_arg('id');
248 parse_list_arg('id', 'name');
249 parse_list_arg( 'id, name' );
250 parse_list_arg( [ 'id', 'name' ] );
251 parse_list_arg( qw[ id name ] );
253 =head2 $DEFAULT_COMMENT
255 This is the default comment string, '-- ' by default. Useful for
260 Darren Chamberlain E<lt>darren@cpan.orgE<gt>,
261 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.