http://www.perlmonks.org?node_id=947200

mmartin has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I was trying to use the "/r" flag in a REGEX Search and Replace and I keep getting the error:
"Bareword found where operator expected at ./test.pl line 11, near "s/Matt/Matthew/r" "

For Example:
#!/usr/bin/perl use warnings; use strict; my $str; my $str2; $str = "Hello World I'm Matt"; $str2 = $str =~ s/Matt/Matthew/r; print " str = $str\n"; print "str2 = $str2\n";
The server it's running on has Perl Version 5.8.8.
Was the Non-Destructive Flag "/r" not implemented in this version of Perl? If so, is it possible to get it without upgrading all of Perl..?



Thanks in Advance,
Matt

Replies are listed 'Best First'.
Re: REGEX Non-Destructive Flag
by toolic (Bishop) on Jan 10, 2012 at 17:42 UTC
    It is not implemented in 5.8.8. It was added in 5.14: see perl514delta. I don't think you can get it without upgrading all of perl.
      Hey toolic, thanks for the reply.

      Ok I was afraid of that... Thanks

      Is there an easier way (less lines) that I could use with my version like my previous example (i.e. $str = $str2 =~ s/pattern/replace/r; ).

      The only way I can think to do this without changing the original string is:
      my $str1 = "How-Are-You-Doing" my $str2 = $str1 $str2 =~ s/-/ /; print $str2; ____OUTPUT____ Hello How Are You Doing


      Thanks,
      Matt


         perl -ne '(my $foo = $_) =~ s/-/ /g;'

        The assignment returns an lvalue which then gets the substitution run on it. Basically the same as yours, just in one statement.

        --MidLifeXis

      mm, seems like "/r" is not in perlre, although at start says it is "Perl 5 version 14.1 documentation"

      Is it deliberated ? or it is a simple bug in the doc which should be reported?

      Thanks!

      UPDATE: Thanks to toolic, it is true

      I was confused by perldelta because "/r" was in "Regular Expressions" section there

Re: REGEX Non-Destructive Flag
by mmartin (Monk) on Jan 10, 2012 at 21:08 UTC
    Hey Guys, thanks for the suggestions.

    Is there a way to print out a string, and in the terminal have it show any special characters (i.e. \n, \t, \s, etc...)

    For Example: I have a variable like this:
    #This variable is equal to the routers description which is a single s +tring that when printed prints into 3 separate lines my $descr = split " STRING: " `snmpget -v1 -c public 10.1.1.1 sysDescr +.0` print "$descr"; ____OUTPUT____ Cisco IOS Software, Catalyst 4500 L3 Switch Software (cat4500-ENTSERVI +CESK9-M), Version 12.2(31)SGA9, RELEASE SOFTWARE (fc1) Technical Support: http://www.cisco.com/techsupport Copyright (c) 1986-2009 by Cisco Systems, Inc. Compiled Mon 12-Jan-09 13:53

    So is there a way to print out $sysDescr and have it actually print the special character symbols...? I want to see what meta-characters are actually in there.
    Something like this:
    Cisco IOS Software, Catalyst 4500 L3 Switch Software (cat4500-ENTSERVI +CESK9-M), Version 12.2(31)SGA9, RELEASE SOFTWARE (fc1)\n Technical Support: http://www.cisco.com/techsupport\n Copyright (c) 1986-2009 by Cisco Systems, Inc.\n Compiled Mon 12-Jan-09 13:53\n


    Any thoughts would be great.


    Thanks,
    Matt


      Data::Dump::Streamer can do that:

      use Modern::Perl; use Data::Dump::Streamer; my $x= "cr-\15, lf-\12, tab-\11, bs-\10"; say Dump $x;

      prints

      $VAR1 = "cr-\r, lf-\n, tab-\t, bs-\b";

      or as a one-liner,

      perl -MDDS -wE 'say Dump "cr-\15, lf-\12, tab-\11, bs-\10";'
        Hey quester,

        Thanks for the reply... Yea that did it! Thanks alot.


        Thanks Again,
        Matt