FreeKB - Replace values in a variable in Perl using =~
Replace values in a variable in Perl using =~

Home > Search


In this example, the $foo variable contains a value of bar.

my $foo = "bar";

 

The =~ operator is used to replace a value in a variable. In this example, bar is replaced with Hello World. The $foo variable will now contain a value of "Hello World". You can use either forward slash or pipe.

$foo =~ s/bar/Hello World/g;
$foo =~ s|bar|Hello World|g;

 


( and ) characters

The ( and ) characters will need to be escaped. For example, let's say you want to replace (foo) with (bar).

$foo =~ s|\(foo\)|\(bar\)|g;

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments