JaeDre619:

When you call a subroutine, you typically want to give it some data to work on. In your case, we give it the name of the file to process. When you call the subroutine, you can give it a list of values, so your subroutine needs a way to access them.

The subroutine gets an array @_ which contains the argument list. While you can use the arguments in the @_ array directly, it can cause you difficulties[1]. Instead, I like to copy the values into local variables, which is what I'm doing in the line you asked about. On the left, I put the list of local variables I want, so the first argument goes into the first variable, the second goes into the next variable, and so on. If any of your local variables is an array, it will consume all remaining values in the argument list--so be aware of it.

$ cat test3.pl #!/usr/bin/perl use strict; use warnings; mysub(1); mysub(2,3,4); mysub(5,6,7,8,9,0); sub mysub { my ($first, $second, @third, @fourth)=@_; print "First: $first, Second: $second, Third: @third, Fourth: @fou +rth.\n"; } $ perl test3.pl Use of uninitialized value $second in concatenation (.) or string at t +est3.pl line 11. First: 1, Second: , Third: , Fourth: . First: 2, Second: 3, Third: 4, Fourth: . First: 5, Second: 6, Third: 7 8 9 0, Fourth: .

Notes:

[1] The difficulty is that @_ contains aliases to the calling values, so if you use the values in the argument list directly, you risk changing the values in the caller's point of view. That can be useful at times, but it can be a pernicious bug:

$ cat test4.pl #!/usr/bin/perl use strict; use warnings; sub noalias { my ($arg)=@_; $arg = uc($arg); print "Arg: $arg.\n"; } sub alias { $_[0] = uc($_[0]); print "Arg: $_[0].\n"; } my $t1 = "foobar"; my $t2 = "barbaz"; noalias($t1); alias($t2); print "T1: $t1, T2: $t2.\n"; $ perl test4.pl Arg: FOOBAR. Arg: BARBAZ. T1: foobar, T2: BARBAZ.

[2] You'll probably want to read perlsub for all the details of how subroutines work. When you do, though, skip the section on prototypes. It turns out that (a) prototypes don't work the way you'd expect them to, (b) beginners should avoid using them as much as possible, and (c) they can be a source of problems. In fact, I don't think I've ever used them.

Sorry for the delay in replying, but I was watching the last few episodes of Azumanga Daioh, and just couldn't pry myself away. ;^)

...roboticus

When your only tool is a hammer, all problems look like your thumb.


In reply to Re^3: help merging perl code routines together for file processing by roboticus
in thread help merging perl code routines together for file processing by JaeDre619

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.