CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Post deleted by hwnd

 



recruiter
User

Jul 5, 2013, 6:58 PM

Post #1 of 5 (358 views)
Post deleted by hwnd

 


BillKSmith
Veteran

Jul 5, 2013, 8:15 PM

Post #2 of 5 (354 views)
Re: [hwnd] Constructor Error handling? [In reply to] Can't Post

Your approach is already very good. As you suggested, you can replace the foreach loop with grep.


Code
use strict; 
use warnings;
my $obj0 = foo->dump(file => 'file.txt', type => 1, head => 1);
my $obj1 = foo->dump(file => 'file.txt', type => 1, head => 1, data => 1);

package foo;
use Carp;
sub new { return bless {}, shift }

sub dump {
my $opt = { file => 1, type => 1, head => 1 };
my $class = shift;
my $self = {@_};
croak("Invalid or undefined filename") unless $self->{file};
croak("Invalid constructor call") if (grep !exists($opt->{$_}), keys %$self);
}

# Expected output:
#
# Invalid constructor call at script.pl line 4.

Good Luck,
Bill


recruiter
User

Jul 5, 2013, 8:56 PM

Post #3 of 5 (350 views)
Re: [BillKSmith] Constructor Error handling? [In reply to] Can't Post

Bill, thanks for the quick feedback. That's what I was looking for, I was using grep with the // operator and without 'exists' and it was returning the wrong output.


recruiter
User

Jul 6, 2013, 10:42 AM

Post #4 of 5 (331 views)
Re: [hwnd] Constructor Error handling? [In reply to] Can't Post

I'm curious, does it matter whether I check if there is a valid value in $self->{file} first and then check for valid keys? I would think it would make sense to use grep to see if valid keys are being used and if so, then check if a value of 'file' is in place?

Also is there really a difference between using if and unless here:


Code
croak("Invalid or undefined filename") unless $self->{file};      
croak("Invalid constructor call") if (grep !exists($opt->{$_}), keys %$self);



and using 'or' here?


Code
$self->{file} or croak("Invalid or undefined filename!"); 
grep {exists $opt->{$_}} keys %$self or croak("Invalid constructor call");



BillKSmith
Veteran

Jul 6, 2013, 12:32 PM

Post #5 of 5 (325 views)
Re: [hwnd] Constructor Error handling? [In reply to] Can't Post

  • With the way that you define $opts, exists is not necessary. I forgot to remove it.


  • Checking for the file key first correctly handles the special case of no keys at all.


  • Your new file-test is OK, but the logic of your new grep is wrong. You would report an error only if there are keys in $self, and none of them match $opt. Note that mine reports an error if there is at least one key that does not match. The difference is a consequence of De Morgan's Theorm from Boolean Algebra.

  • Good Luck,
    Bill

     
     


    Search for (options) Powered by Gossamer Forum v.1.2.0

    Web Applications & Managed Hosting Powered by Gossamer Threads
    Visit our Mailing List Archives