in reply to Printing First n Characters of a Scalar

Answers already abound so if it were for homework I'd be tempted to submit something like the following. That said I once put this in an assignment:
printf("Segmentation fault\n"); sleep(4); printf("just kidding. it's all good.\n");
My TA didn't find it as funny as I did. When she was a younger student and working late (ie 2 AM) someone put a similar printf() in someone else's assignment while the person went out for a cigarette. That wasn't so funny either. Now back to the perl question:
sub first_n_chars($$) { my $N = int shift; my $input = shift; my $output = ""; for(;$N > 0; $N--) { $input =~ m/(.)/; $output .= $1; } return $output; } sub first_n_chars_2($$) { my $N = int shift; my $input = shift; my $dots = ""; for(;$N > 0; $N--) { $dots .= '.'; } my $output = ""; $input =~ m/($dots)/; $output .= $1; return $output; } sub first_n_chars_3($$) { my $N = int shift; my $input = shift; my $output = ""; $input =~ m/(.{$N})/; $output = $1; return $output; } my $foo = "314159"; print "$foo\n"; print first_n_chars(1, $foo) , "\n"; print first_n_chars_2(2, $foo) , "\n"; print first_n_chars_3(3, $foo) , "\n";

Update: Changed " s/$BLAH//; " to " m/$BLAH/; "