Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Win32/Linux portability

by thelycaeum (Initiate)
on Aug 03, 2011 at 17:12 UTC ( #918334=perlquestion: print w/ replies, xml ) Need Help??
thelycaeum has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I have a question:

Is there a function like chomp that removes carriage returns?

Comment on Win32/Linux portability
Replies are listed 'Best First'.
Re: Win32/Linux portability
by ikegami (Pope) on Aug 03, 2011 at 18:18 UTC

    chomp removes whatever you tell it to via $/.

    But you might be better off just using s/\s+\z//; instead of chomp;. Note: It removes trailing space and other whitespace in the process. That's usually a good thing.

      Thanks for your replies, I'm new to Perl so I'm not fit with that abbreviated syntax, e.g. "s/\s+\z//". I guess I'll stick with telling chomp how to handle arguments. I thought about something like:
      if (uc($^O)=~"WIN") {$/="\r\n"}

        I thought about something like:

        No need for that. See perlintro and use

        s/\r\n\z//; # or $line =~ s/\r\n\z//;
Re: Win32/Linux portability
by jethro (Monsignor) on Aug 03, 2011 at 17:41 UTC
    No, but a simple regex will do that:  $line=~s/\r//g;
Re: Win32/Linux portability
by planetscape (Chancellor) on Aug 03, 2011 at 18:52 UTC
Re: Win32/Linux portability
by zek152 (Pilgrim) on Aug 03, 2011 at 17:27 UTC

    My guess is that $output contains a "\r" at the end.

    #! /usr/bin/perl $somenum=5; $output="text.txt\r"; $output2="text.txt"; print " $somenum means that, $output has info.\n"; print " $somenum means that, $output2 has info.\n"; #OUTPUT # has info.s that, text.txt # 5 means that, text.txt has info.

    Without more context I can't provide more help.

    To get rid of a carriage return you can:

    $output =~ s/\r//g;

    Update: The OP changed the post so most of the info is out of context. The last remark should answer the current question.

      Original content (with tags cleaned up; no other changes):

      Hi all,

      I have a question:

      When I try to use a coed line like

      print "  $somenumber means that, $output has info.\n";

      I get different screen outputs in Windows and Linux (tried different shells).

      In windows it looks like it's supposed to be, which is:

      3 means that, text.txt has info.

      But under Linux it looks like this:

      has info.hat, text.txt

      So it looks like it goes back to the start of the line and overwrites the first part of the string after a variable is supposed to be printed. Why is that?


      ww observes that the output shown does not support the supposition of the OP (immediately above)... BASED on the info supplied.

      OP: This is why we urge that you post sufficient code to reproduce the problem; at least a snippet of sample data; error messages (verbatim) if any; and (the only one you gave us) how the output fails to satisfy your expectations/desires. Please see:

      And please do not delete content you've posted. Use <strike>...</strike> if you must delete something, and mark updates with notice this:

      Update: Added matter below the dashes; then reupdated to fix typos and markup.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://918334]
Approved by planetscape
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2015-11-29 22:14 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (753 votes), past polls