in reply to s/// operator and binding

Personally, I feel the answer is rather ugly and I would avoid it. You may think it's okay, though.

(my $path = $logfilepath) =~ s/$ref_srv/$server/i;

If $ref_srv has characters which might have a special meaning in a regex and you wish to avoid this:

(my $path = $logfilepath) =~ s/\Q$ref_srv\E/$server/i;

Cheers,
Ovid

New address of my CGI Course.
Silence is Evil (feel free to copy and distribute widely - note copyright text)

Replies are listed 'Best First'.
Re: Re: s/// operator and binding
by ibanix (Hermit) on Jan 28, 2003 at 00:27 UTC
    D'OH! That's so obvious!

    Thanks. Why do you find it ugly? Is there a better way to do it?

    Cheers,
    ibanix

    $ echo '$0 & $0 &' > foo; chmod a+x foo; foo;

      I don't want to answer for Ovid but I especially dislike mixing the declaration with that kind of assignment. I'd prefer

      my $foo; ($foo = $bar) =~ s/baz/qux/;
      but if you are going to separate them anyway, it makes perfect sense to do it in a way that makes all the steps blindingly obvious such as this:
      my $foo = $bar; $bar =~ s/baz/qux/;
      There's rarely a good reason to sacrifice clarity for brevity.

      -sauoq
      "My two cents aren't worth a dime.";
      
      Well it's ugly to me because it's not immediately apparent what you're trying to do. Why not just do this
      my($path) = $logfilepath; $path =~ s/$ref_srv/$server/i;
      Choosing between the two, I'd go with the simpler. Just because you can do something doesn't mean you should ;)

      update (broquaint): changed <pre> to <code>

      For me, this is an aesthetic thing that you can safely ignore. If I want extra parentheses, I'd code in Lisp :)

      If you like them, however, it's not a problem, so don't worry about my blather.

      Cheers,
      Ovid

      New address of my CGI Course.
      Silence is Evil (feel free to copy and distribute widely - note copyright text)