Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Read into stdin pipe characters

by w3ntp (Beadle)
on Oct 05, 2010 at 19:43 UTC ( #863668=perlquestion: print w/ replies, xml ) Need Help??
w3ntp has asked for the wisdom of the Perl Monks concerning the following question:

I have a perl program that needs to read in characters that contain the "|" character without any "'s

If I have a simple program

#!/usr/local/bin/perl $stuff = @ARGV[2] print $stuff; exit
the program croakes when I pass it a b c|d|e|f
use of " is not allowed

any ideas?

thanks
w3ntp

Comment on Read into stdin pipe characters
Download Code
Re: Read into stdin pipe characters
by toolic (Chancellor) on Oct 05, 2010 at 20:07 UTC
      This input string does not contain quotes. Otherwise this would be easy
        First, please use <code> tags for your code. It's very, very difficult to read your program.

        Are you sure the error is coming from perl? How are you passing pipe characters on the command line if you're not using quotes?

        Also, please "use strict;" and "use warnings;" before posting:
        <code>

        #!/usr/local/bin/perl use strict; use warnings; my $stuff = $ARGV[2]; print $stuff; exit;

        </code>

        You seem to be confusing the string and the literal (piece of shell code) that produces the string.

        The string may not have quotes, but the shell string literal that produces the string must use quotes or some alternative. For example, with bash, you could use any of the following shell commands:

        my_prog.pl a b 'c|d|e|f' my_prog.pl a b "c|d|e|f" my_prog.pl a b c\|d\|e\|f

        In all cases, a string of four letters separated by pipes will be passed to Perl by the shell. print $ARGV[2] will result in the following:

        c|d|e|f
Re: Read into stdin pipe characters
by dasgar (Deacon) on Oct 05, 2010 at 23:09 UTC

    First, I'm going to assume that you're running on a *nix platform based off of your shebang line.

    To call your script, you do have to encapsulate the desired pipe character inside of quotes. If you don't, the shell with believe that the pipe character is telling it to do something. Also, encapsulating it in quotes will not pass the quotes to your script.

    For example, let's say you try calling your program like this: perl script.pl b c|d|e|f. Here's how your shell will process that.

    1. Need to call the perl executable with parameters
    2. script.pl is the first parameter to pass
    3. b is the next parameter to pass
    4. c is the next parameter to pass
    5. Pipe that output to d
    6. Pipe that output to e
    7. Pipe that output to f

    In this case, I'd agree with ReturnOfThelonious that the error is from the shell and not your script. If you were instead to call your script like this : perl script.pl b "c|d|e|f", here's how your shell would process it.

    1. Need to call the perl executable with parameters
    2. script.pl is the first parameter to pass it
    3. b is the next parameter to pass
    4. c|d|e|f is the next parameter to pass

    Notice the lack of quotes in the last step? The quotes will basically tell the shell: "Hey, treat everything in side this quote as a parameter and DO NOT view this content as instructions for you to do". The quotes are not actually passed by the shell into your script. This is why toolic suggested that you "quote your passed strings".

    I apologize for the length of this post. But since you didn't seem to quite understand what toolic was suggesting, I thought I'd go the extra length to clearly explain the quoting from the shell's perspective.

Re: Read into stdin pipe characters
by mjscott2702 (Pilgrim) on Oct 06, 2010 at 08:02 UTC
    Further to previous posters' comments on quoting (for the shell) and using code tags, it would be useful to include the error, since it is almost certainly the *nix shell that is throwing the error. The perl script itself should happily read in any characters and not assign any special meaning to them (well, apart from eol and other "real" special characters)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (13)
As of 2014-09-02 20:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (29 votes), past polls