Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Tutorial on arguments for the new

by Andrew_Levenson (Hermit)
on Dec 11, 2006 at 21:03 UTC ( [id://589150]=perlmeditation: print w/replies, xml ) Need Help??

I don't know how many people this will apply to, but it is certainly something I wish I could have had when starting out:

Arguments: what are they?

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!
C(qw/74 97 104 112/);sub C{while(@_){$c**=$C;print (map{chr($C!=$c?shift:pop)}$_),$C+=@_%2!=1?1:0}}

Replies are listed 'Best First'.
Re: Tutorial on arguments for the new
by Joost (Canon) on Dec 11, 2006 at 21:11 UTC
      I was going to start with "I'd like to register a complaint!" but I realized it was the wrong sketch. We'll save that one for the tutorial on getting started with... that other language.

      And, thanks for pointing that out to me.
      C(qw/74 97 104 112/);sub C{while(@_){$c**=$C;print (map{chr($C!=$c?shift:pop)}$_),$C+=@_%2!=1?1:0}}
      I think he was just trying to explain for others starting out what arguments were and how they were passed to subroutines, I don't think the exact code matters all that much. Even clearer might be:
      my ($bar, $baz) = ("Hello ", "World"); foo($bar, $baz); # ... sub foo{ print "$_[0]"; #prints "Hello " print "$_[1]"; #prints "World"
Re: Tutorial on arguments for the new
by traveler (Parson) on Dec 11, 2006 at 23:58 UTC
    One might also note that arguments are the values that fill parmeters. So if we have
    . . . foo("square", 3); . . . sub foo{ my ($shape, $size) = @_; . . .
    $shape and $size function as parmeters that are filled with the arguments "square" and 3. Now, to be accurate, perl5 does not have true formal parameters as do many other languages including perl6. You can get the perl6 behavior using Perl6::Parameters, however. Anyway, in the general computer science nomenclature, arguments are passed to functions into parameters.

    It seems that the terms are often used to mean the same thing, but that often rankles some language lawyers and other pendantic individuals.

      Duly noted and adjusted.
      C(qw/74 97 104 112/);sub C{while(@_){$c**=$C;print (map{chr($C!=$c?shift:pop)}$_),$C+=@_%2!=1?1:0}}
Re: Tutorial on arguments for the new
by Anonymous Monk on Dec 12, 2006 at 16:32 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://589150]
Approved by Joost
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-04-16 12:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found