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

Good day bros. I am trying to use XML::RSS to create and save an RSS file. I am creating the rss object with the defaults i.e. my $rss = XML::RSS->new(); which according to the docs should create an RSS 1.0 file with UTF-8 encoding. Everything goes fine in terms of creating the object, but when I issue rss->save('my.rss') I get the following errors:
$text is undefined in XML::RSS::_encode(). We don't know how to handle + it! at c:/Perl64/site/lib/XML/RSS/Private/Output/Base.pm line 84. XML::RSS::Private::Output::Base::_default_encode('XML::RSS::Privat +e::Output::V1_0=HASH(0x58e7a98)', undef) called at c:/Perl64/site/lib +/XML/RSS/Private/Output/Base.pm line 76 XML::RSS::Private::Output::Base::_encode('XML::RSS::Private::Outpu +t::V1_0=HASH(0x58e7a98)', undef) called at c:/Perl64/site/lib/XML/RSS +/Private/Output/Base.pm line 853 XML::RSS::Private::Output::Base::_out_seq_items('XML::RSS::Private +::Output::V1_0=HASH(0x58e7a98)') called at c:/Perl64/site/lib/XML/RSS +/Private/Output/V1_0.pm line 83 XML::RSS::Private::Output::V1_0::_output_rss_middle('XML::RSS::Pri +vate::Output::V1_0=HASH(0x58e7a98)') called at c:/Perl64/site/lib/XML +/RSS/Private/Output/Base.pm line 1107 XML::RSS::Private::Output::Base::_render_complete_rss_output('XML: +:RSS::Private::Output::V1_0=HASH(0x58e7a98)') called at c:/Perl64/sit +e/lib/XML/RSS.pm line 488 XML::RSS::_render_complete_rss_output('XML::RSS=HASH(0x4f4ae0)', 1 +.0) called at c:/Perl64/site/lib/XML/RSS.pm line 500 XML::RSS::as_rss_1_0('XML::RSS=HASH(0x4f4ae0)') called at c:/Perl6 +4/site/lib/XML/RSS.pm line 568 XML::RSS::__ANON__[c:/Perl64/site/lib/XML/RSS.pm:568]() called at +c:/Perl64/site/lib/XML/RSS.pm line 557 XML::RSS::_output_env('XML::RSS=HASH(0x4f4ae0)', 'CODE(0x58e7de0)' +) called at c:/Perl64/site/lib/XML/RSS.pm line 569 XML::RSS::as_string('XML::RSS=HASH(0x4f4ae0)') called at c:/Perl64 +/site/lib/XML/RSS.pm line 1370
I tried setting explicit encoding in the new method, and that didn't help. Anyone know what is wrong?

Replies are listed 'Best First'.
Re: XML::RSS save crashing
by boftx (Deacon) on May 10, 2015 at 22:37 UTC

    It looks like someone (you?) is sensibly using use strictures; or use warnings FATAL => 'all'; somewhere.

    Where is $text coming from? My gut reaction is that you are unexpectedly assigning a value of undef to it somewhere, possibly thinking you have an empty string instead. If you are in fact making the assignment in your code you might try this: my $text = $some_str || ''; to ensure a defined value if an empty string is acceptable.

    If it is not, then you need to find out why the data is coming in as undef to begin with.

    Thinking about it further, I suspect you are passing $some_str as a parameter to a method. You could still use the $some_str || '' construct, but it feels more like you should track down why that is undef to begin with.

    You must always remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.
      $text = $some_str || ''

      Please note that the defined-or operator is //, not ||. $text = $some_str || '' silently converts 0 to the empty string due to boolean evaluation of $some_str, causing even more subtile bugs.

      The defined-or operator was introduced in perl v5.10. For older perl versions, you need to use something like $text=defined($some_str) ? $some_str : ''.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)