When you call a sub with parameters, those parameters arrive at the sub in the special array, @_. The usual practice is to copy the elements of that array immediately upon entering the sub.

sub foo { my ( $x, $y ) = @_; # ... }

Sometimes that's done this way:

sub foo { my $x = shift; my $y = shift; # ... }

In that case, rather than just copying, shift is actually pulling items off the array (which are then copied).

In each of these, you can call foo( 'hello', 'world' ), and $x will take the value 'hello' while $y takes the value 'world'. If you call foo( $howdy, $doody ), the variables $howdy and $doody won't be visible inside foo, but their values will be in $x and $y.

In your particular case, you're trying to print $removeq immediately upon entering the sub without grabbing the arguments first. The cleanthefile sub doesn't see the $removeq of the caller directly. It just gets a copy via the @_ array. (The fact that it doesn't have a variable by that name is what the "Global symbol "$removeq" requires explicit package name" error is about.)

It actually does take a copy later, on this line:

my ( $sourcefile, $removeq )= @_;

...but you're trying to print before that.

What I'd recommend, to start, is to get rid of that line, and put this at the very start of the sub:

my ( $removeq ) = @_;

At that point, I think the problem you may have is that $sourcefile doesn't have a value. I don't see where that value might come from, so I don't know how to advise you on how to fix that.

If you're interested in the gory details of sub calls in Perl, see perlsub.


In reply to Re^7: Program unsuspectingly dies with no reason why. by kyle
in thread Program unsuspectingly dies with no reason why. -FIXED! by misconfiguration

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.