Dan Muey wrote:
> Thanks for the reply!
> 
> > Sorry I don't understand your question well, but from
> > overall, I guess that's all about what you want...
> 
> I'll try to make it simpler, I have a tendency to ramble!
> 
> I've seen packages that have a variable like $Package::Error or
> $Package::errstr 
> 
> I want a funtion in that package to return 1 on success or 0 on
> failure but if it is 0 I want to have the reason why it failed so I
> give $Package::Error a value. 

OK, fine.

> 
> #Main.pl
> 
>  use Package; # which exports the variable $Package::Error and the
> function function()

No. You should't export it. "Exporting" means making an "alias" to the
variable in the package that issues the "use".

If you refer to the variable as $Package::Error, you don't need to export
it. If you export it, you would refer to it as simply $Error. But that might
interfere with the main program's use of $Error in some other context.

You can put $Error in the @EXPORT_OK array, which gives the main program the
*option* to import the symbol if the author chooses.

> 
>  if(!function()) { print "It failed and here is why -
>  $Package::Error"; } else { print "It worked oh happy days"; }

Yes, that's fine.

> 
> #     or after executing function()
> 
>  if($Package::Error) { print "It failed and here is why -
>  $Package::Error"; } else { print "It worked oh happy days"; }
> 
> #Package.pm
> 
> package Package;
> ... Export $Package:Error and function()
> my $Package::Error;

No. You can't access "my" variables outside this file. It should be a
global:

   our $Error;

> 
> sub function {
>       undef $Package::Error; # in case it was given a value earlier in the

Since you're in package Package, you don't need to qualify this. You can
just use $Error throughout.

>       script my $r = 1; # unless it fails return 1
>       if(it failed to work) {
>               $r = 0; # it failed so return 0
>               $Package::Error = "IT failed because ...."; #
> set the reason why into the Erro Variable

Same as above.

>       }
>       return $r;
> }

Example:

Foo.pm:

   package Foo;

   use strict;
   use base qw/Exporter/;

   our $Error;
   our @EXPORT_OK = qw/bar $Error/;

   sub bar {
      undef $Error;
      my $aligned = 0;
      $Error = "Frobnitz misaligned", return unless $aligned;
      1;
   }

   1;

main.pl

   #!/usr/bin/perl -w
   use strict;
   use Foo qw/bar/;

   bar() or die $Foo::Error;

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to