RE: Error Variable in Package

2003-07-28 Thread Dan Muey
> 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;
>

Cool, thanks for the info. I'll experiment with that.

Dan

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



RE: Error Variable in Package

2003-07-28 Thread Bob Showalter
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]



Re: Error Variable in Package

2003-07-28 Thread Jeff 'japhy' Pinyan
On Jul 28, Dan Muey said:

>What I'm trying to figure out is the best way to have a function return 0
>on failure and set the Error Variable for me to use.

>   package MyGoodies;
>   ...
>   my $MyGoodies::Error; # declare the variable in the package and Export it and 
> function().


Remove my().  A package variable cannot be a my() variable.

  $MyGoodies::Error = "";

-- 
Jeff "japhy" Pinyan  [EMAIL PROTECTED]  http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
 what does y/// stand for?   why, yansliterate of course.
[  I'm looking for programming work.  If you like my work, let me know.  ]


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



RE: Error Variable in Package

2003-07-28 Thread Dan Muey

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.

#Main.pl

 use Package; # which exports the variable $Package::Error and the function function()

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

#   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;

sub function {
undef $Package::Error; # in case it was given a value earlier in the 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
}
return $r;
}

Is that any clearer?

Thanks

Dan



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



Re: Error Variable in Package

2003-07-28 Thread LI NGOK LAM
Sorry I don't understand your question well, but from overall,
I guess that's all about what you want...

###
# Main.pl
use MyGoodies;
my $fedback = $MyGoodies::Error();

###
# MyGoodies.pm
package MyGoodies;
use strict;

sub Error
{check smth and do smth
return 1 if (everything goes fine)
}
1; # Don't miss it, or your package won't run.
###

But that's quite confuse you return 1 while everything
alright, but, your sub name is Error. So the 1 means
OK or Error ?

HTH

- Original Message - 
From: "Dan Muey" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Tuesday, July 29, 2003 12:58 AM
Subject: Error Variable in Package


Howdy List!

Quick question about Packages and an Error Variable.

I have a little package I made and can do;

 use MyGoodies;

 and can export the $MyGoodies::Error from it as well as a function.

What I'm trying to figure out is the best way to have a function return 0 on
failure and set the Error Variable for me to use.

Is this the best way to do that:
package MyGoodies;
...
my $MyGoodies::Error; # declare the variable in the package and Export it
and function().
...
sub function {
undef $MyGoodies::Error; # incase it was given a value before, right?
my $r;
...
...
if(everythign worked) { $r = 1; }
elsif(it failed miserably) { $MyGoodies::Error = "It failed Miserably you
loser - $@"; }

return $r;
}



In the script:

use MyGoodies;

if(!function()) { print "The Sky is falling - $MyGoodies::Error"; }
else { print "It seems to have worked ok in spite of your ignorance"; }

Is all of that the way that should work or am I missing something?

TIA

Dan

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




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



I hope I'm viewing this correwctIRE: Error Variable in Package

2003-07-28 Thread Dan Muey

> Howdy List!
> 
> Quick question about Packages and an Error Variable.
> 
> I have a little package I made and can do;
> 
>  use MyGoodies;
> 
>  and can export the $MyGoodies::Error from it as well as a function.
> 
> What I'm trying to figure out is the best way to have a 
> function return 0 on failure and set the Error Variable for me to use.
> 
> Is this the best way to do that:
>   package MyGoodies;
>   ...
>   my $MyGoodies::Error; # declare the variable in the 
> package and Export it and function().
>   ...
>   sub function {
>   undef $MyGoodies::Error; # incase it was given 
> a value before, right?
>   my $r;
>   ...
>   ...
>   if(everythign worked) { $r = 1; }
>   elsif(it failed miserably) { $MyGoodies::Error 
> = "It failed Miserably you loser - $@"; }
> 
>   return $r;
>   }
> 
>  
> 
> In the script:
> 
> use MyGoodies;
> 
>   if(!function()) { print "The Sky is falling - 
> $MyGoodies::Error"; }
>   else { print "It seems to have worked ok in spite of 
> your ignorance"; }

Or also I'd like to be able to do something like this afetr I run function() :

if($MyGoodies::Error) { print "The Sky is falling - $MyGoodies::Error"; }
else { print "It seems to have worked ok in spite of  your ignorance"; }

I hope I'm doing/thinking of this correctly...


> 
> Is all of that the way that should work or am I missing something?
> 
> TIA
> 
> Dan
> 
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 

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



Error Variable in Package

2003-07-28 Thread Dan Muey
Howdy List!

Quick question about Packages and an Error Variable.

I have a little package I made and can do;

 use MyGoodies;

 and can export the $MyGoodies::Error from it as well as a function.

What I'm trying to figure out is the best way to have a function return 0 on failure 
and set the Error Variable for me to use.

Is this the best way to do that:
package MyGoodies;
...
my $MyGoodies::Error; # declare the variable in the package and Export it and 
function().
...
sub function {
undef $MyGoodies::Error; # incase it was given a value before, right?
my $r;
...
...
if(everythign worked) { $r = 1; }
elsif(it failed miserably) { $MyGoodies::Error = "It failed Miserably 
you loser - $@"; }

return $r;
}

 

In the script:

use MyGoodies;

if(!function()) { print "The Sky is falling - $MyGoodies::Error"; }
else { print "It seems to have worked ok in spite of your ignorance"; }

Is all of that the way that should work or am I missing something?

TIA

Dan

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