On Wed, Dec 11, 2002 at 11:33:13AM -0500, Selector, Lev Y wrote:
> Hello,
> 
> Here is a regex question
> 
> I am using the following construct to ident embeded SQL:
> 
>    ($sql =<<EOF) =~ s/^\s+SQL: ?//gm;
>       SQL: select row_id
>       SQL:   from gs_employee_queue g_q
>       SQL:  where g_q.YYYYMMDDHHMM = '$YYYYMMDDhhmm'
>       SQL:    and g_q.action = 'D'
>       SQL:    and g_q.status = 'Unprocessed'
> EOF
> 
> The benefit is that the SQL is idented as the Perl code around it (the
> program is easier to read) - and at the same time unnecessary idents are
> removed before sending the SQL to the database.

I'll bite. What's the benefit of removing the indents before sending
it to the database? Surely the additional scanning done in the
database is done faster than the regex machine in Perl takes?

> The drawback is that every time when I need to extract the SQL (to run it
> manually in the DB) - I have to remove all the  ' SQL:' tags manually. 
> 
> So naturally I want to live without the 'SQL:' label.
> But then if I use just \s+ in the regex - it will remove different number of
> spaces from different lines - thus ruining the SQL layout.

I'll bite again. So what? You're database engine is automated, isn't it?
Or do you have a human fetching items from a closet?

> Question:
>   How to construct the regex so that it subtracts the same amount of white
> space from all the line. Namely, it should memorize the space from the first
> line only - and then subtract it from all the lines.
> 
> I know how to do this with 2 regexes. But is there a way to do it in the
> same one substitute (similar to how it is done above)?


    (my $sql = << '--') =~ s/\A(\s+)(?{$::c = $^N})|^(??{$::c})//gm;
         select row_id   
           from gs_employee_queue g_q   
          where g_q.YYYYMMDDHHMM = '$YYYYMMDDhhmm'   
            and g_q.action = 'D'   
            and g_q.status = 'Unprocessed'   
    --
 
    print $sql;

    __END__
    select row_id
      from gs_employee_queue g_q
     where g_q.YYYYMMDDHHMM = '$YYYYMMDDhhmm'
       and g_q.action = 'D'
       and g_q.status = 'Unprocessed'


Abigail

Reply via email to