6

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!

Richard
  • 316
  • 3
  • 12

3 Answers3

11

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.

friedo
  • 65,762
  • 16
  • 114
  • 184
  • 5
    I'd prefer `use 5.012` to `use feature ':5.12'` because it gives a more descriptive error message if you try to run it on a too-old perl. – hobbs Jun 27 '13 at 03:10
4

You need to use feature qw (say);

The documentation for say.

squiguy
  • 32,370
  • 6
  • 56
  • 63
  • Also might be useful to look at [**`use`**](http://perldoc.perl.org/functions/use.html) and research perl modules and how to use them. – Ryaminal Jun 27 '13 at 03:04
1

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';

arafeandur
  • 196
  • 1
  • 5