Luis Pachas <[EMAIL PROTECTED]> wrote:

: I keep getting a function undefined, I used require
: and exporter but I kept getting subroutine undefined.
: at times in the main:: and in the perl modules.

    Exporter works fine. Perhaps you could show us what
you did?


: I went around this by having the Hash of symbolic
: references in the main namespace so it works.

    You are not using a hash of symbolic references
in A.pm. You are using a reference to a hash of code
references.


: I just need to know if PERL

    Perl or perl -- never PERL!

: allows symbolic reference variables or hashes with
: symbolic references to be accessed in the Perl modules
: or just in the main.

   It probably does allow that, but very few people here
will help you with symbolic references. They are an
advanced topic and generally considered off-topic on
this list.


: package A;

use strict;
use warnings; # assumes perl 5.6 and above


: my %b;

    You don't use this hash in this module.


: $b = {

    Never us $a or $b as variables. They have a special
meaning in perl and are already in the symbol table.
Use descriptive names!

    $b is not the same as %b.


:     apple => \&foo1,
:     oranges => \&foo2,
:     open => \&foo3
: };

    I think you meant this. We use 'our' because we want
to access this variable from another module. Note the
hash keys are apple, orange, and open.

our %fruit;

%fruit = (
    apple   => \&foo1,
    oranges => \&foo2,
    open    => \&foo3,
);

    I really wouldn't recommend that hash. It forces
you to jump through too many hoops to get things
working. The working example below doesn't use a hash.


: sub foo1 {
:    print "apples\n"
: }
: 
: sub foo2 {
:    print "oranges\n"
: }
: 
: sub foo3 {
:    my ($item) = @_;
:    print $item."\n"
: }
: 
: 1;
: 
: ## End Module
: 


: MAIN :
: 
: !#/bin/perl

use strict;
use warnings; # assumes perl 5.6 and above


: lib "$ENV/";

    I think you meant the following. It assumes there
is something in $ENV. I doubt there is.

use lib "$ENV/";


   Perhaps this is better. It assumes you are placing
your module in a directory named MOD found in the
current directory.

use lib '.';


: use MOD::A;
: 
: $MOD::A::b{foo1}->();
: $MOD::A::b{foo2}->();
: $MOD::A::b{foo3}->("pairs");

    I think you mean 'pears'. It would go better with
the fruit theme.

    $MOD::A::b{foo1} does not exist. A.pm never set %b
equal to anything. Therefore, $b{foo1} was never set to
a value.

    $A::b is equal to a hash reference, but foo1, foo2,
and foo3 are not it's keys. This will probably fill
your needs.

package A;

use strict;
use warnings;

sub foo1 {
   print "apples\n"
}

sub foo2 {
   return "oranges\n"
}

sub foo3 {
    my $fruit = shift;
    print "$fruit\n";
}

1;


#!/usr/bin/perl

use strict;
use warnings;

use lib './MOD';
use A;

A::foo1();
A::foo2();
A::foo3('pears');

__END__


HTH,

Charles K. Clarkson
-- 
Mobile Homes Specialist
254 968-8328





























-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to