Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
For someone entirely new to programming, what an "argument" is, exactly, may not be perfectly clear. I know it wasn't for me. I'd get plenty of responses about how it reads the "command line argument" or it "passes the argument on...," which meant nothing to me, but when I asked for further clarification, I was beyond help because nobody understands that one can not know what an argument is.
For those who, like me, just sadly could not pick up what an argument is and why, here is an abstract, in laymen's terms!

An argument is, essentially, data supplied to the script ( or sub routine, or...) to fulfill parameters that need to be met. Okay, not so easy to explain. Perhaps an example will clarify.

This program is going to print out a letter or word supplied to it, but instead of inputting the word/letter, we're going to supply it as a command line argument.
use strict; #always and forever, amen. use warnings; #or else. my $argument=$ARGV[0]; #@ARGV is an array containing the arguments supplied. We're taking the + first value. print "\n$argument"; #This here will show you exactly what has been put into $argument.

If you run that, nothing happens. That's because you have supplied no arguments, so $argument is empty. Now try running it again, but when you go to open it in the command prompt, follow the program name with a letter or word, as such:
C:>"C:\foo.pl" arg
Now, if all goes well and I didn't just make a fool out of myself, the program should print "arg," because "arg" was supplied as the first command line argument. Make sense?

Additionally (and I may be going out on a limb to assume that anyone else is as dense as I am), have you ever noticed that when you call a subroutine, you call it as foo();, not foo;? That's because inside of those parentheses, you're supposed to supply your arguments for the subroutine (fed into the temporary array, @_)! It's like a mini-script, I guess. Sort of. In a way.
So for this,
$bar='Hello,'; $baz=' world!'; foo($bar, $baz); #calls the foo subroutine with $bar and $baz as arguments sub foo { print for @_; #will first print out the contents of $bar, then $baz because #those are the arguments given. }

it would be read as "Print $bar, then print $baz";

Arguments don't always have to be variables, though. For instance,
foo("square", 3); sub foo{ my ($shape, $size) = @_; }
This sets $shape to "square" and $size to 3, because they are given as the arguments for the subroutine.
I hope that helps!

In reply to (Subroutine) Arguments explained (sort of.) by Andrew_Levenson

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2021-12-06 02:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (31 votes). Check out past polls.

    Notices?