[Linux & perl] Command line arguments

  • Thread starter rama1001
  • Start date
  • Tags
    Line Linux
In summary, the conversation discusses the concept of command line arguments and how they are used in Perl. The person is learning about this topic and is unsure of its purpose. They have implemented a short program in Perl to list the command line arguments, but are not seeing any output when they run the program. The conversation also mentions how command line arguments can be used to create scripts that behave like Unix/Linux commands.
  • #1
rama1001
132
1
Hi,
I am in process of leraning Linux. I have been structed in understanding of command line arguments and @ARGV in perl.
What is command line arguments? Really, i have no idea.
Though, i have implemented a short program in perl to list this command line arguments but i couldn't see any list when i run my program.

#! /usr/bin/perl

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


Can anyone help with basic information what does this command line arguments and how the perl is accessing them?

Thank you.
 
Technology news on Phys.org
  • #2
Suppose you have the script stored in a file named args.pl. Then type this at the command prompt:

./args.pl one two three

What do you see? ("one two three" are the command line arguments to the script)
 
  • #3
Yes, this i understood before itself. What is use of this? if it prints argumetns then what? How it would be useful in practically?
 
  • #4
it's just a demonstration of how to get the command line arguments, without regard to how you're going to use them.

By using command-line arguments, you can write scripts that behave like Unix/Linux command-line commands.
 
  • #5
Type at your command prompt:

ls

ls *.pl

ls -al

It is the same command (list) but in each case output is different - thanks to the command argument.
 

FAQ: [Linux & perl] Command line arguments

What are command line arguments in Linux and Perl?

Command line arguments are parameters or values that are passed to a program or script through the command line when it is executed. These arguments can be used to customize the behavior of the program and provide input data for its execution.

How do I pass command line arguments in Perl?

In Perl, command line arguments can be accessed through the special array @ARGV. This array contains all the arguments passed to the script and can be accessed using index values starting from 0. For example, $ARGV[0] will contain the first argument, $ARGV[1] will contain the second argument, and so on.

Can I pass multiple command line arguments in one line?

Yes, you can pass multiple command line arguments in one line by separating them with a space. For example, perl script.pl arg1 arg2 arg3 will pass three arguments to the script script.pl.

How can I handle command line arguments in Linux?

In Linux, command line arguments can be handled using the getopt function. This function allows you to specify the options and arguments that your program accepts and parses them accordingly. You can also use the getopts module for a more convenient and flexible way of handling command line arguments.

Are there any special command line arguments in Perl?

Yes, there are a few special command line arguments in Perl that can be used for specific purposes. These include -e, which allows you to pass a Perl code snippet as an argument, -n, which runs the code specified with -e on each line of input, and -p, which does the same as -n but also prints the current line after the code is executed.

Similar threads

Replies
10
Views
1K
Replies
1
Views
2K
Replies
12
Views
3K
Replies
2
Views
1K
Replies
17
Views
2K
Replies
12
Views
9K
Replies
11
Views
4K
Replies
3
Views
690
Back
Top