[Melbourne-pm] problems passing arguments to s{}{}

Jacinta Richardson jarich at perltraining.com.au
Thu Aug 10 00:10:35 PDT 2006


Geoff Crompton wrote:

> sub replace_string {
>     my ($sourced_string, $homedir_shortcut ) = @_;
> 
>     $sourced_string =~ s{$homedir_shortcut}{$ENV{HOME}};

Should be:

                         s{\Q$homedir_shortcut\E}{$ENV{HOME}};

\Q starts quoting meta characters (such as $ -- end of string) and \E ends
quoting.  Interpolation happens first, so that it ends up being:

    s{\Q$HOME\E}{$ENV{HOME}};

and does the right thing.

>     $sourced_string =~ s{$ENV{HOME}}{$homedir_shortcut};

                         s{\Q$ENV{HOME}\E}{...};

With thanks to Paul for spotting the bug in my test example which mislead me as
well.  :)

	J

-- 
   ("`-''-/").___..--''"`-._          |  Jacinta Richardson         |
    `6_ 6  )   `-.  (     ).`-.__.`)  |  Perl Training Australia    |
    (_Y_.)'  ._   )  `._ `. ``-..-'   |      +61 3 9354 6001        |
  _..`--'_..-_/  /--'_.' ,'           | contact at perltraining.com.au |
 (il),-''  (li),'  ((!.-'             |   www.perltraining.com.au   |


More information about the Melbourne-pm mailing list