[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [Fun With Perl] Puzzler (should this work?)



At 17:34 -0400 06/12/1999, Mark-Jason Dominus wrote:
>
>That's really excellent.  It seems to me that the author thinks
>they've passed the user's input as the argument to read_id_line.  But
>actually they've passed it via the global $_ variable.  Right?

My answer (fyi)

The code was written with the assumption that

    shift;
 
inside a subroutine and with no lhs will shift out of @_ (true)
and into $_ (nope)

It worked (as Ronald pointed out) because it's setting $_ before the subroutine
the subroutine takes the argument passed (coincidentally $_) and shifts it
into space; then makes a comparison (coincidentally :-) to $_ (a global)
*independently of what was passed*

so the previous example above works but the one below doesn't work 
because $_ isn't set.

print "entry: ";
START: while($line = <STDIN>){
	chomp($line);
	$id = read_id_line($line);
	print "test id = $id\n";
     print "entry: ";
}

sub read_id_line() {
	shift;
	# >gi|319118|gb|L18858|L18858 MUSM1537RE Mouse
	if (/^>gi\|([0-9]+)[ |][.\n]*/) { return $1 }

	# >5H1A_HUMAN/53-400
	 if (/^>([0-9A-Z_]+)\//) { return $1 }

	# >Contig[0001], 825 bases, 8988 checksum.
	if (/^>Contig\[(\d+)\], /) {
		 $val = "contig$1";
		 return $val;
	}
	/^>(\w+)/;
	return $1;		#generic
} #read_id_line

-- --
        |\      _,,,---,,_       Vicki Brown <vlb@cfcl.com>
  ZZZzz /,`.-'`'    -.  ;-;;,_   Journeyman Sourceror: Scripts & Philtres
       |,4-  ) )-,_. ,\ (  `'-'  P.O. Box 1269  San Bruno  CA  94066
      '---''(_/--'  `-'\_) http://www.cfcl.com/~vlb  http://www.macperl.org

==== Want to unsubscribe from this list? (Don't you love us anymore?)
==== Well, if you insist... Send mail with body "unsubscribe" to
==== fwp-request@technofile.org

</x-flowed>