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

How are escape/special characters defined, and how can I redefine them? (\n \a \b \w \W ... ) Example, if i wanted to change "\n" from break to "<BR>" and is it possible to define something like this? \{string} that I could use in doublequotes? If anyone knows about this perl internal or has any insight please let me know. It may seem strange that I want to redefine "\n" instead of just replacing it with $break or "
" but this is just an example.

Replies are listed 'Best First'.
Re: Special Characters
by dragonchild (Archbishop) on Jul 01, 2004 at 14:49 UTC
    Explain what you're trying to do. I think you're looking for Perl's special variables and not the escape sequences. For example, if you change $\, you change how Perl defines the end of a line when reading a file.

    As for changing character classes ... I would strongly advise against that. Instead, I would define your own character classes as so:

    my $class = qr/[a-yA-Y]/; # Don't want to match z or Z if ($string =~ /^${class}+$/) { print "'$string' doesn't have a z in it!\n"; }

    Similar arguments can be made for the other regex escape sequences. Remember - if you redefine it for the entire of Perl, you have redefined it for all the modules you use, such as DBI, CGI, etc. I don't think that's what you really want to do.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

    I shouldn't have to say this, but any code, unless otherwise stated, is untested

Re: Special Characters
by jeffa (Bishop) on Jul 01, 2004 at 15:11 UTC

    Yes, you can change Perl's built in variables such as $/ and $\ to suit your needs, but something tells me you would be better off solving your problem with a CPAN module instead - a solution that has been tried, tested, and proven to be to robust enough to take care of the problems you haven't addressed yet. For example, HTML::FromText does a pretty darned good job of converting plain text into HTML for you. But adding line break tags is really trivial:

    $str =~ s/\r?\n?/<br\/>\n/g; $_ =~ s/\r?\n?$/<br\/>\n/ for @array;

    Question ... did you mean a literal &lt;BR> or a <BR> tag? Either case, you really should be keeping up date with HTML standards ... lower case and close off non-nested tags: <br/>.

    Please do heed dragonchild's advice and give us more information on what you are trying to do. For example, posting a good snippet of the file you are parsing would be a good start.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: Special Characters
by TrekNoid (Pilgrim) on Jul 01, 2004 at 14:57 UTC
    Here's some code I use to replace carriage returns with the HTML break tag. I use this method to replace all sorts of special characters, due to it's being easy to work with. You should be able to put whatever you want in the string variables.

    I'm sure there's a more elegant way to do it, though :)

    my $p1 = chr(10); my $q1 = "<BR>"; $line =~ s/$p1/$q1/g;
    Trek