SPUG: Something to loose sleep over

Chris Wilkes cwilkes at singingfish.com
Fri Apr 28 12:26:02 CDT 2000


Think that this line will give you what you want in terms of printing:
	print "array = '@{shift()}' @_\n";

As for the weirdness you're seeing -- are you using strict? :)

This works on both 5.6 and 5.005_03

Here's the code.  It works with and without the $x = shift lines.

#!/usr/bin/perl

@A = (1..3);
@B = (4..6);
@C = (7..9);

asubroutine(\@A,"B","C");
asubroutine(\@B,"E","F");
asubroutine(\@C,"Y","Z");

sub asubroutine {
  print "array = '@{shift()}' @_\n";
}

Results:
array = '1 2 3' B C
array = '4 5 6' E F
array = '7 8 9' Y Z


On Fri, 28 Apr 2000, David Waring wrote:

> Warning this is a demonstration of a bug that only appears in certain 
> installations of Perl. It is a weird bug and I am really pointing it 
> out as a topic of conversation. Ignore this if you are not interested 
> in really yucky stuff.
> 
> 
> We have recently had a problem with passing parameters to a 
> subroutine. It only appears on one of our many boxes that are all 
> running the same version of Perl (can't remember the version but it 
> is pre 5.6) All boxes are running AIX.
> 
> 
> To paraphrase the code as closely as possible
> 
> 
> .....
> # call a subroutine several times with different parameters
> 
> asubroutine(\@A,"B","C");
> asubroutine(\@D,"E","F");
> asubroutine(\@X,"Y","Z");
> 
> 
> sub asubroutine{
> 	print @_,\n";
> 	$x= shift @_;
> 	$y= shift @_;
> 	$z= shift @_;
> }
> 
> RESULTS
> ABC
> ABC
> ABC
> 
> whoa thats weird the subroutine is acting like it was called three 
> times with the same parameters
> 
> Now we can fix this in several ways. All of the following changes 
> behaved properly an had the expected results of
> ABC
> DEF
> XYZ
> 
> 1) get rid of the multiple shifts and instead grab all the incoming 
> params at once as any normal person would do
> 
> ($x,$y,$z) = @_;
> 
> 2) forget about $z and only shift in the x y values leaving @_ with a 
> single value
> 
> 3) stop passing the reference in the params just pass the 'normal' scalars;
> 
> 4) removing all the shifts and just printing @_
> I mean how on earth can what is done later with @_ affect the values 
> in it?????????
> 
> Doing #1 but then setting @_ = (); -- which should be pretty damn 
> close to shifting out all the values from @_ worked normally.
> 
> The Print statement before the shifts was not important. The wrong 
> values were being set for x y z anyway.
> 
> 
> 
> 
> We will be reinstalling Perl on this box but does anyone have any 
> idea what in the deepest bowels of Perl could be going on here????
> 
> David Waring
> 
> 
> 
> 
>  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
>      POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
>  Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
>  SUBSCRIBE/UNSUBSCRIBE: Replace "action" below by subscribe or unsubscribe
>            Email to majordomo at pm.org: "action" spug-list your_address
> 
> 
> 


 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
 Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
 SUBSCRIBE/UNSUBSCRIBE: Replace "action" below by subscribe or unsubscribe
           Email to majordomo at pm.org: "action" spug-list your_address





More information about the spug-list mailing list