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

How can I print a { or } to my output? I have tried chr(123) and \{ with no luck.

Replies are listed 'Best First'.
Re: print curly brackets
by davido (Cardinal) on Jul 12, 2011 at 00:25 UTC

    You may need to show us what you're trying to do. Curly brackets within double-quoted strings have special meaning only when attached to some construct that looks relevant to Perl's "double quote like interpolation", such as "$hash{indexing}" and "@{$dereferencing} (for example). If you're having trouble printing the brackets you may either use single quotes to eliminate interpolation, or make your string not look like one of the special cases that makes Perl think you're interpolating.

    But if you show us your issue we may be able to suggest a solution.

    perlop has a good discussion on quoteing behaviors.


    Dave

Re: print curly brackets
by james2vegas (Chaplain) on Jul 11, 2011 at 23:16 UTC
    print "{ output }" perhaps?
Re: print curly brackets
by ikegami (Patriarch) on Jul 12, 2011 at 02:59 UTC
    huh?
    $ perl -le'print chr(123);' { $ perl -le'print "\{";' { $ perl -le'print "{";' {
Re: print curly brackets
by Anonymous Monk on Jul 12, 2011 at 02:22 UTC
    See perlintro and show your code
    ## no interpolation print '{ }'; print q"{ }"; print q<{}>; print q/{ }/; print q _{}_; print q v{ }v; print q{{}}; print q[{ }]; print q({}); print q!{ }!; __END__ { }{ }{}{ }{}{ }{}{ }{}{ }
Re: print curly brackets
by locked_user sundialsvc4 (Abbot) on Jul 12, 2011 at 12:40 UTC

    One subtlety of Perl is the difference between single and double quotes.

    The statement, print '$foo\n'; would print the string:   $foo\n.   That is to say, six literal characters exactly as shown.

    The statement, print "$foo\n"; would print whatever is the value of the variable $foo, followed by a newline.

    What’s the difference ... the only difference?   The quote-marks.   Perl calls this process, interpolation.

    If you say, print '\{';, notice the single quotes, then the two characters will be output literally:   a backslash followed by a curly-brace.   Whereas, in double quotes, the two-character sequence would be interpreted as a character-escape meaning, “one left curly-brace.”

    Postscript:   So, how do you produce a backslash in a double-quoted string?   By using two backslashes.   “\\” is a character-escape corresponding to “a literal backslash.”

    Fun with Perl:   What about ten literal backslashes in a row?   Here’s one way:   print '\' x 10;   The “x” operator is a handy thing indeed, sometimes.

Re: print curly brackets
by Anonymous Monk on Jul 12, 2011 at 15:41 UTC

    All great responses. Thanks. As Dave suggests, I need to be more specific about what I'm really asking for. Here goes: I am parsing an .xml file, formatting, and outputting to an .rtf file. Some of the input strings contain curly brackets (a debug print to the screen verifies that these brackets are in the input string) but they simply don't show up in my output rtf file. So I've tried several substitutions. All 4 of these options cause Word to believe that the output rtf file is corrupted and won't allow me to even open it:

    $formatline =~ s/\{/\{/g;

    or

    my $left_bracket = chr(123); $formatline =~ s/\{/$left_bracket/g;

    or

    my $left_bracket = "{"; $formatline =~ s/\{/$left_bracket/g;

    or

    my $left_bracket = '{'; $formatline =~ s/\{/$left_bracket/g;

    The only success I've had is changing the { to a ( as follows:

    $formatline =~ s/\{/(/g;

      but they simply don't show up in my output rtf file.

      Great start, but your code doesn't demonstrate that -- it doesn't generate a rtf file which doesn't show {}

      All your examples s/\{/{/g; will replace braces all day and all night :) but they won't necessarily make them appear visible in a rtf document :)

      It is basic escaping problem -- { and } are special in RTF, so use one of RTF::Parser / RTF::Lexer / RTF::Writer

      $formatline =~ s/\{/\{/g; doesn't do anything. It replaces «{» with «{». Perhaps you want to replace «{» with «\{»?

      $formatline =~ s/\{/\\{/g;

      I'm just guessing. I don't know RTF, and you didn't specify what you wanted to do.

Re: print curly brackets
by Anonymous Monk on Jul 12, 2011 at 16:40 UTC

    Oh! It's an rtf issue rather than a perl issue. Thanks for pointing me in the right direction.