http://qs1969.pair.com?node_id=11137939

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

The perl guide document "perlpacktut" contain the following command statements, which look like assignments.

$UTF8{Euro} = pack( 'U', 0x20AC ); # Equivalent to: $UTF8{Euro} = "\x{20ac}"; $Unicode{Euro} = unpack( 'U', $UTF8{Euro} );

The tokens `$UTF8{Euro}` and `$Unicode{Euro}` seem to be treated as variables. However, the typical perl variables I am familiar with are of the form $v or ${v} but not $v{u}. In fact, the following code ends up with an error.

#!/usr/bin/perl my $v{u} = "hello"; print($v{u}, "\n");

syntax error at ./test.pl line 3, near "$v{u"

What are `$UTF8{Euro}` and `$Unicode{Euro}`? Which perl guide document explain this kind of token?

Thank you in advance.

Replies are listed 'Best First'.
Re: Variable with curly braces?
by Athanasius (Archbishop) on Oct 24, 2021 at 06:38 UTC

    Hello wyt248er, and welcome to the Monastery!

    The syntax $UTF8{Euro} = ...; is indeed a variable assignment, but the variable assigned to in this case is a member of a hash. You can see this yourself in two ways:

    # Method 1: With an explicit declaration use strict; use Data::Dump; my %v; # Hash declaration $v{u} = "hello"; print $v{u}, "\n"; dd %v; # Show the contents of the hash
    # Method 2: Without a declaration # No "use strict" here! use Data::Dump; $v{u} = "hello"; print $v{u}, "\n"; dd %v; # Show the contents of the hash

    The first method declares the hash %v using the my keyword, as mandated by the use strict; pragma. This is good practice. The second method just uses the hash without first declaring it. This will work only if the use strict; pragma is not in effect. This is considered bad style in modern Perl.

    Bear in mind that the Perl documentation has been around for some time, and may pre-date modern best practice. In any case, the code examples in the documentation are snippets only, not complete scripts.

    Update: Added 2 code comments

    Hope that helps,

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

Re: Variable with curly braces? (updated)
by AnomalousMonk (Archbishop) on Oct 24, 2021 at 08:24 UTC

    Further to Athanasius's reply:

    The second method just uses the hash without first declaring it. This will work only if the use strict; pragma is not in effect. This is considered bad style in modern Perl.
    wyt248er: The second method works (without strictures) because it will autovivify a named package-global variable of the correct type if it does not already exist. Package-globals are... well, global, and best practice suggests avoiding their use without a good and well-understood reason. See perldata for more examples of the use of hashes. (Update: And see the Perl Data Structures Cookbook for lots more on hash/array structures.)


    Give a man a fish:  <%-{-{-{-<

Re: Variable with curly braces?
by Bod (Parson) on Oct 24, 2021 at 22:14 UTC
    syntax error at ./test.pl line 3, near "$v{u"

    The reason the this is a syntax error is that you cannot declare one element of an array or hash with my.

    Athanasius has already given you great examples of working code.

    To add...Perl has three basic data types: scalars, arrays and hashes:
    scalars hold a single value and are of the form $scalar
    arrays hold an ordered list of values and are accessed individually as $array[n] where n is the index in the list. They are accessed as a whole list using @array
    finally hashes are unordered lists accessed by a key. Individually they are $hash{key} and as a complete hash with %hash.