LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Perl: Getting a script to accept arguments (https://www.linuxquestions.org/questions/programming-9/perl-getting-a-script-to-accept-arguments-48992/)

JStew 03-09-2003 03:17 PM

Perl: Getting a script to accept arguments
 
Here is what I wrote so far:

#! /usr/bin/perl -w
# match.pl

system("clear");
print "Please enter a file to be searched: ";
$file_searched = <STDIN>; chomp $file_searched;

open (FILE1, $file_searched) || die "File not found.\n";
@line = <FILE1>;
close FILE1;

print "What text pattern do you want to search for? ";
$pattern = <STDIN>; chomp $pattern;

$line_number = 1;
foreach $line (@line) {
if($line =~ m/$pattern/g) {
print "Found another \"$pattern\" at Line Number: $line_number.\n";
}
$line_number++;
}

exit 0;

END OF CODE

What can i do to add to this that will accept an option like this:
./match.pl filename search_string

????

llama_meme 03-09-2003 04:52 PM

Well, you need to use the @ARGV array.

Alex

acid_kewpie 03-09-2003 04:56 PM

probably want Getopts in there too if it gets more complex.

JStew 03-09-2003 05:13 PM

Quote:

Well, you need to use the @ARGV array.
How would I implement that into a Perl script since it doesnt have an int main(int argc, char *argv[])?

llama_meme 03-09-2003 05:15 PM

It's just an already-existing global variable.

Alex

JStew 03-10-2003 04:47 AM

ok, ok... now really...
what would this code look like? :-)

llama_meme 03-10-2003 04:57 AM

@ARGV is just an array of the arguments passed to the program (which I can't be bothered to write for you just now; someone else might). There's a lot of freely available documentation on these kinds of Perl basics, not least the comprehensive man page documentation that comes with Perl.

Alex

acid_kewpie 03-10-2003 05:02 AM

sounds a lot like an RTFM to me, JStew! :D

JStew 03-10-2003 06:24 AM

yeah, it is acid-- i really tried to disguise it as best i could but, you know ignorance... hard to conceal it at times :-)

acid_kewpie 03-10-2003 06:26 AM

yeah, that and old age are a bad combination i guess.....

JStew 03-10-2003 06:56 AM

<grumble> well, thanks a lot acid ... you killed the thread! </grumble>


All times are GMT -5. The time now is 04:13 PM.