Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing Arugments into Perl Script!?

Status
Not open for further replies.

jonx

Programmer
Sep 2, 2003
34
0
0
US
Anyone know a way to pass arguments into an actual script? not a subroutine... for example

ex: perl -w script <arguments>

I wanted to create a script to upload a file, where the <arguments> is the filename to upload. Anyone know how?

After I get this, I could just run it in my bash script...

perl -w upload.pl filename1
perl -w upload.pl filename2
perl -w upload.pl filename3

=) Anyway to do this? in C you could do this using the
int main(int argc, char *argv) any equivalent in perl?
 
no problem

it's a builtin array @ARGV

$arg1 = shift;
$arg2 = shift;

will let you pull args off the command line, as will

$arg1 = $ARGV[0];
$arg2 = $ARGV[1];


Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
Note: You can also get the arguments out using the <> operator. From &quot;Learning Perl&quot;:

Another way to read input is with the diamond operator: <>. This works like <STDIN> in that it returns a single line in a scalar context (undef if all the lines have been read) or all remaining lines if used in a list context. However, unlike <STDIN>, the diamond operator gets its data from the file or files specified on the command line that invoked the Perl program. For example, you have a program named kitty, consisting of

#!/usr/bin/perl
while (<>) {
print $_;
}
and you invoke kitty with

kitty file1 file2 file3
then the diamond operator reads each line of file1 followed by each line of file2 and file3 in turn, returning undef only when all of the lines have been read. As you can see, kitty works a little like the UNIX command cat, sending all the lines of the named files to standard output in sequence. If, like cat, you don't specify any filenames on the command line, the diamond operator reads from standard input automatically.


Technically, the diamond operator isn't looking literally at the command-line arguments; it works from the @ARGV array. This array is a special array initialized by the Perl interpreter to the command-line arguments. Each command-line argument goes into a separate element of the @ARGV array. You can interpret this list any way you want.[2] You can even set this array within your program and have the diamond operator work on that new list rather than the command-line arguments, like so:

@ARGV = (&quot;aaa&quot;,&quot;bbb&quot;,&quot;ccc&quot;);
while (<>) { # process files aaa, bbb, and ccc
print &quot;this line is: $_&quot;;

 
Thanks for the help guys... =)

Would any of you know how to download a file that requires a username and password? Its a https site...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top