FreeKB - Perl Replace values in a variable (=~)
Perl - Replace values in a variable (=~)

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 0c614 in the box below so that we can be sure you are a human.




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |