adding a pg src file example
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Utils.pm
CommitLineData
1a24938d 1package SQL::Translator::Utils;
2
3# ----------------------------------------------------------------------
93d12e9c 4# $Id: Utils.pm,v 1.2 2003-04-17 13:41:36 dlc Exp $
1a24938d 5# ----------------------------------------------------------------------
6# Copyright (C) 2003 darren chamberlain <darren@cpan.org>
7#
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.
11#
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.
16#
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
20# 02111-1307 USA
21# -------------------------------------------------------------------
22
23use strict;
24use base qw(Exporter);
25use vars qw($VERSION @EXPORT_OK);
26
27use Exporter;
28
29$VERSION = 1.00;
93d12e9c 30@EXPORT_OK = ('debug', 'normalize_name');
1a24938d 31
32# ----------------------------------------------------------------------
33# debug(@msg)
34#
35# Will send debugging messages to STDERR, if the caller's $DEBUG global
36# is set.
37#
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
40# from caller():
41#
42# debug("PKG: Bad things happened on line LINE!");
43#
44# Will be warned as:
45#
46# [SQL::Translator: Bad things happened on line 643]
47#
48# If called from Translator.pm, on line 643.
49# ----------------------------------------------------------------------
50sub debug {
51 my ($pkg, $file, $line, $sub) = caller(1);
52 {
53 no strict qw(refs);
54 return unless ${"$pkg\::DEBUG"};
55 }
56
57 $sub =~ s/^$pkg\:://;
58
59 while (@_) {
60 my $x = shift;
61 chomp $x;
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";
67 }
68}
69
93d12e9c 70
71sub normalize_name {
72 my $name = shift;
73
74 # The name can only begin with a-zA-Z_; if there's anything
75 # else, prefix with _
76 $name =~ s/^([^a-zA-Z_])/_$1/;
77
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;
81
82 # All duplicated _ need to be squashed into one.
83 $name =~ tr/_/_/s;
84
85 # Trim a trailing _
86 $name =~ s/_$//;
87
88 return $name;
89}
90
1a24938d 911;
92
93__END__
94
95=head1 NAME
96
97SQL::Translator::Utils - SQL::Translator Utility functions
98
99=head1 SYNOPSIS
100
101 use SQL::Translator::Utils qw(debug);
102 debug("PKG: Bad things happened");
103
104=head1 DESCSIPTION
105
106C<SQL::Translator::Utils> contains utility functions designed to be
107used from the other modules within the C<SQL::Translator> modules.
108
109No functions are exported by default.
110
111=head1 EXPORTED FUNCTIONS
112
113=head2 debug
114
115C<debug> takes 0 or more messages, which will be sent to STDERR using
116C<warn>. Occurances of the strings I<PKG>, I<SUB>, and I<LINE>
117will be replaced by the calling package, subroutine, and line number,
118respectively, as reported by C<caller(1)>.
119
120For example, from within C<foo> in F<SQL/Translator.pm>, at line 666:
121
122 debug("PKG: Error reading file at SUB/LINE");
123
124Will warn
125
126 [SQL::Translator: Error reading file at foo/666]
127
128The entire message is enclosed within C<[> and C<]> for visual clarity
129when STDERR is intermixed with STDOUT.
93d12e9c 130
131=head2 normalize_name
132
133C<normalize_name> takes a string and ensures that it is suitable for
134use as an identifier. This means: ensure that it starts with a letter
135or underscore, and that the rest of the string consists of only
136letters, numbers, and underscores. A string that begins with
137something other than [a-zA-Z] will be prefixer with an underscore, and
138all other characters in the string will be replaced with underscores.
139Finally, a trailing underscore will be removed, because that's ugly.
140
141 normalize_name("Hello, world");
142
143Produces:
144
145 Hello_world
146
147A more useful example, from the C<SQL::Translator::Parser::Excel> test
148suite:
149
150 normalize_name("silly field (with random characters)");
151
152returns:
153
154 silly_field_with_random_characters
155