http://www.perlmonks.org?node_id=11104978


in reply to Currency symbols in variable names

Hi, not all characters may be used in variable names, even under UTF-8. See perldata.

Try this:

#!/usr/bin/perl use strict; use warnings; use diagnostics; use utf8; use feature qw/say/; binmode STDOUT, ":utf8"; my $ιlan; say '------------------------';

Hope this helps!


The way forward always starts with a minimal test.

Replies are listed 'Best First'.
Re^2: Currency symbols in variable names
by pme (Monsignor) on Aug 25, 2019 at 11:39 UTC
    Hi 1nickt,

    It seems to be that unicode characters can be in variable names if they are letters and currency symbols are not considered as letters.

    my $愛 = 4; # correct

    my $s = '★'; # correct

    my $★ = 'star'; # incorrect

    However, it makes sense. What could we do with this?

    sub $_to_₯ { ... }
    Update: This one works ...

    my $元 = 3;

      Hi again, I'm not sure whether What could we do with this? refers to the sub after the question, or the way Perl disallows certain characters in variable names. If the latter, I would suggest that independent of your variable naming issue, you consider extensibility of your program. There doesn't seem to be much advantage to a sub named $_to_₯ as compared to a more generic one that takes arguments:

      use strict; use warnings; use Test::More; use Test::More::UTF8; # loads utf8.pm sub convert_currency { my %map = ( '$₯' => sub { $_[0] * 105.4 }, '₯$' => sub { $_[0] * 0.0095 }, '$€' => sub { $_[0] * 0.89 }, '€$' => sub { $_[0] * 1.12 }, ); my ($from, $to, $amount) = @_; my $converter = $map{"$from$to"}; return sprintf( '%.3f', $converter->($amount) ); } is( convert_currency('$', '₯', 5), '527.000', '$ -> ₯' ); is( convert_currency('€', '$', 42.02), '47.062', '€ -> $' ); is( convert_currency('$', '€', 42.02), '37.398', '$ -> €' ); done_testing;
      $ prove -v monks/11104981.pl monks/11104981.pl .. ok 1 - $ -> ₯ ok 2 - € -> $ ok 3 - $ -> € 1..3 ok All tests successful. Files=1, Tests=3, 0 wallclock secs ( 0.02 usr 0.00 sys + 0.04 cusr + 0.00 csys = 0.06 CPU) Result: PASS

      Hope this helps!


      The way forward always starts with a minimal test.
        I wanted to say that currency symbol are not allowed in names because $ is a sigil too.