Friday, January 27, 2012

Perl keyword 'our' doesn't work on old Perl

If you need to declare a variable in old (pre 5.6?) Perl, and can't use the 'my' keyword...


#!/usr/bin/perl -w
#
#
#
use strict;
use Getopt::Std;

our $opt_v;
getopts('v');
sub VERBOSE { $opt_v; }


This will fail:


$ ./try.pl
Use of reserved word "our" is deprecated at ./try.pl line 8.
Global symbol "$opt_v" requires explicit package name at ./try.pl line 8.
Execution of ./try.pl aborted due to compilation errors.


You can use this instead:


#!/usr/bin/perl -w
#
#
#
use strict;
use Getopt::Std;

use vars '$opt_v';
getopts('v');
sub VERBOSE { $opt_v; }


There is a discussion on what the error means at http://www.perlmonks.org/?node_id=23916