in reply to Re^5: Program unsuspectingly dies with no reason why.
in thread Program unsuspectingly dies with no reason why. -FIXED!

Updated code
if ($remove) { $remove = lc($remove); my %remove_cmd = ( remove => "rmquedev -d $remove -q $remove" ); print "Are you sure you want to remove print queue \"$remove\" [Y/ +n]?"; $_ = <>; chomp($_); $_ = "Y" if ( length($_) == 0 ); if ( $_ =~ /[Yy]/ ) { # Wait for user-input system( $remove_cmd {'remove'} ); print "Prepairing to remove \"$remove\" from the CC_print.printers file.\n"; sleep 2; my $removeq = $remove; &cleanthefile($removeq); } else { die "You chose NOT to remove queue \"$remove\"\n"; } }

Sub-Process
sub cleanthefile { my $sourcefile = "/u/ccsys/CC_print.printers"; print $removeq; print "$sourcefile\n"; # Keep a dedicated backup of CC_print.printers if ( -t ne "/root/CC_print.printers.bkp" ) { system("touch /root/CC_print.printers.bkp"); sleep 2; system("cp -R $sourcefile /root/CC_print.printer.bkp"); } if ( -t eq "/root/CC_print.printers.bkp" ) { system("cp -R $sourcefile /root/CC_print.printer.bkp"); # grab the sourcefile and the string to remove as the first and second + arguments my ( $sourcefile, $removeq )= @_; my $tempfile = "/u/ccsys/CC_print.printers.bkp_mkpq"; # create a temporary file if (-e ne "$tempfile") { system("touch /u/ccsys/CC_print.printers.bkp_mkpq"); } # extract lines from $sourcefile which do NOT have the string to + remove # note that the \' inserts ticks so that strings with spaces can + work, too if ( -t "$tempfile") { #system ("cat $sourcefile \| grep -v $removeq $sourcefile > $t +empfile"); #`grep -v $removeq $sourcefile /dev/null /dev/null > $tempfile +`; sleep 1; print "Waiting to unlink..\n"; # delete source file, then rename working file unlink($sourcefile); rename( $tempfile, $sourcefile ); system("rm -rf $tempfile"); } } }

I placed a print statment for $removeq to me it seems as if $removeq is not being passed correctly. I'm rather new to Perl, I wouldn't doubt this is just product of my inexperience with programming in general. No matter how much I read I can't seem to find a solution to this issue!

Replies are listed 'Best First'.
Re^7: Program unsuspectingly dies with no reason why.
by kyle (Abbot) on Feb 15, 2008 at 17:24 UTC

    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.

      Call to sub-routine
      my $removeq = $remove; my $sourcefile = "/u/ccsys/CC_print.printers"; my $tempfile = "/u/ccsys/CC_print.printers.bkp_mkpq"; &cleanthefile( $removeq, $sourcefile, $tempfile );

      sub cleanthefile { my @filehandles; my ( $sourcefile, $removeq, $tempfile ) = @filehandles; # Keep a dedicated backup of CC_print.printers if (-e "/root/CC_print.printers.bkp" ) { `cp -R $filehandles[1] $filehandles[2]`; # create a temporary file if (-T ne "$filehandles[2]") { system("touch /u/ccsys/CC_print.printers.tmp"); } # extract lines from $sourcefile which do NOT have the string to + remove # note that the \' inserts ticks so that strings with spaces can + work, too if ( -T "$filehandles[2]") { # system ("cat $sourcefile \| grep -v $removeq $sourcefile > $ +tempfile"); `grep -v $filehandles[0] $filehandles[1] /dev/null /dev/null > + $filehandles[2]`; sleep 1; print "Waiting to unlink..\n"; # delete source file, then rename working file unlink($filehandles[1]); rename( $filehandles[2], $filehandles[1] ); system("rm -rf $filehandles[2]"); } } else { system("touch /u/ccsys/CC_print.printers.bkp"); system("cp -R $filehandles[1] /root/CC_print.printer.bkp"); print "Please re-run the program, had to create a backup\n"; } }
      I think you've got me on the right path Kyle, I can actually print the array of elements "@filehandles". It comes out in a order queuename, sourcefile, tempfile. Thanks so much for the help!