LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 03-07-2006, 04:24 AM   #1
paddyjoy
Member
 
Registered: Apr 2005
Location: Sydney
Distribution: FC5
Posts: 174

Rep: Reputation: 30
Passing parameters to PERL script


Is it possible to pass parameters to a perl script? I can do it with a bash script but not sure if it can be done with perl?


Code:
[paddy@paddy tmp]$ cat test.sh
#!/bin/sh

echo $0
echo $1
[paddy@paddy tmp]$ ./test.sh "Test input"
./test.sh
Test input
[paddy@paddy tmp]$
Also is there a way to loop through the input parameters? I need to be able to send a list of files to my perl script and have it prefrom an action on each one.

Thanks,
Paddy
 
Old 03-07-2006, 04:53 AM   #2
paddyjoy
Member
 
Registered: Apr 2005
Location: Sydney
Distribution: FC5
Posts: 174

Original Poster
Rep: Reputation: 30
Ok figured it out!

Code:
#!/usr/bin/perl

my $argc = 0;
    foreach my $arg (@ARGV) {
        printf "Argument %2d: '%s'\n", ++ $argc, $arg;
    }
 
Old 03-07-2006, 05:18 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,269

Rep: Reputation: 165Reputation: 165
perl -h

If you want to do something for each input file, then
Code:
while (<>) {
   blah blah
}
will iterate through a list of files on the command line (placed in @ARGV) or stdin.,

you can use the -p -n switches (perl -h)
which will assume a loop for you,

eg: perl -p -e 's/THIS/THAT/' will do it for each file or stdin
 
Old 03-07-2006, 11:04 AM   #4
scuzzman
Senior Member
 
Registered: May 2004
Location: Hilliard, Ohio, USA
Distribution: Slackware, Kubuntu
Posts: 1,851

Rep: Reputation: 47
Why don't you just:
Code:
#!/usr/bin/perl

foreach (@ARGV) {
  print "$_ \n";
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing parameters to a shell script neocookie Linux - General 5 10-20-2005 11:44 AM
Passing parameters to bash script Kamikazee Programming 4 10-01-2005 06:41 AM
passing parameters to functions in shell script kushalkoolwal Programming 1 09-28-2005 02:40 PM
Passing Parameters to Bash Script mooreted Linux - Software 3 04-05-2004 09:08 PM
passing java parameters to a unix script nephilim Programming 25 10-22-2003 10:51 AM


All times are GMT -5. The time now is 07:16 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration