Re: Exporter query On 6/6/07, jeevs  wrote:
> #!c:/perl/bin/perl.exe
> #Calculator.pm
>
> package calc;
>
> use strict;
> use warnings;
> use Exporter;
>
> our @ISA = qw(Exporter);
> our @EXPORT = qw(add);
>
> sub add
> {
>         my ($num1,$num2) = @_;
>     $num1+$num2;
> }
>
> 1;
>
> >>>>>>>>>>>>>>>>>>>
>
> #!c:/perl/bin/perl.exe
> #adcalculator.pl
>
> use strict;
> use warnings;
> use Calculator;
>
> my $addn = add(1,9);
> print $addn;
>
> >>>>>>>>>>>>>>
>
> it says
> undefined subroutine &main::add called at adcalculator,pl line 8.
>
>
> Where am I going wrong???

Your sin was these two lines:

> #Calculator.pm
> package calc;

You stuffed the package calc into a file named "Calculator.pm". When you say

    use Calculator;

you do

    BEGIN {
         require Calculator; # which loads Calculator.pm
         Calculator->import; # if the Calculator package has an import sub
    }

The &add sub is exported by the calc package, which inherited the
import from Exporter to get these exporting abilities. But this import
was never called.

      require Calculator;
      calc->import;

would work. But what you probably want is to use

      package Calculator; # in your Calculator.pm file

Cheers,
Adriano Ferreira

P.S. As a stylistical note, usually you provide exporting capabilities
to a module via Exporter with code like that:

       require Exporter; # no need to use
       our @ISA qw(Exporter);

or

        use base qw(Exporter);

>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>
>
>

-- 
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/


