Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Ovid (2709)

  (email not shown publicly)
AOL IM: ovidperl (Add Buddy, Send Message)

Stuff with the Perl Foundation. A couple of patches in the Perl core. A few CPAN modules. That about sums it up.

Journal of Ovid (2709)

Friday February 02, 2007
05:35 AM

Subtle Bug When Using Readonly

[ #32303 ]

The code originally had this:

my $CurrentUser = $ENV{SUDO_USER} || getpwuid $>;

Since that should be a constant and I was adding other constants, I changed it to this:

use Readonly;
Readonly my $CURRENT_USER => $ENV{SUDO_USER} || getpwuid $>;

Do you see the bug? It's a fatal error and three people read through the code and missed it (which is really no excuse for me writing it in the first place). Fortunately, it's an easy fix.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • Just looking at it, I'd guess that it's a precedence issue and you need some parens thrown in. This is what I get from B::Deparse.

      % perl -MO=Deparse -MReadonly -e 'Readonly my $CurrentUser = $ENV{SUDO_USER} || getpwuid $>'
      Type of arg 1 to Readonly::Readonly must be one of [$@%] (not scalar assignment) at -e line 1, at EOF
      -e had compilation errors.
      use Readonly;
      &Readonly(my $CurrentUser = $ENV{'SUDO_USER'} || getpwuid $>);

    So, not what I expected, but still an

    • It's a context error. Consider the first line:

      my $CurrentUser = $ENV{SUDO_USER} || getpwuid $>;

      That must be scalar context (in fact, the || forces the left side to be in scalar context). However, consider the new version:

      Readonly my $CURRENT_USER => $ENV{SUDO_USER} || getpwuid $>;

      We have a list, not an assignment, so we have list context. getpwuid $> in list context generates more than one value and Readonly recognizes that we're trying to use a scalar, so it dies. The fix is to fo

      • I'm not so sure "lists in scalar context" is what you mean. The comma operator in scalar context does that and qw(...) looks like a funny kind of list that might use the comma operator.
  • This is probably one of those times to say “Perl 6 will fix that.”

    (Besides providing for such as “is ro.”)