Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double curly braces in perl

I was looking at perl code online and came across something I hadn't seen before and can't find out what it's doing (if anything).

if($var) {{
   ...
}}

Does anyone know what the double curly braces mean?

like image 503
Chris Avatar asked May 31 '12 14:05

Chris


People also ask

What do curly brackets mean in Perl?

Perl uses the "percent" symbol and curly braces with respect to the name of an associative array as a whole, whereas individual elements within an array are referred to as scalars, although the index is still placed in curly braces (unlike the shift in nomenclature used for arrays).

What does double curly braces mean in react?

React uses JSX, In JSX for evaluation of any variable, state object , expression etc has to be enclosed in {}. While giving inline styles in JSX, it has to be specified as an object so it has to be inside curly braces again. {}. This is the reason there are two pairs of curly braces.

What is double curly braces in Python?

The curly braces are part of Django Template Language. The part encapsulated between double curly braces {{ }} is nothing but a variable. That's how DTL, Jinja2 and other template languages work. They have their own set of rules which translates the template in to python and later to HTML code.

What is a curly braces in programming?

In programming, curly braces (the { and } characters) are used in a variety of ways. In C/C++, they are used to signify the start and end of a series of statements. In the following expression, everything between the { and } are executed if the variable mouseDOWNinText is true. See event loop.


2 Answers

There are two statements there. An "if" statement and a bare block. Bare blocks are loops that are executed exactly once.

say "a";
{
   say "b";
}
say "c";

# Outputs a b c

But being loops, they do influence next, last and redo.

my $i = 0;
say "a";
LOOP: {  # Purely descriptive (and thus optional) label.
   ++$i;
   say "b";
   redo if $i == 1;
   say "c";
   last if $i == 2;
   say "d";
}
say "e";

# Outputs a b b c e

(next does the same as last since there is no next element.)

They are usually used to create a lexical scope.

my $file;
{
   local $/;
   open(my $fh, '<', $qfn) or die;
   $file = <$fh>;
}
# At this point,
# - $fh is cleared,
# - $fh is no longer visible,
# - the file handle is closed, and
# - $/ is restored.

It's unclear why one was used here.


Alternatively, it could also be a hash constructor.

sub f {
   ...
   if (@errors) {
      { status => 'error', errors => \@errors }
   } else {
      { status => 'ok' }
   }
}

is short for

sub f {
   ...
   if (@errors) {
      return { status => 'error', errors => \@errors };
   } else {
      return { status => 'ok' };
   }
}

Perl peeks into the braces to guess if it's a bare loop or a hash constructor. Since you didn't provide the contents of the braces, we can't tell.

like image 142
ikegami Avatar answered Oct 16 '22 19:10

ikegami


It's a trick usually employed with do, see chapter Statement Modifiers in perlsyn.

Probably the author wanted to jump out of the block with next or the like.

like image 21
daxim Avatar answered Oct 16 '22 21:10

daxim