symbolic references in perl module help

2004-07-16 Thread Luis Pachas
Hi I have a problem,
I have a PM
i have this
A.pm :
package A;
my %b;
$b = {
   apple = \foo1,
   oranges = \foo2,
   open = \foo3
};
sub foo1 {
  print apples\n
}
sub foo2 {
  print oranges\n
}
sub foo3 {
  my ($item) = @_;
  print $item.\n
}
1;
## End Module
MAIN :
!#/bin/perl
lib $ENV/;
use MOD::A;
$MOD::A::b{foo1}-();
$MOD::A::b{foo2}-();
$MOD::A::b{foo3}-(pairs);
exit();
Thats it, I keep getting a function undefined, I used require and exporter 
but I kept getting subruotine undefined...at times in the main:: and in the 
perl modules... I went around this by having the Hash of symbolic references 
in the main namespace so it works...I just need to know if PERL allows 
symbolic reference variables or hashs with symbolic references to be access 
in the Perl modules or just in the main.

THank you in advance

PERL RULEZ
_
Check out the latest news, polls and tools in the MSN 2004 Election Guide! 
http://special.msn.com/msn/election2004.armx

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



Re: symbolic references in perl module help

2004-07-16 Thread Gunnar Hjalmarsson
Luis Pachas wrote:
Hi I have a problem,
I noticed several problems.
I have a PM
i have this
A.pm :
package A;
my %b;
$b = {
Must be global. And you need to decide if you want a hash or a hash 
reference. I'm assuming a hash reference.

our $b = {
   apple = \foo1,
   oranges = \foo2,
   open = \foo3
};
sub foo1 {
  print apples\n
}
sub foo2 {
  print oranges\n
}
sub foo3 {
  my ($item) = @_;
  print $item.\n
}
1;
## End Module
MAIN :
!#/bin/perl
lib $ENV/;
use MOD::A;
$MOD::A::b{foo1}-();
$MOD::A::b{foo2}-();
$MOD::A::b{foo3}-(pairs);
Still assuming hash references, do you mean:
$MOD::A::b-{apple}();
$MOD::A::b-{oranges}();
$MOD::A::b-{open}(pairs);
I went around this by having the Hash 
of symbolic references in the main namespace so it works...
Which hash of symbolic references? They are hard references, not symbolic.
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response



Re: symbolic references in perl module help

2004-07-16 Thread James Edward Gray II
On Jul 16, 2004, at 7:54 AM, Luis Pachas wrote:
Hi I have a problem,
I have a PM
i have this
A.pm :
package A;
my %b;
$b = {
   apple = \foo1,
   oranges = \foo2,
   open = \foo3
};
sub foo1 {
  print apples\n
}
sub foo2 {
  print oranges\n
}
sub foo3 {
  my ($item) = @_;
  print $item.\n
}
1;
## End Module
MAIN :
!#/bin/perl
lib $ENV/;
use MOD::A;
$MOD::A::b{foo1}-();
$MOD::A::b{foo2}-();
$MOD::A::b{foo3}-(pairs);
exit();
Thats it, I keep getting a function undefined, I used require and 
exporter but I kept getting subruotine undefined...at times in the 
main:: and in the perl modules... I went around this by having the 
Hash of symbolic references in the main namespace so it works...I just 
need to know if PERL allows symbolic reference variables or hashs with 
symbolic references to be access in the Perl modules or just in the 
main.
You're using a boat load of tricks to bypass the real problem.  Why 
don't you show us the code that is producing the function undefined 
errors and let us fix that for you.

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



RE: symbolic references in perl module help

2004-07-16 Thread Charles K. Clarkson
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