Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Six uses for curlies?

by glwtta (Hermit)
on Jan 13, 2006 at 04:40 UTC ( [id://522883]=perlquestion: print w/replies, xml ) Need Help??

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

Hello,

I'm reading chromatic's "What Is Perl 6?", and he mentions that in Perl 5 curly braces are overloaded to have six different functions.

I've got the obvious ones: code blocks, hash refs, hash elements/slices, dereferencing, and their various uses in regexen (do these count?) - what else is there?

Replies are listed 'Best First'.
Re: Six uses for curlies?
by davido (Cardinal) on Jan 13, 2006 at 05:15 UTC

    • Code blocks (bare, labled, if(){}, while(){}, do{}, foreach(){}, for(){}, sub{}, map{}, etc.)
    • Anonymous hash constructors
    • Hash subscripting (and hash slice subscripting)
    • Dereferencing
    • Quantification (Regexen)
    • Extended regexp patterns, ie (??{...})

    I'll bet I missed something completely obvious.

    ...not to mention the fact that you can legitimately use {} in quote-like constructs such as:

    • s{}[]
    • tr{}{}
    • q{}, qq{}, qr{}, qx{}, etc.
    • m{}

    Dave

      I just saw that comments at the end of the article also mention variable name disambiguiation (eg "${foo}_bar"), didn't think of that one.

      So if we count the regex uses that's 7, and if we don't it's only 5 (seems there's no reason to count the quoting uses, since pretty much anything can be used there).

        Actually, more than seven if you count the uses within regexen: (?{...}), and (??{...}) are two different types of extended patterns, plus quantification, though you could argue that the curlies function as code blocks within the extended regex patterns, though that's a difficult distinction. Disambiguation!, I knew I missed a good one.


        Dave

Re: Six uses for curlies?
by TimToady (Parson) on Jan 13, 2006 at 17:56 UTC
    Well, the six in question is a rather fuzzy number, but you can count things like \x{2639} and \N{WHITE FROWNING FACE} as a different category, plus curlies are used in formats to hide newlines in argument lists, though arguably that's also a code block. But in that case the primary intent is not to supply a code block, since the code in question is merely expected to return a list. Also, if I recall correctly, at one point you could replace the = and . surrounding a format with curlies, but that seems to be broken currently. The internals of toke.c still pretend that they are curlies though.
Re: Six uses for curlies?
by ikegami (Patriarch) on Jan 13, 2006 at 07:17 UTC

    Another special case of subscripting is glob subscripting. For example, *glob{SCALAR}.

    Another special case of disambiguation is in the name of certain special variables, such as ${^TAINT} and ${^ENCODING}.

    Thankfully, there's no ${ or $}.

      Oh yes there is so a $} variable. It's even supported in PPI :)
      adam@red:~/cpan2/trunk/Process$ perl -de 1 Loading DB routines from perl5db.pl version 1.25 Editor support available. Enter h or `h h' for help, or `man perldebug' for more help. main::(-e:1): 1 DB<1> $} = "Hello World!\n"; DB<2> print $} Hello World! DB<3>
      There is a ${, it's just that you have to (use disambiguation!) access it as ${{}. Much like $}, Perl doesn't use it internally for anything, but it is a punctuation global variable so use strict 'vars'; doesn't trigger on it.
        This is an excellent thread for folks new to Perl like me. Perhaps moving this thread to the Tutorials section would be appropriate.
        I have been wondering about significant examples of operator overloading within Perl and this is a good one.
        Perhaps a reason for the need for operator overloading is due to the finite number of symbols on a standard keyboard?
        To me having an operator with six or seven different meanings could lend to confusion, but perhaps the burden of concocting different operators for each of the seven meanings is too great.
        I haven't finished reading Wall et al's "Programming Perl" so I don't know what the creator's philosophy is in this regard.

        Edited by planetscape - removed small tags

Re: Six uses for curlies?
by jZed (Prior) on Jan 13, 2006 at 05:32 UTC
    I'll take the easy one: literal curly braces :-).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-24 04:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found