Longest Common Prefix in perl

I can’t help finding this slightly elegant. Unfortunately I no longer remember where the idea came from.

use strict;
use warnings;
sub longest_common_prefix {
    my $prefix = shift;
    for (@_) {
	chop $prefix while (! /^$prefix/);
    return $prefix;
print longest_common_prefix(@ARGV), "

One response to “Longest Common Prefix in perl”

Leave a Reply

Your email address will not be published.