in reply to Re^8: Export and use different package in module
in thread Export and use different package in module

Ohh, I think I see what you are getting at - is it the following?

package Foo; our $var = "Hello"; sub func { print $var, "\n" } package Bar; print $var, "\n"; # Prints "Hello" func(); # Error: Undefined subroutine &Bar::func called

In this example, why is $var still accessible when we've switched to package Bar, while sub func is no longer accessible? Well, the answer is in what our actually does: it "makes a lexical alias to a package (i.e. global) variable of the same name in the current package for use within the current lexical scope." Because in the above example, package Bar is in the same lexical scope as package Foo, anything declared for the current lexical scope in package Foo is still accessible in package Bar. The package variable is $Foo::var, but its alias $var is available in the whole lexical scope.

sub declarations don't work the same way that our does - in the above example, sub func sets up &Foo::func, but that does not carry over into Bar. I understand this distinction may be a bit strange, but it also has historical reasons: our wasn't added to the language until Perl 5.6, about six years after Perl 5.0 - before that, there was the vars pragma. (BTW, note that Lexical Subroutines are no longer experimental in Perl 5.26 and up.)

Since declaring two packages in the same lexical scope causes this "leakage" of lexically-scoped features across package boundaries, and this can cause confusion and sometimes subtle bugs, it's usually recommended to put each package declaration into its own lexical scope, in other words:

{ package Foo; our $var = "Hello"; sub func { print $var, "\n" } } { package Bar; print $Foo::var, "\n"; Foo::func(); } # Or, as of Perl 5.14: package Foo { ... } package Bar { ... }

If you do this, it forces you to reach across package boundaries using more explicit syntax, as I've shown above. Yes, it's more verbose, I understand you don't want to prefix everything with Device::BCM2835::*. The usual mechanism to get symbols (package variables and subs, there is no difference there!) from one package to another is the import/export mechanism I showed previously, i.e. copying symbol table entries. I poked around a bit on CPAN to see if there's a module to hide the somewhat ugly code I showed, and maybe Symbol::Alias could be useful:

package Foo { our $var = "Hello"; sub func { print $var, "\n" } } package Bar { use Symbol::Alias '$Foo::var' => 'var', 'Foo::func' => 'func'; print $var, "\n"; func(); }

Or for your case: use Symbol::Alias map {("Device::BCM2835::$_"=>$_)} qw/ gpio_fsel gpio_set gpio_clr ... /;

Replies are listed 'Best First'.
Re^10: Export and use different package in module
by chenhonkhonk (Acolyte) on Feb 23, 2019 at 13:19 UTC

    Thank you, this is a very informative reply, also for being patient with me. My Perls were version 24 and 22, so I was just a few versions short.

    A side note I didn't think about, can a scalar be substituted in a scalar name? I think it was strict refs that complains if you use a scalar as the name of a hash, which might be why the earlier example you showed me needed it (if following the same rules). If I could just do ${pi} = "biglonglibraryname::subthing::", I wouldn't have to breach scope, and I could get a short name. Thanks again.

      A side note I didn't think about, can a scalar be substituted in a scalar name?

      Yes, the mechanism basically works for any package variables (scalars, arrays, hashes, filehandles, etc.) and subs. It doesn't work for lexical variables, i.e. those declared with my.

      package Big::Long::Library::Name { our $scalar = "Foo"; our @array = qw/ Bar Quz /; our %hash = ( Baz => 42 ); sub function { print "Hello\n" } } package MyPackage { BEGIN { *MyPackage::blah = \&Big::Long::Library::Name::function; package Foo; *MyPackage::myscalar = \$Big::Long::Library::Name::scalar; *MyPackage::array = \@Big::Long::Library::Name::array; *MyPackage::hash = \%Big::Long::Library::Name::hash; } print "$myscalar @array $hash{Baz}\n"; blah; }

      I had to put the operations in a BEGIN block so that Perl knows about the variable names at compile time, and the package Foo; is there because of this detail. As a slightly nicer alternative, you can use Symbol::Alias:

      package Big::Long::Library::Name { our $scalar = "Foo"; our @array = qw/ Bar Quz /; our %hash = ( Baz => 42 ); sub function { print "Hello\n" } } package MyPackage { use Symbol::Alias 'Big::Long::Library::Name::function' => 'blah', '$Big::Long::Library::Name::scalar' => 'myscalar', '@Big::Long::Library::Name::array' => 'array', '%Big::Long::Library::Name::hash' => 'hash'; print "$myscalar @array $hash{Baz}\n"; blah; }