in reply to Pragma (more) like Java's 'import'?

It would just be a matter of importing the end part of the package name into main's namespace e.g
package Foo::Bar::Baz; sub import { $main::{"Baz::"} = $Foo::Bar::{"Baz::"}; }
However, I don't believe Exporter has this sort of functionality (or any other module for that matter), so you'd have to implement it yourself.
HTH

_________
broquaint

Replies are listed 'Best First'.
Re: Re: Pragma (more) like Java's 'import'?
by jryan (Vicar) on May 11, 2003 at 23:19 UTC

    Make him do it himself? No way, not at Perlmonks! (-:

    package Exporter::End; sub import { my @packages = @_; shift @packages; @packages = (caller)[0] unless defined @packages; foreach my $orig (@packages) { my ($end) = $orig =~ /(\w+)$/; *{"${orig}::import"} = sub { my $pkg = (caller)[0]; *{"${pkg}::${end}::"} = *{"${orig}::"}; } } } 1;

    And then use it like this:

    package Foo::Bar::Baz; use Exporter::End; sub x {print 1}; # ... package main; use Foo::Bar::Baz; Baz::x(); # same as main::Baz::x();

    Or like this:

    package Foo::Bar::Baz; sub x {print 1}; # ... package main; use Exporter::End qw(Foo::Bar::Baz); Baz::x(); # same as main::Baz::x();
Re: Re: Pragma (more) like Java's 'import'?
by theorbtwo (Prior) on May 11, 2003 at 22:28 UTC

    ++, but I'm fairly certian that you want %s there, or even better *s. OTOH, using a * would kill $main::Baz as well as making an alias for the package. (* is better then % because it aliases, rather then coppies, the symbol tables.)


    Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).

      but I'm fairly certian that you want %s there, or even better *s
      Assigning with %Foo::Bar::Baz:: will copy the various globs in the symbol table, which should suffice, copying the glob is more efficient and explicitly accessing the glob would do the same job e.g
      ## copy every glob %main::Baz:: = %Foo::Bar::Baz::; ## copy the Baz:: glob $main::{"Baz::"} = $Foo::Bar::{"Baz::"}; ## explicitly reference the glob *main::Baz:: = *Foo::Bar::Baz::
      They all achieve the same goal (roughly), whereas the first example does a complete copy of the symbol table, the second two just create a reference. So one of the second two would be the desired behaviour in this case.
      HTH

      _________
      broquaint

        D'oh!