in reply to Question on XML output using Perl

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

...what the A.M. said.  In particular, where do $self and $error_excerpt come from?  For example, did you just leave out the my $self = shift; at the beginning of the routine in this sample code only, or is it also missing from the real code?

In case $error_excerpt is not available in the scope of the routine, you'd always get "No Error Excerpt.".  This would be the case if $error_excerpt is defined as a lexical variable in another routine.  In this case, an empty package variable of the same name would (silently) be used instead — unless you enable strictures...

Always use strict; use warnings; — it would likely have warned you of these types of errors.

Replies are listed 'Best First'.
Re^2: Question on XML output using Perl
by siddheshsawant (Sexton) on Mar 16, 2010 at 16:08 UTC

    I have typed this at initial part of the code...

    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 sub error_excerpt.For general output also I am not getting error_excerpt if there is any when I used

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

    Is there anything missing? I have used use strict and use warnings as well.Where I am doing mistake?

      Perhaps, as other monks have mentioned, something along these lines might point you in the right direction.
      #!/usr/bin/perl use strict; use warnings; my $e = Error::Excerpt->new; print $e->get_error_excerpt, qq{\n}; $e->set_error_excerpt(q{We have an error}); print $e->get_error_excerpt, qq{\n}; package Error::Excerpt; sub new { my $class = shift; my $self = { _error_excerpt => undef, }; bless ($self, $class); return $self; } sub set_error_excerpt{ my $self = shift; my $ee = shift; $self->{_error_excerpt} = $ee; } sub get_error_excerpt{ my $self = shift; my $ee = $self->{_error_excerpt}; return $ee?$ee:q{No Error Excerpt}; }
      No Error Excerpt We have an error
      imo, dividing up the task using getters and setters is often worth considering. Add error checking as appropriate.

        If I keep a code like this then I gets error_escrpt where ever it is defined ....I am getting the value from the database...the code is as follows:

        $self->{_error_excerpt} = $error_excerpt if defined($error_excerpt); r +eturn $self->{_error_excerpt};

        But the problem with this code is it do not print anything when there is no error excerpt.

      You still haven't answered where $error_excerpt comes from... but on second thought, maybe you meant (?)

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

        If I keep a code like this then I gets error_escrpt where ever it is defined ....I am getting the value from the database...the code is as follows:

        $self->{_error_excerpt} = $error_excerpt if defined($error_excerpt); return $self->{_error_excerpt};

        But the problem with this code is it do not print anything when there is no error excerpt.