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

Hello monks, I am trying to print xml type output using perl for which I used XML::Simple module.When I check one value is defined or not like this

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

and I print corresponding value in sub print like this:

if(defined $self->error_excerpt()) { printf("\t\tError Excerpt:\t\t%s\n", $self->error_excerpt()); }

I am getting an output

<error_excerpt>No Error Excerpt</error_excerpt>

although the error excerpt value is defiened..

One more thing when I used

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

I gets proper error excerpt value where the error_excerpt is defined and I do not get any value and just gets

<error_excerpt></error_excerpt>

where I actually wants to print "No error Excerpt exist"

Can anybody tell me where I am doing mistake?If possible rectify me.Thanks in advance !!!!

Replies are listed 'Best First'.
Re: Question on XML output using Perl
by almut (Canon) on Mar 16, 2010 at 15:53 UTC
    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.

      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.

        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."; }
Re: Question on XML output using Perl
by Anonymous Monk on Mar 16, 2010 at 15:38 UTC