Search code examples
bashperlhtml-entitieshtml-encode

Perl script with library function produces no output


I created this small little maintenance script to unescape HTML entities.

use HTML::Entities;

use warnings;

while ( <> ) {
    decode_entities($_);
}

The problem is, it produces no output when I pipe it through bash like so,

echo "&quot;a&quot;" | perl ../../tmp.pl

Solution

  • If you want to see some output, you'd better print/say the return value:

    use HTML::Entities;
    
    use warnings;
    
    while (<>) {
        print decode_entities($_);
    }
    

    Since in void context, the function modifies the string in-place, you could convert your script into a one-liner pretty easily:

    perl -MHTML::Entities -pe 'decode_entities($_)'
    

    Like a while (<>), loop, the -p switch loops over each line of the input (either standard input, or the filename arguments), with the addition of a print in the continue block, which is run once at the end of each iteration of the loop.