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

How to read in data with pipe characters

by w3ntp (Beadle)
on Oct 07, 2010 at 12:12 UTC ( [id://863988]=perlquestion: print w/replies, xml ) Need Help??

w3ntp has asked for the wisdom of the Perl Monks concerning the following question:

This is a clarification of my prior question:
#!/usr/local/bin/perl chomp($_=$ARGV[0]); system `echo $_ > /tmp/gerbil.txt`; exit;
If I run the script this is what I get
/try.pl a|b|c|d bash: b: command not found bash: c: command not found bash: d: command not found
The program above is passed live data one line at a time. It does not contain any # charters. Any thoughtt? thanks

Replies are listed 'Best First'.
Re: How to read in data with pipe characters
by ww (Archbishop) on Oct 07, 2010 at 12:22 UTC
    Don't "clarify" by starting a new thread. Edit, <strike>ing as necessary (but leave an audit trail of your original post, so as to keep any replies in context.

    Reaped: How to read in data with pipe chars has been considered for reaping.

Re: How to read in data with pipe characters
by cdarke (Prior) on Oct 07, 2010 at 12:31 UTC
    /try.pl a|b|c|d

    It is unusual to run a script from the root directory, you probably mean:
    ./try.pl 'a|b|c|d'
    but that is a shell question, it has nothing to do with Perl.
Re: How to read in pipe characters
by Anonymous Monk on Oct 07, 2010 at 12:17 UTC
    The pipe character means something special to your shell: for most, it means to pipe the output of one command to the input of the next. Quote your argument string, eg "a|b". See your shell's documentation for quoting rules and special characters.
Re: How to read in data with pipe characters
by JavaFan (Canon) on Oct 07, 2010 at 13:34 UTC
    /try.pl a|b|c|d bash: b: command not found bash: c: command not found bash: d: command not found
    That's an error you're getting due to bash, not Perl. You tell bash to call "/try.pl" with the argument 'a', and pipe the result into b, its result into c, and that result into d.

    You probably want /try.pl 'a|b|c|d'. But, we aren't done. Once you have 'a|b|c|d', you feed it back into a shell. So, again, you need to escape it, but putting quotes around $_.

    But why are you writing a Perl program which all it does it take input, and feed the input back to another shell? Why not just:

    #!/usr/bin/sh echo $1 > /tmp/gerbil.txt
    However, if you do write it in Perl, why both the backticks, and system? Do you even know what the backticks do? Now you are saying: "Perl, please start a shell, and execute the command I'm giving you. Gather the output, and starts a second shell, executing whatever was written to STDOUT by the first shell." Is that really your intention?
Re: How to read in data with pipe characters
by Anonymous Monk on Oct 07, 2010 at 12:27 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-25 14:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found