Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl: Can't call method "say" without a package or object reference

Im trying to learn Perl, am using OS-X 10.8.4 and have Perl version:

This is perl 5, version 12, subversion 4 (v5.12.4) built for darwin-thread-multi-2level

I am trying to run this code:

#!/usr/bin/env perl

use strict;
use warnings;


my $a = 1;
my $b = 1;

say $a + $b ;

And I am getting this:

Can't call method "say" without a package or object reference at test2.pl line 10.

Thanks!

like image 606
Richard Avatar asked Jun 27 '13 03:06

Richard


3 Answers

say is a new feature, added in Perl 5.10. In order to not break old code, it's not available by default. To enable it, you can do

use feature 'say';

But it's probably better to do

use feature ':5.12';

which will turn on all new features available in Perl 5.12 (the version you're running). That includes the say, state, switch, unicode_strings and array_base features.

See the feature documentation for what each of those does.

like image 74
friedo Avatar answered Oct 20 '22 02:10

friedo


You need to use feature qw (say);

The documentation for say.

like image 25
squiguy Avatar answered Oct 20 '22 03:10

squiguy


Modern::Perl is a great package on CPAN that turns on functions in modern versions of perl as well as pragmas like warn and strict that (imho) all perl programmers should use. All my programs start this way now:

use Modern::Perl '2013';

like image 1
arafeandur Avatar answered Oct 20 '22 01:10

arafeandur