Operating System - HP-UX
1825793 Members
2598 Online
109687 Solutions
New Discussion

Re: Strange print behaviour in perl

 
Mark McDonald_2
Trusted Contributor

Strange print behaviour in perl

Hi All,

We are developing a checking script written in perl, it runs some scripts on a host then emails the output as csv to allow it to be imported in to excel.

The report is started with:

open(MAILX,"|/usr/bin/mailx -s \"$type Handover report for $host\" $email");
print MAILX "$host: $uname\n\n $r/$c checks failed, details below:\n\n";

foreach(@$errors) {
print MAILX "\t$_\n\n";


Then it runs through thte output from each script, creating the csv details, see example below:

print MAILX "23,HW/ OS,OS (Red Hat),(Red Hat Linux specific item / TBD) t ," ;
if($results->[23]{result})
{ print MAILX "$results->[23]{result}"; }
print MAILX "," ;
if($results->[23]{error})
{ print MAILX "$results->[23]{error}"; }
print MAILX "\n"; <+++++++++++

print MAILX "24,HW/ OS,OS (SuSE),(SuSE Linux specific item / TBD) ," ;
if($results->[24]{result})
{ print MAILX "$results->[24]{result}"; }
print MAILX "," ;
if($results->[24]{error})
{ print MAILX "$results->[24]{error}"; }
print MAILX "\n";


The creates output like:
.
.
6,HW/ OS,General,OS internal disk mirroring configured properly ,FAIL,/dev/vg00/lvolu11 not mirrored.
.
.
23,HW/ OS,OS (Red Hat),(Red Hat Linux specific item / TBD) t ,N/A,NON-HPUX
24,HW/ OS,OS (SuSE),(SuSE Linux specific item / TBD) ,N/A,NON-HPUX
25,Backup,Backup,scheduled filesystem backup configured and working ,PASS,OK


However - Sometimes the newline marked with <+++++++ fails to show in the email. The new line is wholly dependent on the line ending TBD) t ," ;
If there are less than 3 spaces before the comma the newline fails to print. The t is in there for testing purposes. Any Ideas why?

We are having similar problems with other lines, too, some seem to require 4 spaces before the comma, others are happy with 2.

Anyone come across this before?

13 REPLIES 13
James R. Ferguson
Acclaimed Contributor

Re: Strange print behaviour in perl

Hi Mark:

This doesn't make sense. Have you set the 'stict' and 'warnings' pragma in your script? If not, at least for the purposes here do 'use warnings;'

Regards!

...JRF...
Mark McDonald_2
Trusted Contributor

Re: Strange print behaviour in perl

JRF - I agree it makes no sense at all. I did not write the script and I do not know perl. But I'm learning fast!!!!

I added "use warnings;" near the top and got an error:
Can't locate warnings.pm in @INC

So give me a few minutes with my good friend google....
Mark McDonald_2
Trusted Contributor

Re: Strange print behaviour in perl

ok - perl is version 5.005_03
Mark McDonald_2
Trusted Contributor

Re: Strange print behaviour in perl

It is using the "-w" on the shebang line...
James R. Ferguson
Acclaimed Contributor

Re: Strange print behaviour in perl

Hi (again) Mark:

> Can't locate warnings.pm in @INC

This would suggest that your version of Perl is very old. To see your version, do:

# perl -v

In lieu of the 'warnings' pragma, you can use the older:

#!/usr/bin/perl -w

...at the interpreter line.

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: Strange print behaviour in perl

HI (again) Mark:

My guess is that your data-dependent behavior may involve things like carriage-return (\r) or backspace (\010) characters.

For example:

# perl -e 'print STDERR "hi","\r";print "?\n"'
?i

You might try adding an automatic linefeed to print statements with the '-l' switch. Hence on your shebang (interpreter line):

#!/usr/bin/perl -wl

Regards!

...JRF...
Mark McDonald_2
Trusted Contributor

Re: Strange print behaviour in perl

OK - I will give that a try - however due to the way is is printing the data, I will need some way of supressing it for some of the print statements.

Does "printf" work in perl?
Hein van den Heuvel
Honored Contributor

Re: Strange print behaviour in perl

1) Yes, printf works find in perl,

2) 5.005_03

That's wicked old.
Not having looked for a solid timeline,
I would guess over 10 years.
5.6 was normal 5 years back, 5.8 pretty standard. 5.10 desirable most platforms.
Just a gutfeel.

Hein.



James R. Ferguson
Acclaimed Contributor

Re: Strange print behaviour in perl

HI (again) Mark:

> Does "printf" work in perl?

Yes, and that would be the most appropriate way to handle your needs. The 'printf' function works just like you would expect.

Regards!

...JRF...
Mark McDonald_2
Trusted Contributor

Re: Strange print behaviour in perl

Thanks all.

I will approach the boss regarding upgrading the perl version, and have a play with the "-l" and printf.
Mark McDonald_2
Trusted Contributor

Re: Strange print behaviour in perl

Just been informed there is a perl 5.8 on the box, so will be playing with that too!!!
Mark McDonald_2
Trusted Contributor

Re: Strange print behaviour in perl

OK, what is this line doing?

if($results->[9]{result}) { print MAILX "$results->[9]{result}";

I guess its - if array result position 9 contains something, then print it....

Can some one help me understand this bit?
Mark McDonald_2
Trusted Contributor

Re: Strange print behaviour in perl

Thanks guys - using 5.8 now, and its doing what it is supposed to with print and \n where needed.

Script is a lot tidier now and I am adding comments as I work out what each bit does. Another few days of this and Perl scripting will be going on my CV!!!!