in reply to Re: Question on XML output using Perl
in thread Question on XML output using Perl

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?

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

Re^3: Question on XML output using Perl
by almut (Canon) on Mar 16, 2010 at 16:17 UTC

    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.

        If there is no error excerpt:
        #!/usr/bin/perl use strict; use warnings; use XML::Simple qw(:strict); 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; } sub error_excerpt { } if ( defined('_error_excerpt') ) { print "error Excerpt\n"; } else { if ( not defined('_error_excerpt') ) { print "No error Excerpt exists\n"; } }