Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Perl, how can I replace only the first character of a string?

Tags:

string

perl

I don't know the slightest bit of Perl and have to fix a bug in a Perl script.

Given a variable $myvar which contains a string, if the first character is a dot, replace it with "foo/bar".

How can I do this?
(Bonus points if you can guess the bug)

like image 665
Cephalopod Avatar asked Oct 06 '10 09:10

Cephalopod


2 Answers

$myvar =~ s+^\.+foo/bar+ ;
like image 64
Benoit Avatar answered Oct 01 '22 00:10

Benoit


You can use substr:

 substr($myvar, 0, 1, "foo/bar") if "." eq substr($myvar, 0, 1);
like image 30
Eugene Yarmash Avatar answered Oct 01 '22 00:10

Eugene Yarmash