Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any difference between "standard" and block package declaration?

Tags:

perl

Usually, a package starts simply as

package Cat;
... #content
!0;

I just discovered that starting from the perl 5.14 there is the "block" syntax too.

package Cat {
    ... #content
}

It is probably the same. But just to be sure, is there any difference?

And about the 1; at the end of the package file. The return value of any block, is taken as the value of the last evaluated expression. So can I put the 1; before the closing }? To make require happy, is there any difference between:

package Cat {
    ... #content
    1; 
}

and

package Cat {
    ... #content 
}
1;
like image 514
Nemo Avatar asked Jul 05 '13 08:07

Nemo


People also ask

What is the difference between package specification and package body?

The specification is the interface to the package. It declares the types, variables, constants, exceptions, cursors, and subprograms that can be referenced from outside the package. The body defines the queries for the cursors and the code for the subprograms.

Can we create a package body without specification?

Answer: Yes, we can create a package body without package specification.

What can you do with the Dbms_lob package?

The DBMS_LOB package provides subprograms to operate on BLOBs, CLOBs, and NCLOBs. You can use DBMS_LOB to access and manipulate specific parts of LOBs or complete LOBs. You can also refer to "Large objects (LOBs)" in Oracle TimesTen In-Memory Database PL/SQL Developer's Guide.

What is the difference between function procedure and package in PL SQL?

Unlike a function, the procedure does not have any specific return type and doesn't return single but multiple values. Package: A package, which is a schema object, is responsible for grouping PL/SQL types, subprograms and items, which are logically related.


2 Answers

Of course there is a difference. The second variant has a block.

A package declaration sets the current namespace for subs and globals. This is scoped normally, i.e. the scope ends with the end of file or eval string, or with an enclosing block.

The package NAME BLOCK syntax is just syntactic sugar for

{ package NAME;
  ...;
}

and even compiles down to the same opcodes.

While the package declaration is syntactically a statement, this isn't semantically true; it just sets compile-time properties. Therefore, the last statement of the last block is the last statement of the file, and there is no difference between

package Foo;
1;

and

package Foo {
  1;
}

wrt. the last statement.

The package BLOCK syntax is interesting mainly because it looks like class Foo {} in other languages, I think. Because the block limits scope, this also makes using properly scoped variables easier. Think:

package Foo;
our $x = 1;
package main;
$::x = 42;
say $x;

Output: 1, because our is lexically scoped like my and just declares an alias! This can be prevented by the block syntax:

package Foo {
  our $x = 1;
}
package main {
  $::x = 42;
  say $x;
}

works as expected (42), although strict isn't happy.

like image 65
amon Avatar answered Oct 14 '22 17:10

amon


package Foo { ... }

is equivalent to

{ package Foo; ... }

which is different from the following in that it create a block

package Foo; ...

This only matters if you have code that follows in the file.


package Foo { ... }

isn't equivalent to

BEGIN { package Foo; ... }

I would have liked this, but that proposal was not accepted.


require (and do) requires that the last expression evaluated in the file returns a true value. { ... } evaluates to the last value evaluated within, so the following is fine:

package Foo { ...; 1 }

Placing the 1; on the outside makes more sense to me — it pertains to the file rather than the package — but that's merely a style choice.

like image 28
ikegami Avatar answered Oct 14 '22 17:10

ikegami