Showing posts with label old_perl. Show all posts
Showing posts with label old_perl. Show all posts

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

Thursday, December 03, 2009

Lexical filehandles on old (pre 5.6) Perl...

I was looking into this because of wanting to use lexical filehandles in pre 5.6 Perl.

http://www.perl.com/doc/manual/html/pod/perldata.html#Typeglobs_and_Filehandles

http://perl.plover.com/local.html#2_Localized_Filehandles

http://stackoverflow.com/questions/613906/why-does-programming-perl-use-local-not-my-for-filehandles

http://www.perlmonks.org/?node_id=305217


#!/usr/bin/perl -w
#
#
#
use strict;

my $file = 'file.txt';

local *FH;

# my $file_fh = do { *FH };
my $file_fh = \*FH;

open ($file_fh, "> $file") || die "Can't write to '$file': $!\n";

my $line = "This is some text";

print $file_fh "$line\n";

close $file_fh;

Thursday, February 17, 2005

Need to update all of the CPAN modules on a server?

If you are using the CPAN module within Perl, you can update all of the installed modules on your system using the command:


perl -MCPAN -e 'CPAN::Shell->install(CPAN::Shell->r)'


This forces CPAN to produce a list of all of the outdated modules on the machine and install them in one hit...

Not recommended for your average joe. you'll have to go over all of your module dependecies and check to see if updates break any of your live code.. still though there are instances where this can be useful.

Something else to think about... have a look here:


lrwxrwxrwx 1 root root 5 Feb 5 21:36 /usr/lib/perl/5.8 -> 5.8.4
drwxr-xr-x 5 root root 4096 Jan 30 2004 /usr/lib/perl/5.8.2
drwxr-xr-x 4 root root 4096 May 4 2004 /usr/lib/perl/5.8.3
drwxr-xr-x 30 root root 4096 Feb 17 18:05 /usr/lib/perl/5.8.4
drwxrwsr-x 9 root staff 4096 Mar 7 2002 /usr/local/lib/perl/5.6.1
drwxrwsr-x 21 root staff 4096 Dec 5 2003 /usr/local/lib/perl/5.8.0
drwxrwsr-x 4 root staff 4096 Oct 30 2003 /usr/local/lib/perl/5.8.1
drwxrwsr-x 11 root staff 4096 Mar 25 2004 /usr/local/lib/perl/5.8.2
drwxrwsr-x 12 root staff 4096 Jan 23 10:59 /usr/local/lib/perl/5.8.3
drwxrwsr-x 27 root staff 4096 Feb 17 18:08 /usr/local/lib/perl/5.8.4


On a box where Perl has been upgraded a few times there will be lots of old dirs with modules in...