CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
passing input to perl command

 



Achilles
Novice

Aug 13, 2009, 12:41 AM

Post #1 of 19 (2290 views)
passing input to perl command Can't Post

Hi,

In awk in can pass input to a command like this


Code
echo "1 2" | awk '{p=$1+$2;print v}'

or I can use v option to pass input to awk command

In the similar way How can I pass input to a perl command. For example :


Code
perl -pi -e 'if ( $. == 2 ) {print "Hello\n" }' input.txt


In the above command; number 2 and the string "Hello" are hard coded. How can I pass that through parameter?

I want to do something like :


Code
echo "2 Hello" | perl -pi -e 'if ( $. == $1 ) {print "$2\n" }' input.txt


Thanks in advance.


spider
User


Aug 13, 2009, 1:17 AM

Post #2 of 19 (2287 views)
Re: [Achilles] passing input to perl command [In reply to] Can't Post

Either I have missunderstood you, or you have missunderstood perl a bit. To me it looks like you try do only use the perl binary program on the commandline without writing your perl program.

In perl you write your program, and it can take what ever input you would like. Like in a shell-script you can on unix add the path to the perl-binary on the first line of the script like:
#!/usr/bin/perl
Then you don't have to refer to the perl binary on the commandline, so insted of saying > perl hello.pl
you just say> hello.pl
All input arguments are stored in an array @ARGV, so the first parameter is $ARGV[0].
A simple script could therefor be:

Code
#!/usr/bin/perl 
print "Name: $ARGV[0]\n" ;
print "Age: $ARGV[1]\n" ;

Usage from commandline:
hello.pl Bill 45
Gives output:
Name: Bill
Age: 45

Have a look at
http://perldoc.perl.org/perlintro.html for more intro to perl.


Achilles
Novice

Aug 13, 2009, 1:24 AM

Post #3 of 19 (2285 views)
Re: [spider] passing input to perl command [In reply to] Can't Post

Thanks for your reply.
Yes I want to execute that code as 'one liner' from my unix command line, instead of putting it in a file.

Is it possible to pass inputs to such perl commands?


spider
User


Aug 13, 2009, 1:32 AM

Post #4 of 19 (2282 views)
Re: [Achilles] passing input to perl command [In reply to] Can't Post

Can you describe some more what you want to do?

Why don't you want to have a perl-program?
You can still use it from the commandline, and it will be less to write every time. You can create the program generic, so you can use it for several things.


vikas.deep
User

Aug 13, 2009, 1:42 AM

Post #5 of 19 (2282 views)
Re: [spider] passing input to perl command [In reply to] Can't Post


Code
#!/usr/bin/perl 
Then you don't have to refer to the perl binary
on the commandline, so insted of saying > perl hello.pl
you just say> hello.pl

but when I did the same I get the error

Code
[root@localhost Documents]# infernal.pl 
bash: infernal.pl: command not found

Even though I have added the shebang command in infernal.pl as suggested by you and many other perl programmers but still I have to say perl infernal.pl and not infernal.pl as I would like

Quote
-For all my suggestions " I am sure someone else can do it in a better or elegant manner!"


Achilles
Novice

Aug 13, 2009, 1:55 AM

Post #6 of 19 (2280 views)
Re: [spider] passing input to perl command [In reply to] Can't Post

Thanks for the reply.

I'm writing a shell script to do standard checks in java files.
In one place of that script, I want to modify a line of the java file using the one line perl command that I posted above.

If I make that piece of code as a perl script file, I need to supply two script files to developers. If I keep that as part of my shell program I just can ship that once script file.

Is that not possible to pass parameters to command-line perl?


ichi
User

Aug 13, 2009, 2:00 AM

Post #7 of 19 (2278 views)
Re: [Achilles] passing input to perl command [In reply to] Can't Post


Code
# echo 1 2 | perl -e 'print <>' 
1 2



Achilles
Novice

Aug 13, 2009, 2:10 AM

Post #8 of 19 (2275 views)
Re: [ichi] passing input to perl command [In reply to] Can't Post

Thanks Ichi. How can I access the individual values.

For example, If I want to sum that 1 & 2 that is passed from echo.

something like, echo "1 2" | perl -e 'print arg1+arg2'
where arg1=1 (received from echo)
arg2=2(received from echo)


spider
User


Aug 13, 2009, 2:15 AM

Post #9 of 19 (2273 views)
Re: [vikas.deep] passing input to perl command [In reply to] Can't Post

is internal.pl in your path?
It also have to be executable (chmod +x infernal.pl).
If not in your path, you can adress it with full path (or just ./infernal.pl if you are in the same directory).


ichi
User

Aug 13, 2009, 2:17 AM

Post #10 of 19 (2270 views)
Re: [Achilles] passing input to perl command [In reply to] Can't Post


In Reply To
Thanks Ichi. How can I access the individual values.

For example, If I want to sum that 1 & 2 that is passed from echo.

