siddheshsawant has asked for the wisdom of the Perl Monks concerning the following question:

hello monks, I think my previous question was not enough informative to understand so I am asking it in much simpler language.Following are the parts of my code

The initial part was as follows:

#constructor sub new { my ($class) = @_; #my ($proto) = @_; #to make the constructor both class and object method #my $class = ref($proto) || $proto; my $self = { #attributes of object _id => undef, _user_scenario => undef, _result_type => undef, _result_log_link => undef, _not_exposed_reason => undef, _error_excerpt => undef, _triage => undef, }; bless $self, $class; return $self; }

Then I wrote error_excerpt subroutine as follows:

sub error_excerpt { my ($self, $error_excerpt) = @_; return defined($error_excerpt)?$self->{_error_excerpt}:"No Error Excer +pt."; }

When I tried to access the value of error excerpt through subroutine call every time I gets "No Error Excerpt" as an output.Where as I want error excerpt value when it is defined and to print "No Error Excerpt." string when no error_excerpt defiend.I am using strict as well warnings in the code

Kindly let me know where I am doing mistake? Thanks in advance !!!!

Replies are listed 'Best First'.
Re: A question on defined loop
by FunkyMonk (Bishop) on Mar 16, 2010 at 16:33 UTC
    package Foo; # your posted code here package main; my $foo = Foo->new; $foo->{_error_excerpt} = "hi"; say $foo->error_excerpt(undef); # No Error Excerpt. say $foo->error_excerpt(1); # hi

    Is that not what you expect to see? Perhaps you should put a print inside error_excerpt and make sure that $error_excerpt really has the value you think it has.

Re: A question on defined loop
by almut (Canon) on Mar 16, 2010 at 16:33 UTC

    Please show a self-contained example that exhibits the problem.  In this revised sample, it's not clear how and where you're calling error_excerpt()...

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: A question on defined loop
by lostjimmy (Chaplain) on Mar 17, 2010 at 14:25 UTC
    It would make more sense if you were checking the object's error excerpt (i.e. $self->{_error_excerpt}) and not some value you pass to the method. I will have to assume (since you don't show it) that you are calling error_excerpt without any arguments, so it is always undef, and hence you always get "No Error Excerpt".

    Something like the following would make more sense:

    sub error_excerpt { my $self = shift; return defined($self->{_error_excerpt}) ? $self->{_error_excerpt} : +"No Error Excerpt."; }