Can't print entire line.

N

Nene

Hi,

Underneath __DATA__ is a snippet of a file. I can't print the entire
line, only the first word. You will see '^M', when the file was
produced it appended those '^M'. What am I doing wrong:


#!/usr/bin/perl -w
use strict;

my @tmsh_show = <DATA>;

while (my $line = <@tmsh_show>) {
chomp($line);

print "$line\n";

}

__DATA__
--------------------------------------^M
Status ^M
Availability : available^M
State : enabled^M
Reason : The pool is available^M
^M
Traffic ServerSide^M
Bits In 45.2M^M
Bits Out 0^M
Packets In 79.8K^M
Packets Out 0^M
Current Connections 0^M
Maximum Connections 35^M
Total Connections 8.2K^M
^M
Ltm::pool Member: BatchESP_7438 172.16.70.66:7438^M

--------------------------------------------------^M
Status ^M
Availability : available^M
State : enabled^M
Reason : Pool member is available^M
^M
Traffic ServerSide General^M
Bits In 201.9K -^M
Bits Out 0 -^M
Packets In 342 -^M
Packets Out 0 -^M
Current Connections 0 -^M
Maximum Connections 3 -^M
Total Connections 41 -^M
Total Requests - 0^M
 
J

Jürgen Exner

Nene said:
Underneath __DATA__ is a snippet of a file. I can't print the entire
line, only the first word. You will see '^M', when the file was
produced it appended those '^M'. What am I doing wrong:

You are processing the file on a different OS then where it was created.

In other words: you are running into the differences between line
endings in Unix, DOS/Windows, and Apple.
Run the file through unix2dos or dos2unix (whichever way you need to do
it) and those mysterious ^M should disappear.

jue
 
P

Peter J. Holzer

Nene said:
What am I doing wrong:

It appears that you are trying to read twice from the same filehandle. [...]
use strict;

my @tmsh_show = <DATA>;


Every line has now been read into the array. The DATA file handle
is at end-of-file. You cannot read any more data from it.

while (my $line = <@tmsh_show>) {


It looks like you are trying to read data again, but I cannot
figure out where you are expecting to read the data from...

No, look again: He[1] tries to use @tmsh_show as a file handle. I haven't
the slightest idea what this is supposed to do, but what it appears to
do is split each element of @tmsh_show and return each field in turn.
With perl 5.10.1 program prints something like:

--------------------------------------^M
Status
^M
Availability
:
available^M
State
:
enabled^M
[...]

(which is not what the OP claims: It doesn't print the first word of
every line, it prints every word - but separately).

hp


[1] Or she, or whatever. I am always tempted to address people where I
can't guess whether they are male or female as "it", but that would be
impolite. The English language needs a gender-neutral personal pronoun,
like some Skandinavian languages have.
 
P

Peter J. Holzer

Peter J. Holzer said:
What am I doing wrong:

It appears that you are trying to read twice from the same filehandle. [...]

use strict;

my @tmsh_show = <DATA>;


Every line has now been read into the array. The DATA file handle
is at end-of-file. You cannot read any more data from it.


while (my $line = <@tmsh_show>) {


It looks like you are trying to read data again, but I cannot ^^^^^^^^^^^^^^^^^^^^^^^^^
figure out where you are expecting to read the data from...

No, look again: He[1] tries to use @tmsh_show as a file handle.


Using a filehandle _is_ trying to read data.

Sorry, I read that as "trying to read *the* data again", not reading
some other data which has not previously been read.

But the code isn't using @tmsh_show as a filehandle...




Deparse says it is a glob():

while (defined((my $line = glob(join($", @tmsh_show))))) {

Ah, yes. I forgot about the <> notation for glob. I never use that.

hp
 
I

Ilya Zakharevich

Deparse says it is a glob():

Of course. AFAIK, the rules of disambiguation of <> are very simple:
if it is a bareword or a $BARE::WORD, it is a readline(); otherwise it
is a glob. E.g., <${foo}> is a glob.

Yours,
Ilya
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,222
Members
46,809
Latest member
moe77

Latest Threads

Top