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

Having to remove \r from $ARGV[-1]

by JPMH (Novice)
on Nov 15, 2012 at 12:37 UTC ( #1004004=perlquestion: print w/ replies, xml ) Need Help??
JPMH has asked for the wisdom of the Perl Monks concerning the following question:

I find I'm having to explicitly remove \r from $ARGV-1 before Getopt::Long will process command lines properly from a shell script. Is this normal ?

Unless I put $ARGV[-1] =~ s/\r//; at the top of my subroutine before getting the command-line arguments from GetOptions(), GetOptions can't cope with the extra \r there. So for example if the final argument is a filename, then 'filename\r' gets passed back instead of the filename itself, which then can't be found.

Admittedly, I probably have myself to blame, by running sh scripts on Cygwin to call my perl scripts, but using a Windows editor to write them, and not being careful to specify any particular linefeed behaviour.

But presumably this can't be that uncommon. Should I be surprised at having to strip the extra '\r's by hand, rather than being able to leave it all to GetOptions ?

Comment on Having to remove \r from $ARGV[-1]
Select or Download Code
Re: Having to remove \r from $ARGV[-1]
by bart (Canon) on Nov 15, 2012 at 12:48 UTC
    You can add a space at the end of the line, that'll probably take care of it. Unless your editor is set to drop trailing spaces...

    More reliable is to use " --" to tell GetOptions that there are no more options. In that case, the trailing CR will probably be ignored.

    Note that in a more recent perl (= less than a few years old), CR inside source code is automatically handled, because in older perls, it did choke on it (e.g. when running a Windows text script on Linux). But not for the shebang line (and I honestly don't know what happens now, with multiline strings).

    Of course if you had a bit of discipline and set the line endings to Unix in your text editor, you wouldn't have this problem. Most decent text editors allow you to chose a default line ending for new file, or, I would hope so.

Re: Having to remove \r from $ARGV[-1]
by McA (Priest) on Nov 15, 2012 at 15:24 UTC

    Hi JPMH!

    I just wrote the following script for cygwin perl:

    use strict; use warnings; use Data::Dumper; print Dumper(\@ARGV);

    ... and called it with perl a.pl arg1 arg2. I couldn't see a \r at the end of the line. So my guess is: In your shell script where you call the perl script is a extra \r at the end of the line. Look at your editor settings there.

    Best regards
    McA

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (9)
As of 2014-12-22 12:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (116 votes), past polls