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

Hi all,

more and more I see code examples of the following form:

{ package Modul; use This; use That; ... }

What is the difference to the form of

package Modul1; use This; use That;

without being wrapped into a block?

Can someone enlight me, please?

Best regards
McA

Replies are listed 'Best First'.
Re: Package declaration in code block
by LanX (Saint) on Apr 23, 2014 at 16:14 UTC
    Scope! The namespace is restricted to the block, you don't need to declare the package at the end to return to that older namespace again.

    edit
    > perl $x=42; { package Inner; $x=666; } print $x; __END__ 42

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      Another way to illustrate it would be:

      use 5.010; { package Foo; say __PACKAGE__; } say __PACKAGE__;

      If your Perl is recent enough, you can even do this:

      use 5.014; package Foo { say __PACKAGE__; } say __PACKAGE__;
      use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name

      Hi Rolf,

      thank you for the fast reply.

      Is there an advantage in a classic package, one package per file?

      Regards
      McA

        If you're posting example code, it's useful for everything to be in one self-contained example file, so that people can just copy and paste it into their editor and run it, rather than them having to create a directory called Foo and make a file in there called Bar.pm and paste some code in there, and then create another folder called Boo and make a file in there called Far.pm and paste more code in there, and then...

        use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name
        I use this form in my .t files when I need a quick class mock-up: it stands out more.
Re: Package declaration in code block
by InfiniteSilence (Curate) on Apr 23, 2014 at 16:29 UTC

    The package declarations themselves are not too exciting since, as you can see below, the package can be referenced in other packages. However, notice for a variable declared with our it doesn't matter whether you put the package-in-the-block before or after the normal one, the use of the unqualified variable will default to the one outside of the block.

    #!/usr/bin/perl -w use strict; { package niceThing; our $compat = 4234; sub howdy { return qq|Howdy in package niceThing!|; } } package nonNiceThing; our $compat = 4555; sub howdy { return qq|Howdy in package nonNiceThing!|; } package main; print niceThing::howdy(); print nonNiceThing::howdy(); print "\n$compat\n"; print "\n$niceThing::compat\n";
    Result:
    Howdy in package niceThing!Howdy in package nonNiceThing! 4555 4234

    This works even if you take the package name out of the enclosed block so my suspicion is that in the code you are looking at the package declaration was put into the block for convenience and readability.

    Celebrate Intellectual Diversity