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

> Can anyone give me some help on this? Maybe it's due to looking at php code
> all day but I would really like to get this done before going to bed. It's
> already 3:40 AM here but oh well... Any help is appreciated!
> 
> --



-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
Selina Mandrake - The Slayer (Buffy parody) - http://shlom.in/selina

Only dead fish go with the flow.
    — via Nadav Har’El

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to