something like, echo "1 2" | perl -e 'print arg1+arg2'
where arg1=1 (received from echo)
arg2=2(received from echo)

if you are going to do it that way, you might as well as in 1 and 2 as arguments to the Perl script. ( i think spider has shown you how).


vikas.deep
User

Aug 13, 2009, 2:20 AM

Post #11 of 19 (2269 views)
Re: [spider] passing input to perl command [In reply to] Can't Post

yes yes this PATH thing please explain how to set the path
when I tried ./infernal.pl
I get

[root@localhost Documents]# ./infernal.pl
bash: ./infernal.pl: Permission denied

that change mode chmod command is it to be executed from command line
-For all my suggestions " I am sure someone else can do it in a better or elegant manner!"


spider
User


Aug 13, 2009, 2:26 AM

Post #12 of 19 (2267 views)
Re: [ichi] passing input to perl command [In reply to] Can't Post


In Reply To

Code
# echo 1 2 | perl -e 'print <>' 
1 2



Or just:

Code
# echo 1 2



spider
User


Aug 13, 2009, 2:36 AM

Post #13 of 19 (2263 views)
Re: [vikas.deep] passing input to perl command [In reply to] Can't Post

Setting environment variables on unix is shell dependent, but in ex bash you can do something like:

Code
export PATH=$PATH:/mydir/my_bin

The $PATH holds the existing PATH, so you need to include that not to loose all the old. It is seperated with colon (:) so than you can just add what you want.

Regardles of the PATH you need to make your file executable.
When doing that ./file should work without having to set the path. The path is just where your shell looks for executables.
To make the file executable you use the chmod command.
You can just run it from the commandline:

Code
chmod +x filname

or, you could do

Code
chmod 755 filename



spider
User


Aug 13, 2009, 2:48 AM

Post #14 of 19 (2262 views)
Re: [Achilles] passing input to perl command [In reply to] Can't Post

You can get the sum of 1 and 2 by sending 1 and 2 as parameters to perl as ichi says:

Code
perl -e 'print $ARGV[0] + $ARGV[1]' 1 2


But the solution is not good.
If you want one file only, and want it to be a shell-script, then you should solve your issu in the shellscript.
An alternativ solution (the one I would have used), is to write the complete script as a perl-script, and than you could distribute only this. Alternativ you can do a mixed solution, but then you have to distribute two files.


Achilles
Novice

Aug 13, 2009, 4:19 AM

Post #15 of 19 (2257 views)
Re: [spider] passing input to perl command [In reply to] Can't Post

Thanks for your replies.
I made my mind to ship two files. Can anyone please let me know how to put the following code in to perl file?


Code
perl -pi -e 'if ( $. ==  ARGV[1]  ) {print "ARGV[2]\n" }' input.txt

The following doesn't work. As you see I'm a perl noob :(

Code
#!/usr/bin/perl -pi -e 
if ( $. == ARGV[1] ) {print "ARGV[2]\n" }' ARGV[3]

I want to pass ARGV[3] as the file name to be modified


(This post was edited by Achilles on Aug 13, 2009, 4:26 AM)


spider
User


Aug 13, 2009, 5:05 AM

Post #16 of 19 (2251 views)
Re: [Achilles] passing input to perl command [In reply to] Can't Post

Can you please describe the input and what you want?

A good tip for your programing is to add the following lines to the top of your code:

Code
#!/usr/bin/perl 
use strict ;
use warnings ;
use diagnostics;


Then


Achilles
Novice

Aug 13, 2009, 5:35 AM

Post #17 of 19 (2249 views)
Re: [spider] passing input to perl command [In reply to] Can't Post

I want my program to insert text "Hi" at 2nd line of inputfile
.
I want to pass inputfile, line number to be modified & text to be inserted

like

#myscript.pl inputfile,2,"Hi"


spider
User


Aug 13, 2009, 10:56 PM

Post #18 of 19 (2236 views)
Re: [Achilles] passing input to perl command [In reply to] Can't Post

I'm not sure of a good way of dooing this.
I have moved around in files with the seek function (take a look at http://perldoc.perl.org/functions/seek.html), but that is a long time ago. The seek also just set position based on bytes, not line numbers.
If the file you are manipulating is not very big, I would probably just read the the complete file into an array, and do the tings after. It is not an elegant way, but that is a way I know.
Then it is easy to make a loop, and add things where you want.


vikas.deep
User

Aug 14, 2009, 4:06 AM

Post #19 of 19 (2229 views)
Re: [Achilles] passing input to perl command [In reply to] Can't Post

When you say

Quote
2nd line of inputfile

I must quote from the perl documentation that

Quote
Perl's idea of what constitutes a line may not match yours

You may want to use special variable "$." and as Spider has suggested seek().
Please read http://perldoc.perl.org/perlvar.html for the docmentation of "$."
-For all my suggestions " I am sure someone else can do it in a better or elegant manner!"

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives