1 package SQL::Translator::Utils;
3 # ----------------------------------------------------------------------
4 # $Id: Utils.pm,v 1.2 2003-04-17 13:41:36 dlc Exp $
5 # ----------------------------------------------------------------------
6 # Copyright (C) 2003 darren chamberlain <darren@cpan.org>
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 @EXPORT_OK);
30 @EXPORT_OK = ('debug', 'normalize_name');
32 # ----------------------------------------------------------------------
35 # Will send debugging messages to STDERR, if the caller's $DEBUG global
38 # This debug() function has a neat feature: Occurances of the strings
39 # PKG, LINE, and SUB in each message will be replaced with elements
42 # debug("PKG: Bad things happened on line LINE!");
46 # [SQL::Translator: Bad things happened on line 643]
48 # If called from Translator.pm, on line 643.
49 # ----------------------------------------------------------------------
51 my ($pkg, $file, $line, $sub) = caller(1);
54 return unless ${"$pkg\::DEBUG"};
62 $x =~ s/\bPKG\b/$pkg/g;
63 $x =~ s/\bLINE\b/$line/g;
64 $x =~ s/\bSUB\b/$sub/g;
65 #warn '[' . $x . "]\n";
66 print STDERR '[' . $x . "]\n";
74 # The name can only begin with a-zA-Z_; if there's anything
76 $name =~ s/^([^a-zA-Z_])/_$1/;
78 # anything other than a-zA-Z0-9_ in the non-first position
79 # needs to be turned into _
80 $name =~ tr/[a-zA-Z0-9_]/_/c;
82 # All duplicated _ need to be squashed into one.
97 SQL::Translator::Utils - SQL::Translator Utility functions
101 use SQL::Translator::Utils qw(debug);
102 debug("PKG: Bad things happened");
106 C<SQL::Translator::Utils> contains utility functions designed to be
107 used from the other modules within the C<SQL::Translator> modules.
109 No functions are exported by default.
111 =head1 EXPORTED FUNCTIONS
115 C<debug> takes 0 or more messages, which will be sent to STDERR using
116 C<warn>. Occurances of the strings I<PKG>, I<SUB>, and I<LINE>
117 will be replaced by the calling package, subroutine, and line number,
118 respectively, as reported by C<caller(1)>.
120 For example, from within C<foo> in F<SQL/Translator.pm>, at line 666:
122 debug("PKG: Error reading file at SUB/LINE");
126 [SQL::Translator: Error reading file at foo/666]
128 The entire message is enclosed within C<[> and C<]> for visual clarity
129 when STDERR is intermixed with STDOUT.
131 =head2 normalize_name
133 C<normalize_name> takes a string and ensures that it is suitable for
134 use as an identifier. This means: ensure that it starts with a letter
135 or underscore, and that the rest of the string consists of only
136 letters, numbers, and underscores. A string that begins with
137 something other than [a-zA-Z] will be prefixer with an underscore, and
138 all other characters in the string will be replaced with underscores.
139 Finally, a trailing underscore will be removed, because that's ugly.
141 normalize_name("Hello, world");
147 A more useful example, from the C<SQL::Translator::Parser::Excel> test
150 normalize_name("silly field (with random characters)");
154 silly_field_with_random_characters