Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (8)
    As of 2014-07-28 06:16 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (189 votes), past polls