Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm wanting to strip out the table name from a fully qualified field name in an SQL statement. The following code works:
#!/usr/bin/perl my $sql = 'SELECT table.field0, table.field1 FROM table;'; $sql =~ s/table.//g; print "$sql\n";
Yet table is used in a number of points elsewhere in the code. I would like to move it into a constant,
use constant TABLE_NAME => 'table';
but I'm haven't figured out how to write the regular expression yet.
$stmt =~ s/TABLE_NAME.//g; # doesn't work
Can you guys help? TIA, you're are the coolest!

Replies are listed 'Best First'.
Re: Can I move a substitution string into a constant?
by tcf22 (Priest) on Nov 27, 2003 at 21:24 UTC
    Perl constants are actually functions, so try
    my $tmp = quotemeta(TABLE_NAME . '.'); $stmt =~ s/$tmp//g;

    Update: Fixed code, because I didn't test before I posted.

    - Tom

Re: Can I move a substitution string into a constant?
by Zed_Lopez (Chaplain) on Nov 27, 2003 at 21:31 UTC
    We just did this one here.
Re: Can I move a substitution string into a constant?
by davido (Cardinal) on Nov 27, 2003 at 21:47 UTC
    Constants, just like functions, don't automatically interpolate into strings or regular expressions. This behavior is documented in the POD for the constant module:

    Constants defined using this module cannot be interpolated into strings like variables.

    You can however, use the nifty-ugly obfu trick:

    use constant TABLE_NAME => 'table'; my $string = "The constant TABLE_NAME contains '@{[TABLE_NAME]}'.\n";

    ...or to fit your particular needs:

    $stmt =~ s/@{[TABLE_NAME]}.//g;


    Dave


    "If I had my life to live over again, I'd be a plumber." -- Albert Einstein
Re: Can I move a substitution string into a constant?
by ysth (Canon) on Nov 27, 2003 at 21:35 UTC
    Perl constants are really just (optimized) subs, and to do this directly you interpolate the result of calling the sub into the substitution string:
    $stmt =~ s/${\&TABLE_NAME()}.//g;
    Or use a $tmp var as tcf22 suggests.

    (Update: don't forget to use quotemeta if needed:

    $stmt =~ s/\Q${\&TABLE_NAME()}.\E//g
    and I have a feeling you meant to backslash that .)
Re: Can I move a substitution string into a constant?
by Cody Pendant (Prior) on Nov 28, 2003 at 00:15 UTC
    $stmt =~ s/TABLE_NAME.//g; # doesn't work

    Just to note that the dot might cause major problems there, as it matches "any character".

    If your table name is 'customers' and you have another table called customersummary, then you'll replace the first letter of that too, so if you're changing 'customers.' to 'buyers.', it will also change 'customersummary' to 'buyers.mmary'.



    ($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss') =~y~b-v~a-z~s; print
Re: Can I move a substitution string into a constant?
by CountZero (Bishop) on Nov 27, 2003 at 21:47 UTC
    You could of course put the table name in a variable and promise yourself not to change it. ;)

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law