Unexplained output for PERL print statement

X

Xavier

I have the following code in my perl script to generate some HTML:

printf "\<img src=\"Dir1/%s/%s\"\>\n", $dirname, $filename;

but the output instead of looking ilke

<img src="Dir1/dirname/filename">

looks like

">mg src="Dir1/dirname/filename

i cannot for the life of me figure out what is causing perl to munge
up such a simple output string...i've tried using sprintf to a
variable and then printing that variable but i get the same munged up
output. i've tried a straight print statement as well with the same
results.

could those with more experience offer some pointers? i'm running this
on redhat 9.0 with perl 5.8.0

thanks
 
J

Jürgen Exner

Xavier said:
I have the following code in my perl script to generate some HTML:

printf "\<img src=\"Dir1/%s/%s\"\>\n", $dirname, $filename;

Maybe a stupid question, but why are you using printf() to begin with?
There is nothing in your desired output that would require you to use
printf() instead of the much simpler print().

jue
 
M

Matthew Braid

Xavier said:
I have the following code in my perl script to generate some HTML:

printf "\<img src=\"Dir1/%s/%s\"\>\n", $dirname, $filename;

but the output instead of looking ilke

<img src="Dir1/dirname/filename">

looks like

">mg src="Dir1/dirname/filename

i cannot for the life of me figure out what is causing perl to munge
up such a simple output string...i've tried using sprintf to a
variable and then printing that variable but i get the same munged up
output. i've tried a straight print statement as well with the same
results.

could those with more experience offer some pointers? i'm running this
on redhat 9.0 with perl 5.8.0

thanks

What's in $filename? Is there maybe a weird character at the end?

It looks like printf initially puts:

<img src="Dir1/dirname/filename

but then gets a character that the terminal interprets as 'return to the
beginning of the line'. printf then prints out the final ">, which is
put at the start of the line instead of the end thanks to that funny
character.

Maybe there's a carriage return there without a line-feed?

MB
 
F

fifo

I have the following code in my perl script to generate some HTML:

printf "\<img src=\"Dir1/%s/%s\"\>\n", $dirname, $filename;

but the output instead of looking ilke

<img src="Dir1/dirname/filename">

looks like

">mg src="Dir1/dirname/filename

Well there's nothing wrong with your print statement, so I'd carefully
check that $dirname and $filename are set sensibly.

Also, it's usually considered more perlish not to use printf if you
don't need to. The above can be written more clearly as:

print qq|<img src="Dir1/$dirname/$filename">\n|;
 
R

Robert Stelmack

Xavier said:
I have the following code in my perl script to generate some HTML:
printf "\<img src=\"Dir1/%s/%s\"\>\n", $dirname, $filename;
but the output instead of looking ilke
<img src="Dir1/dirname/filename">
looks like
">mg src="Dir1/dirname/filename
Here is what I tried to do to replicate your problem as source:

#!/usr/bin/perl
use strict;
use warnings;
my $dirname ="DIR";
my $filename ="FIL";
printf "\<img src=\"Dir1/%s/%s\"\>\n", $dirname, $filename;

....and here is my output:

bash-2.05b$ x.pl
<img src="Dir1/DIR/FIL">
bash-2.05b$

....all run on cygwin with:

bash-2.05b$ perl -v

This is perl, v5.8.0 built for MSWin32-x86-multi-thread
(with 1 registered patch, see perl -V for more detail)

Copyright 1987-2002, Larry Wall

Binary build 805 provided by ActiveState Corp. http://www.ActiveState.com
Built 18:08:02 Feb 4 2003
i cannot for the life of me figure out what is causing perl to munge
up such a simple output string
<<snip>>

Try printing out the $dirname, $filename just before their use.
 
G

Glenn Jackman

Matthew Braid said:
What's in $filename? Is there maybe a weird character at the end?
Maybe there's a carriage return there without a line-feed?

Perhaps you're reading a DOS text file, and chomp-ing?

Can reproduce with:
$dirname='dir'
$filename="file\r"
printf "\<img src=\"Dir1/%s/%s\"\>\n", $dirname, $filename;
# ==> ">mg src="Dir1/dir/file

You might want to trim whitespace off the end of $filename:
$filename =~ s/\s+$//;
 
M

Michele Dondi

Subject: Unexplained output for PERL print statement
^^^^ ^^^^^
[snip]
I have the following code in my perl script to generate some HTML:

printf "\<img src=\"Dir1/%s/%s\"\>\n", $dirname, $filename;
^^^^^^

1. No such a thing called PERL, see 'perldoc -q difference'

2. It's not a print() statement, but a unnecessary, IMHO printf() one.
^^^^^^^^^^^

3. Also, what about qq|cool foo... "bar baz" foo again|?


Michele
 
G

gnari

[help with obvious \r embedded in print'ed variable value]
Try printing out the $dirname, $filename just before their use.

actually, for debug prints, allways do them like this:

print "dirname=[$dirname] filename=[$filename]\n" if $debug;

this will make any whitespace/CR be more visible, as the
case where $dirname='a,b';$filename='';

gnari
 
X

Xavier

Jürgen Exner said:
Maybe a stupid question, but why are you using printf() to begin with?
There is nothing in your desired output that would require you to use
printf() instead of the much simpler print().

i have tried all variants of the print statement and just using printf
in the example.
 
X

Xavier

glenn...u are right, i am reading filenames from a dos file and
chopping. ur suggestion helped solve the problem. could u explain why
just a chop wasn't sufficient? i guess i'm not too familiar with the
dos representation of a newline.

thanks
x.
 
T

Tad McClellan

Scott Bryce said:
Chomp removes all trailing
^^^
^^^ no it doesn't
newline characters.


chomp() removes whatever is in $/ (apart from
the special "paragraph mode").

If $/ = "\n" (the default), then chomp() removes ONE trailing
newline character.

If its argument is an array, chomp() removes ONE trailing
newline character from each array element.
 
M

Michele Dondi

Chop removes the last character in a string. Chomp removes all trailing
newline characters.

In a OS independent way too! Modulo Tad's explanation, of course...


Michele
 
J

Joe Smith

Scott said:
Because a DOS file uses a CR/LF pair to as a newline character. Chop
removed the Line Feed and left the Carriage Return in place. Chomp would
have removed both the Carriage Return and the Line Feed.

When reading a DOS file on Windows, chomp removes both characters.

When reading a DOS file on Linux (Samba shares or FTP binary or socket),
chomp() won't remove the CR part of CRLF unless the $/ variable has
been explicitly set.
-Joe
 
T

Tad McClellan

When reading a DOS file on Windows, chomp removes both characters.


Not really.

perl's input/output routines do the translation of line endings.


When reading a DOS file on Windows, the input routine removes the
CR and chomp removes the LF.


ie. the CR is gone before chomp() is called.
 

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,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top