Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Perl replace my string with "1"?

I have the following code:

#$domain = domainname.co.uk
#$root = public_html
#$webpage = domainname.co.uk/foo/bar/foobar.html
my $string = ($webpage =~ s/^$domain//g);
my $linkFromRoot = $dbh->quote($root . $string);

Usualy this works fine but for some reason the output is "public_html 1" instead of "public_html/foo/bar/foobar.html".

Can anyone see why?

like image 600
Phil Jackson Avatar asked Feb 06 '10 09:02

Phil Jackson


1 Answers

You are not getting the correct answer because the substitution returns you 1 which is the number of items substituted. See perlfaq4's answer to How can I count the number of occurrences of a substring within a string?

$domain = "domainname.co.uk";
$root = "public_html";
$webpage = "domainname.co.uk/foo/bar/foobar.html";
my $string = ($webpage =~ s/^$domain//g);
print $string."\n";

Remove the $string and just do $webpage =~ s/^$domain//g; and then do the string concatenation with $webpage.

like image 77
ghostdog74 Avatar answered Oct 02 '22 16:10

ghostdog74