This is a legitimate question for a beginner. But it is quite basic in any introductory book or documentation. In the long run you will get a lot more help here at the monestary if you demonstrate that you have done some work or study on your own. Describe what you have read and where you are stuck. Or better yet, show us some code that illustrates your best attempt to solve your own question.

For subroutines and their arguments, the general idea is this: you can call a sub with values this way: mysub($val1, $val2, etc). Then in the subroutine itself, those values are available in the special array called @_.

Here's some code to show some alternatives for accessing those values:

#!/usr/bin/perl -w use strict; sub mysub { # the args are in @_ my $bigness = $_[0]; # copy one val my ($size, $color, $style) = @_; # or grab them all my $proportions = shift; # or shift; @_ is implicit uc($_[1]); # or use @_ directly # do stuff with $bigness, $size, etc. } mysub('big', 'blue', 'fuzzy');

Because any action on the values in @_ changes their values back where they were called, many people follow the rule of not doing anything with or to the @_ values directly (e.g. $_[2]) unless the purpose of the subroutine is to directly change the parameter in question. Instead they use one of the first three methods above to copy the values and then work with that.

You will find a lot of help for these basic issues here at Perl Monks in the Library and in Tutorials. In particular a quick visit to the Library yields perlman:perlsub on subroutines. (I suggest you ignore material relating to prototypes for now.)

------------------------------------------------------------
"Perl is a mess and that's good because the
problem space is also a mess.
" - Larry Wall


In reply to Re: input variables by dvergin
in thread input variables by dreman

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.