Hi all,

First post... I'm a member of maine.pm, too, but we're a quiet bunch and I have a noisy problem. So, here goes...

The problem is that one of my CGI scripts dies on older versions of Perl with a "not a CODE reference", though it runs fine on newer versions. (The cut-off is somewhere between 5.6.1 and 5.8.1). I watered down the production code and ran a test version on different servers (see below). To sum it up, this line works on the new and not on the old, and I'm wondering if there is a "low-profile" workaround:

my ($process, $target) = wantarray? (\&push_array, [EMAIL PROTECTED]): (\&concat, \$record);

------------------------------------------------------------------

#!/usr/bin/perl

use strict;

my @words = qw(foo bar baz bark bleak);
my ($string, @array);

print "Content-type: text/plain\n\n" if $ENV{'GATEWAY_INTERFACE'};
print "You are using Perl version ";
printf "%vd", $^V;
print "\n\n";

eval{$string = manage(@words)};
print "Eval error in scalar assignment: [EMAIL PROTECTED]" if $@;
print "System error in scalar assignment: $!\n" if $!;

[EMAIL PROTECTED] = manage(@words)};
print "Eval error in array assignment: [EMAIL PROTECTED]" if $@;
print "System error in array assignment: $!\n" if $!;

print "STRING:\n", $string, "\n";
print "ARRAY:\n", join("\n", @array), "\n";

sub manage {
my @list = @_;
my (@records, $record);
my ($process, $target) = wantarray? (\&push_array, [EMAIL PROTECTED]): (\&concat, \$record);
for my $item (@list) {
&$process($target, $item);
}
return wantarray? @records: $record;
}


sub concat {
        # target, new content
        ${$_[0]} .= $_[1] . "\n";
}

sub push_array {
        # target, new content
        push @{$_[0]}, $_[1];
}

------------------------------------------------------------------

This is the output on 5.8.1 and 5.8.3:

You are using Perl version 5.8.3

STRING:
foo
bar
baz
bark
bleak

ARRAY:
foo
bar
baz
bark
bleak

------------------------------------------------------------------

This is the output on 5.6.1:

You are using Perl version 5.6.1

Eval error in scalar assignment: Not a CODE reference at code_ref_test.pl line 29.

STRING:

ARRAY:
foo
bar
baz
bark
bleak

------------------------------------------------------------------

I admit the test code shows a convoluted way of achieving not much, but it's reasonably close to the production version. Thanks for your time and advice.

Bogart Salzberg
http://inkfist.com
Portland, Maine

_______________________________________________
Boston-pm mailing list
[EMAIL PROTECTED]
http://mail.pm.org/mailman/listinfo/boston-pm

Reply via email to