Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Package declaration in code block

by InfiniteSilence (Curate)
on Apr 23, 2014 at 16:29 UTC ( [id://1083386]=note: print w/replies, xml ) Need Help??


in reply to Package declaration in code block

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1083386]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-18 03:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found