in reply to Re^7: Using pos() inside regexp (no /e)
in thread Using pos() inside regexp

Yes! This is exactly the thing i talk about, pos() for next iteration of regexp is not changes. And looks like there is no way to change it, and this is realy meaningless because variable regexp save initial value of variable and use this value for search and another for replace results.
$ perl -MDevel::Peek -e "\$_='qwerty';Dump(\$_);s/[rt]/Dump(\$_);/ge; +print Dump(\$_)" SV = PV(0x801838) at 0x82fde0 REFCNT = 1 FLAGS = (POK,pPOK) PV = 0x201fe0 "qwerty"\0 CUR = 6 LEN = 8 SV = PVMG(0x823514) at 0x82fde0 REFCNT = 1 FLAGS = (SMG,POK,pPOK) IV = 0 NV = 0 PV = 0x201fe0 "qwerty"\0 CUR = 6 LEN = 8 MAGIC = 0x2018d0 MG_VIRTUAL = &PL_vtbl_mglob MG_TYPE = PERL_MAGIC_regex_global(g) MG_LEN = 3 SV = PVMG(0x823514) at 0x82fde0 REFCNT = 1 FLAGS = (SMG,POK,pPOK) IV = 0 NV = 0 PV = 0x201fe0 "qwerty"\0 CUR = 6 LEN = 8 MAGIC = 0x2018d0 MG_VIRTUAL = &PL_vtbl_mglob MG_TYPE = PERL_MAGIC_regex_global(g) MG_LEN = 4 SV = PVMG(0x823514) at 0x82fde0 REFCNT = 1 FLAGS = (SMG,POK,pPOK) IV = 0 NV = 0 PV = 0x2010d0 "qwey"\0 CUR = 4 LEN = 8 MAGIC = 0x2018d0 MG_VIRTUAL = &PL_vtbl_mglob MG_TYPE = PERL_MAGIC_regex_global(g) MG_LEN = -1
So, it seems my task can't be solved by using follow construction
$data =~s/([^\n]{16})/pos($data)=0;ch($1)/ge;
(Note: escaping of $ simbol is nessesary for bash
# perl -e "\$_='qwerty';s/r/print pos();/e;" 3 BUT # perl -e "$_='qwerty';s/r/print pos();/e;" syntax error at -e line 1, near ";=" Execution of -e aborted due to compilation errors. # uname -v Darwin Kernel Version 10.4.0: Fri Apr 23 18:28:53 PDT 2010; root:xnu-1 +504.7.4~1/RELEASE_I386
)

Replies are listed 'Best First'.
Re^9: Using pos() inside regexp (no /e)
by ww (Archbishop) on Oct 10, 2010 at 19:28 UTC

    Take your "BUT" example and extend it:

    perl -e "$_='qwerty'; s/r/print pos(). \"\n\";/e; print $_;

    OUTPUT:

    3 qwe1ty

    Note the "1" (one) in the replaced output. Maybe you can go somewhere with this.

      Again, escape of '$' is necessary for bash shell which used at unix-like system
      perl -e "$_='qwerty'; s/r/print pos(). \"\n\";/e; print $_;" syntax error at -e line 2, near "=" Execution of -e aborted due to compilation errors.
      but then $ is escaped
      perl -e "\$_='qwerty'; s/r/print pos(). \"\n\";/e; print \$_;" 3 qwe1ty
      It seems you try your example under windows, and it is work fine there, but i'm macos user. 1(one) is returned by print function and s/// insert it because it is last return at executed section, this is clear, but it can be easily fix
      perl -e "\$_='qwerty'; s/r/print pos(). \"\n\";\$&/e; print \$_;" 3 qwerty

        Now I'm starting to wonder if your assertions can be taken at face value. However, in fairness, maybe I'm just missing the point(s).

        Nonetheless:

        On my "unix-like" system (Ubuntu 10.4, perl 5.10.1), :

        ~$ perl -e '$_="qwerty"; s/r/print pos(). "\n";/e; print $_;' 3 qwe1ty

        And your latest code (third block in ), modified (a) to actually do something with $*& (i.e. print it); (b) to be more explicit; and (c) to produce readable output under Ubuntu, looks like line 1 below and produces the output in lines 2-4:

        perl -e "\$_='qwerty'; s/r/print pos(). \"\n\";print \$&.\"\n\";/e; pr +int \$_ . \"\n\";" 3 r qwe1ty ~$

        (See also ikegami's Re^4: Using pos() inside regexp (no /e).)

        or, looking at the issue through the mechanism of a script, executed in the Gnome terminal OR bash:

        #!/usr/bin/perl use strict; use warnings; use feature qw(say); # perl -le "\$_='qwerty';s/r/print pos()/e;" my $str ='qwerty'; $str =~ s/[rq]/say pos($str); say $&;/eg; print "\t\$str: $str \n\n"; my $str0 ='qwerty'; $str0 =~ s/(r|q)/say pos($str0); say $&;/eg; print "\t\$str0: $str0 \n\n"; my $str1 ='qwerty'; $str1 =~ s/[rz]/say pos($str0); say $&;/eg; print "\t\$str1: $str1 \n\n"; my $str2 ='qwerty_erk'; $str2 =~ s/r/say pos($str2); say $&;/eg; print "\t\$str2: $str2 \n\n"; my $var='four'; $var =~ s/[fu]/say pos($var); say $&;/eg; print $var . "\n\n";

        I don't think execution of the code above supports your interpretation of the appearance of the number 1 in the output:

        ~/pl_test$ perl braveghost.pl 0 q 3 r $str: 1we1ty 0 q 3 r $str0: 1we1ty Use of uninitialized value in say at braveghost.pl line 17. r $str1: qwe1ty 3 r 8 r $str2: qwe1ty_e1k 0 f 2 u 1o1r ~/pl_test$

        Does your statement that "1(one) is returned by print function and s/// insert it because it is last return at executed section" mean you believe that invoking print in the regex somehow assigns its return value to pos()?

        BTW, re "macos" -- "Mac OS" is the Apple TM style.