MrSparks has asked for the wisdom of the Perl Monks concerning the following question:

#!/usr/bin/perl 2 3 use strict; 4 use warnings; 5 use diagnostics; 6 7 my %nationality_of = ( 8 'Ovid' => 'Greek', 9 'John Davidson' => 'Scottish', 10 'Tennyson' => 'English', 11 'Poe' => 'Tracky', # Geek? 12 ); 13 14 my @nationalities = @nationality_of( 'Ovid', 'Tennyson' ); 15 print "@nationalities"; 16

This piece of simple code is not compiling for me. This is the error:
Possible unintended interpolation of @nationalities in string at test line 15 (#1)
2 (W ambiguous) You said something like '@foo' in a double-quoted string
3 but there was no array @foo in scope at the time. If you wanted a
4 literal @foo, then write it as \@foo; otherwise find out what happened
5 to the array you apparently lost track of.
6
7 Global symbol "@nationality_of" requires explicit package name at test line 14.
8 syntax error at test line 14, near "@nationality_of( "
9 Global symbol "@nationalities" requires explicit package name at test line 15.
10 Execution of test aborted due to compilation errors (#2)
11 (F) You've said "use strict" or "use strict vars", which indicates
12 that all variables must either be lexically scoped (using "my" or "state"),
13 declared beforehand using "our", or explicitly qualified to say
14 which package the global variable is in (using "::").
15
16 Uncaught exception from user code:
17 Global symbol "@nationality_of" requires explicit package name at test line 14.
18 syntax error at test line 14, near "@nationality_of( "
19 Global symbol "@nationalities" requires explicit package name at test line 15.
20 Execution of test aborted due to compilation errors.
~

Any explanations please, I cannot understand why in the book @nationalities is in double quotes. Thank you.

THANK YOU FOR THE REPLIES.

Replies are listed 'Best First'.
Re: Problem with hash slice.
by Athanasius (Archbishop) on Feb 21, 2015 at 06:08 UTC

    Hello MrSparks,

    To make a hash slice, you need to use the braces (curly brackets) that normally go with hashes, not parentheses:

    #! perl use strict; use warnings; use diagnostics; my %nationality_of = ( 'Ovid' => 'Greek', 'John Davidson' => 'Scottish', 'Tennyson' => 'English', 'Poe' => 'Tracky', # Geek? ); my @nationalities = @nationality_of{ 'Ovid', 'Tennyson' }; # <-- Ch +ange parentheses to braces here! print "@nationalities";

    Output:

    16:03 >perl 1162_SoPW.pl Greek English 16:03 >

    @nationalities is in double quotes to force the behaviour provided by double-quote interpolation. See, for example, the entry for the $LIST_SEPARATOR variable (usually spelled $") in perlvar.1

    P.S. Please don’t include line numbers when posting code.2

    Updates:
    1 With $" set to a space (the default), print "@nationalities"; is equivalent to print join(' ', @nationalities);.
    2 This makes it harder for monks to copy-and-paste the code and run it. If you want to see line numbers on code displayed in PerlMonks, add &001;: to the “Code Prefix” box in the “Code Listing Settings” section of your Display Settings.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: Problem with hash slice.
by roboticus (Chancellor) on Feb 21, 2015 at 06:10 UTC

    MrSparks:

    You almost had it--your mistake was using parentheses. For an array slice, you use brackets:

    my @array = (0, 1, 2, 3, 4, 5); my @temp = @array[1..3];

    For hash slices, you use braces:

    my @nationalities = @nationality_of{'Ovid', 'Tennyson'};

    The first message is a warning, but you can ignore it, because it'll go away once you fix the previous error. (The line that declared the array has a syntax error, so @nationalities never got properly declared.)

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.