in reply to replacing $` with ordinary capturing in global substitutions

s<(\w+)/><substr( $_, 0, pos $_). "$1\n">ge;
Boris

Replies are listed 'Best First'.
Re^2: replacing $` with ordinary capturing in global substitutions (!pos)
by tye (Sage) on Sep 01, 2004 at 19:57 UTC

    I guess you never tested this. pos() doesn't work inside of a s///:

    #!/usr/bin/perl -w use strict; $_= "This/is/a/test"; s<(\w+)/><$`$1\n>g; print $_,$/,$/; $_= "This/is/a/test"; s<(\w+)/><substr( $_, 0, pos $_). "$1\n">ge; print $_,$/;

    produces:

    This This/is This/is/a test Use of uninitialized value in substr at - line 9. Use of uninitialized value in substr at - line 9. Use of uninitialized value in substr at - line 9. This is a test

    - tye        

      Tested and it works fine for me.
      __OUTPUT__ This This/is This/is/a test This This/is This/is/a test ~ borisz$ perl -V Summary of my perl5 (revision 5 version 8 subversion 4) configuration: Platform: osname=darwin, osvers=7.3.0, archname=darwin-thread-multi-2level
      What perl version do you use?
      Boris

        Thanks. It's nice to know that this has changed. I'd never noticed it being considered a bug, so I wasn't expecting any change here.

        So downgrade the warning to it only not being portable to older versions of Perl. I got lucky and had versions of Perl handy that disagree on this feature; it doesn't work in v5.6.0 but works in v5.6.1 and likely stays (not)working for versions on each side of that division.

        - tye