Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Strange behaviour (bug?) of use strict 'subs' (was: ... strict 'vars')

by Hofmator (Curate)
on Jun 23, 2002 at 12:09 UTC ( [id://176589]=perlquestion: print w/replies, xml ) Need Help??

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

Just the other night I came across the following. Consider

use strict; use warnings; my %foo; @foo{1..3} = (); print keys %foo; @foo{qw/1 2 3/} = (); print keys %foo;
both ways to assign to a hash slice work fine and produce the same results as expected.

Now add the following line to it:die "strange .." if foo eq 'bar'; which should make perl barf a "use of bareword 'foo' " error - as we are under strict. But it doesn't!! At least for me. I tried this under Perl 5.6.0 (ActivePerl Build 623) on Windows and Perl 5.6.1 on Linux (2.2.14).

If you remove the first hash slice line (@foo{1..3} = ();) then it works (or better not works i.e. throws the bareword error) correctly.

Well, so my questions are:

  • can anyone reproduce that behaviour (on different versions)?
  • can anyone explain it?
  • is it a (known?) bug?

-- Hofmator

2002-06-23 Edit by Corion : Changed title

Replies are listed 'Best First'.
Re: Strange behaviour (bug?) of use strict 'subs'
by particle (Vicar) on Jun 23, 2002 at 13:32 UTC
    it seems to me the problem is only found after using the range operator. also notice the bareword can be any word, it does not need to be in an existing glob.

    > perl -w -Mstrict -e"my%f;++@f{(1,2)};die'ack'if foo eq 'bar'" Bareword "foo" not allowed while "strict subs" in use at -e line 1. Execution of -e aborted due to compilation errors. > perl -w -Mstrict -e"my%f;++@f{qw/1 2/};die'ack'if foo eq 'bar'" Bareword "foo" not allowed while "strict subs" in use at -e line 1. Execution of -e aborted due to compilation errors. > perl -w -Mstrict -e"my%f;++@f{1..2};die'ack'if foo eq 'bar'" >
    (ActiveState Perl 5.6.1, Win32)

    a look at the code using B::Terse might help a little...

    ~Particle *accelerates*

Re: Strange behaviour (bug?) of use strict 'vars'
by ariels (Curate) on Jun 23, 2002 at 12:15 UTC
    It looks like a bug. I get the buggy behaviour on 5.6.1 (<samp>This is perl, v5.6.1 built for sun4-solaris</samp>), but not on 5.005_03 (<samp>This is perl, version 5.005_03 built for sun4-solaris</samp>).

    I think we need abigail-II...

    Update

    I get the same results (no error message, i.e. the bug is exercised) with v5.6.1 on i686-linux, IP27-irix and alpha-dec_osf.

    (Temporary) update2

    particle suggests I mention, until the editors fix the node title, that it's really strange behaviour of use strict 'subs': it relates to the use of a bareword.
Re: Strange behaviour (bug?) of use strict 'vars'
by Anonymous Monk on Jun 23, 2002 at 17:40 UTC
    Behavior confirmed in 5.8.0-RC2 (linux) as well. No bare word warnings anywhere if a list-context range op is used in a statement prior to the bare words.
    use strict; use warnings; print 1..2,"\n"; die 'bug' if foo eq bar;
      Hmm... My Perl 5.8.0 RC 2 (i686-linux-thread-multi-64all-ld) on linux (RedHat 7.3) gives a Bareword "foo" not allowed while "strict subs" in use at - line 5. on that code. It also raises an error on the orgininal post when use strict is in effect.

      Can anyone test this on another 5.8.0 RC 2 ?

      -- Joost downtime n. The period during which a system is error-free and immune from user input.
        My mistake. It appears I wasn't calling the perl I thought I was. 5.8.0-RC2 (linux) does raise exceptions on the code just as Joost observes. My apologies for any confusion.
Re: Strange behaviour (bug?) of use strict
by little (Curate) on Jun 23, 2002 at 13:15 UTC
    Mh, funny.
    # condition will not be true but raises no error if (defined foo) { print ref(foo); }; # raises an error as foo is marked as sub die "strange .." if foo() eq 'bar'; # also die "strange .." if (foo() eq 'bar'); # and even this does not show up _any_ errors or warnings #!/usr/bin/perl -w use strict; use diagnostics; . . . print ref(foo); # So is foo something special ? # No, no error or warning also # when replacing above line simply with print ref(bar);
    So, I have no idea either what perl is assuming here.

    Have a nice day
    All decision is left to your taste
    Update
    Changed title to ".. use strict" as it seems not clear which pragma is conflicted, could be strict 'refs' as well
    Update 2
    even more funny, as this is different from particle's command line call in the use of -m instead of -M.

    C:\web\>perl -w -mstrict -e"my %f;++@f{qw/1 2/};die'ack'if foo eq 'bar';"
    Unquoted string "foo" may clash with future reserved word at -e line 1.

    Just to show why I think that it's not clear which pragma is conflicted. :-)
    addendumAristotle is correct, as here the strict pragma does not import any pragmas and is disabled. So at least its a prove as he said.
      No, it's not. -m equates to use MODULE qw(); (see perlrun) which means -mstrict disables all strictures. Further, it actually demonstrates that it is the subs stricture that is conflicted, because enabling it causes Perl to skip the (then obviously redundant) bareword warnings.

      Makeshifts last the longest.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://176589]
Approved by ariels
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-25 23:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found