Strange behaviour due to missing semi colon

N

Niall Macpherson

I have noticed some strange behaviour if I accidently miss out the line
terminating semi-colon after a use directive . Here is some sample code
(Active Perl 5.8.7)

use strict;
use warnings;
use Data::Dumper;

my ($foo, $bar) = (2,3);
my $foobar = $foo + $bar;

print "\nfoo = [$foo], bar = [$bar], foobar = [$foobar]\n";
exit(0);

This produces the expected output

H:\Perl Scripts>semi_colon.pl

foo = [2], bar = [3], foobar = [5]

H:\Perl Scripts>

However if I remove the terminating semi-colon on line 3 - i.e

use Data::Dumper

instead of getting a syntax error (as I would expect) , I get the
following

H:\Perl Scripts>semi_colon.pl
Data::Dumper version 3 required--this is only version 2.121_04 at
C:/Perl/lib/Ex
porter/Heavy.pm line 121.
BEGIN failed--compilation aborted at H:\Perl Scripts\semi_colon.pl line
5.

H:\Perl Scripts>

Can someone please explain ?

Thanks
 
N

Niall Macpherson

Niall Macpherson wrote:

instead of getting a syntax error (as I would expect) , I get the
following


OK - I have now just realised that I wouldn't necessarily get a syntax
error since presumably use is interpreting the code on line 5 as either
the VERSION or a LIST as in the form

use Module LIST
use Module VERSION
or
use Module VERSION LIST

However I still don't understand why 'version 3 required' is being
displayed as the error ?
 
P

Paul Lalli

Niall said:
I have noticed some strange behaviour if I accidently miss out the line
terminating semi-colon after a use directive . Here is some sample code
(Active Perl 5.8.7)

use strict;
use warnings;
use Data::Dumper;

my ($foo, $bar) = (2,3);
my $foobar = $foo + $bar;

print "\nfoo = [$foo], bar = [$bar], foobar = [$foobar]\n";
exit(0);

This produces the expected output

H:\Perl Scripts>semi_colon.pl

foo = [2], bar = [3], foobar = [5]

H:\Perl Scripts>

However if I remove the terminating semi-colon on line 3 - i.e

use Data::Dumper

instead of getting a syntax error (as I would expect) , I get the
following

H:\Perl Scripts>semi_colon.pl
Data::Dumper version 3 required--this is only version 2.121_04 at
C:/Perl/lib/Ex
porter/Heavy.pm line 121.
BEGIN failed--compilation aborted at H:\Perl Scripts\semi_colon.pl line
5.

H:\Perl Scripts>

Can someone please explain ?

perldoc -f use

use takes an optional second parameter that specifies the minimum
version number of the module to require.

use Foo 5;

means that at least version 5 of Foo.pm must be available, otherwise
exit with an error. When you left off your semicolon, the next
expression was taken to be a part of the prior statement, because Perl
doesn't care about whitespace. That expression, the assignment of 2
and 3, returns the value 3. That value is taken to be the second
argument to your use Data::Dumper statement.

Paul Lalli
 
N

Niall Macpherson

Paul said:
That expression, the assignment of 2
and 3, returns the value 3. That value is taken to be the second
argument to your use Data::Dumper statement.

Paul Lalli

I half got there on my own but you have now explained it fully !
Thanks Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top