Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

substituting constants within regex?

by Anonymous Monk
on Jul 09, 2007 at 07:41 UTC ( [id://625575]=perlquestion: print w/replies, xml ) Need Help??

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

I'm having trouble figuring out the syntax for substiuting the value of constants within a regular expression. Given that Perl constants are actually implemented as subroutines, the more general question is how to evaluate subroutines within regular expressions. ie.
#!/usr/bin/perl -w use strict; use warnings; use constant NEWLINE => '^M'; while (<>) { s/\Q$NEWLINE$//; # incorrect syntax here print; }
Any insight on the syntax would be appreciated.

Replies are listed 'Best First'.
Re: substituting constants within regex?
by Zaxo (Archbishop) on Jul 09, 2007 at 07:55 UTC

    When you use constant Foo . . . , 'Foo' becomes the name of a sub. Inside a regex, you need to use a form which makes the sub be called by evaluation. The \Q is not helping, either.

    while (<>) { s/${\NEWLINE()}$//; # should work print; }
    It looks like you're dealing with foreign line ends, so you might do better to set $/ to CRLF or whatever and chomp.

    In a substitute string, the /e modifier would be needed to call the sub.

    After Compline,
    Zaxo

      Thanks to both of you for your responses!

      > It looks like you're dealing with foreign line ends...

      I'm dealing with cleaning up the output of 'script' which sometimes has a ^M at EOL & '^M' in other cases. Changing $/ would be great if everything was consistent, but I see examples of both the control code & ASCII representations existing in the output.

      FWIW.

Re: substituting constants within regex?
by Ovid (Cardinal) on Jul 09, 2007 at 08:31 UTC

    I recommend the Readonly module:

    #!/usr/bin/perl -w use strict; use warnings; use Readonly; Readonly my $NEWLINE => '^M'; while (<>) { s/\Q$NEWLINE$//; print; }

    Cheers,
    Ovid

    New address of my CGI Course.

Re: substituting constants within regex?
by ForgotPasswordAgain (Priest) on Jul 09, 2007 at 07:55 UTC

    The usual (ugly) trick for interpolating subs inside double quotes is:

    "@{ [ NEWLINE ] }"

    That is, dereference an array ref containing your sub call. It works because strings recognize @.

Re: substituting constants within regex?
by halley (Prior) on Jul 09, 2007 at 13:46 UTC
    Nobody's mentioned that '^M' isn't the way you express a newline. It looks like you're trying to remove all those ^Ms you see in your files. Those are not really "caret character" plus "letter m character" (which is what you describe), but a special character called a carriage return that just appear that way in editors.

    People who see this problem are likely using some form of Unix. You likely have a program called dos2unix on your system. Use that on your files.

    Barring that, here's a perl one-liner from the Unix prompt that will do the same thing.

    % perl -pi~ -e 's/\r//' *.txt

    If you really need to do this in your own code, the bit between the single quotes is useful to you.

    Update: From above replies, it looks like you do know the difference between '^M' and '\r', but I hope the information is still useful for yourself or others.

    --
    [ e d @ h a l l e y . c c ]

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://625575]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-03-28 14:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found