Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Importing variables (not functions) from in-file packages

by tobyink (Canon)
on Feb 12, 2014 at 16:00 UTC ( [id://1074642]=note: print w/replies, xml ) Need Help??


in reply to Importing variables (not functions) from in-file packages

The beauty of our is that there is no need to export within the same file. It works lexically.

use strict; use warnings; package MyPackage; # temporarily step into MyPackage our $globalVar; package main; # back to main $globalVar = 42; print $MyPackage::globalVar, "\n";
use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name

Replies are listed 'Best First'.
Re^2: Importing variables (not functions) from in-file packages
by puterboy (Scribe) on Feb 12, 2014 at 18:37 UTC
    Ahhh interesting. I didn't know that one could step in & out of a package multiple times. Thanks

      Yup - and you don't even need to do so at the "top level". package statements can appear within blocks. Like...

      package main; use strict; use warnings; { package Person; use Data::Dumper (); # load, but import nothing! sub new { my $class = shift; bless {@_}, $class; } sub dump { my $self = shift; package Data::Dumper; # The following line is executed in Data::Dumper, # so it can see the "Dumper" sub defined there! print Dumper($self); } # We're back to the Person package here because # the closing brace ended the Data::Dumper package printf "Line %d is in package %s\n", __LINE__, __PACKAGE__; } # And now we're back to the main package. printf "Line %d is in package %s\n", __LINE__, __PACKAGE__; my $bob = Person->new(name => 'Robert'); $bob->dump;
      use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1074642]
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: (7)
As of 2024-04-19 09:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found