develooper Front page | perl.perl5.porters | Postings from August 2017

Misspelled carrot variables

From:
Abigail
Date:
August 23, 2017 10:25
Subject:
Misspelled carrot variables
Message ID:
20170823102726.GA2264@almanda.fritz.box

If you use strict and warnings, and you typo a lexical variable name,
you get a compilation error:

    use 5.26.0;
    use strict;
    use warnings;

    my $variable = 1;
    say $vriable;
    __END__
    Global symbol "$vriable" requires explicit package name (did you forget to declare "my $vriable"?) at /tmp/meep line 6.
    Execution of /tmp/meep aborted due to compilation errors.


Even if you don't use strict, and use package variable, Perl warns if
you typo your variable name:

    use 5.26.0;
    no strict;
    use warnings;

    use vars qw [$variable];
    say ${vriable};
    __END__
    Name "main::vriable" used only once: possible typo at /tmp/meep line 6.
    Use of uninitialized value $vriable in say at /tmp/meep line 6.


But all safety is off when using variable names starting with a carrot:

    use 5.26.0;
    use strict;
    use warnings;

    "foo" =~ /(f)(o)(o)/ or die "No match";

    say $_ for @{^CAPTURES};    # Instead of @{^CAPTURE}.
    __END__


No errors, no warnings.


Since we seem to be getting more such variables, this is unfortunate.



Abigail



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About