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

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

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

    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.

      hmmm whats for are you modified my code?

      executed part of regexp works as function and it return value which will used by regexp to make replace, i.e. result of last executed command will be a string to replace:

      example
      # perl -e "\$_='qwerty';\$v='_MATCH_'; s/r/print \$v/; print \"\n\".\$ +_;"; qweprint _MATCH_ty # perl -e "\$_='qwerty';\$v='_MATCH_'; s/r/print \$v/e; print \"\n\".\ +$_;"; _MATCH_ qwe1ty # perl -e "\$_='qwerty';\$v='_MATCH_'; s/r/print \$v;\$v/e; print \"\n +\".\$_;"; _MATCH_ qwe_MATCH_ty
      1(one) is result of print
      perldoc -f print print FILEHANDLE LIST print LIST print Prints a string or a list of strings. Returns true if successful. ...
      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()?

      No, I mean that: was 'qwerty' became 'qwe1ty' because 'print pos()' return '1'.