Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

If I put my OO class, called Foo::Bar in the examples below, in the same file at the end, Perl does not let me reference the globally-scoped package namespace variable $Foo::Bar::data within methods of this class. However, if I use the same package code and require or use it from an external library, it seems the non-subroutine (ie: method/constructor/etc) code in the class is run.

You need to review what strict vars does for you , see Tutorials

perl is permissive, perl allows you to do pretty much anything , unless you tell it to, hey, protect me from these common mistakes

Coping with Scoping and Modern Perl give a good introduction

I've learned that I can work-around this by using a forward-declaration with our(), combined with a BEGIN block to initialize the package globals. This seems to have the desired result, although I'm wondering if this is really the best way to support using an OO package like this from either a package definition in the same file as main:: or a real module file included with require.

There is a right way to inline a module, see zentara package/module tutorial

our is not a "workaround", when you enable strict vars, our is one way to communicate to strict, this is not a typo, this is a variable of mine

To inline a module you need BEGIN{} because use is executed like BEGIN at compile time

See Coping with Scoping

I'm curious if this is an intentional design limitation of Perl, or merely a side-effect of putting my main:: package code before the Foo::Bar package code. I understand that a require() call will execute the non-subroutine code in the package at that point, but this seems to be in contradiction to how subroutines work; I can call sub_foo() before declaring it, but apparently can't do this with in-file packages.

Neither. Everything including perl has a compilation order and execution order -- this is not a design limitation -- you gotta bake the bread before you can eat it, assemble the bicycle before you can ride...

You can't access the value of a variable before it is assigned, without a BEGIN{} our $data runs after   $foobar_o->text(1); is called ... too late

See BeginCheckInitEndUnitcheck.pl

# Package

Why do you need a comment to tell you that "package Foo::Bar;" is a package? Think about it:)


In reply to Re: Behavior of 'our' variables in the package-namespace by Anonymous Monk
in thread Behavior of 'our' variables in the package-namespace by Apero

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2022-05-22 02:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (78 votes). Check out past polls.

    Notices?