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;
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.
Answer: Yes, we can create a package body without package specification.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With