On Sat, Feb 1, 2014 at 4:57 AM, Shlomi Fish <shlo...@shlomifish.org> wrote:

> Hi Omega,
>
> On Sat, 1 Feb 2014 03:40:01 -0500
> Omega -1911 <1911...@gmail.com> wrote:
>
> > Hello List: I am trying to go through a folder of php scripts to add a
> > database prefix to lines that have a select statement. Since the database
> > prefix will differ, I am simply trying to add:
> >
> > ".$database_prefix."
> >
> > to those lines. For example, instead of the line looking like: $sql =
> > "SELECT * FROM bs_services"; I need to have it look like: $sql = "SELECT
> *
> > FROM ".$database_prefix."_bs_services";
> >
> > I have tried a variety of Perl examples including the the code below, but
> > it gives an error.
> >
> > $_ =~ s/bs_/".$database_prefix."_bs/g;
> >
>
> It appears that the right hand side of the s/// operation will try to
> interpolate «$database_prefix» as a variable. What you need is to escape
> the
> «$» using «\$»:
>
> [SHELL]
>
> shlomif@telaviv1:~$ cat Test.pl
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> while(<>)
> {
>     $_ =~ s/bs_/".\$database_prefix."_bs/g;
>     print $_;
> }
> shlomif@telaviv1:~$ perl Test.pl
> $sql = "SELECT * FROM bs_services";
> $sql = "SELECT * FROM ".$database_prefix."_bsservices";
> shlomif@telaviv1:~$
> [/END OF SHELL]
>
> A few notes:
>
> 1. It's not a good idea to overuse $_:
> http://perl-begin.org/tutorials/bad-elements/#overuse_dollar_underscore
>
> 2. «$_ =~ s///;» can simply be written as «s///» with the implicit $_
> variable.
>
> Regards,
>
>         Shlomi Fish
>
> --
Good advice Shlomi. Thank you again. I should have caught that with fresh
eyes!

Reply via email to