Perl compiler? error

Ask a Question related to PERL Miscellaneous, Design and Development.

  1. #1

    Default Perl compiler? error

    Lets say a friend of mine :) wrote code which had a bad error in it:

    #!/usr/bin/perl
    use warnings;
    use strict;

    my $words = ["a", "b", "c"];
    my $num = 612;

    if($num == 1)
    {
    print "moo\n";
    }
    elsif (map($_->[0] =~ /\d{6}/, @$words))
    {
    print "Don't give up day job\n";
    }

    When run this gives the error:
    Can't use string ("a") as an ARRAY ref while "strict refs" in use at
    ../test.pl line 8.
    After about 1/2 hour of head banging, the problem (which was
    originally in a much larger program) was finally discovered. The code
    problem wasn't on line 8 :). I have fixed that and slapped my "friend"
    around the face for stupidity.

    However this leads to my real question:
    Is the error message above, an error in the compiler? For instance if
    the close bracket of an elsif is forgotton, you get an error on the
    line where the close bracket was left out. *Not* on the line where the
    start of the IF THEN ELSE syntax began.

    e.g. if I fixed line 8 to (oops left out close bracket for elsif):

    elsif (map(/\d{6}/, @$words)

    Error message is:

    syntax error at ./test.pl line 14, near ")
    {"
    Execution of ./test.pl aborted due to compilation errors.
    fatted Guest

  2. Similar Questions and Discussions

    1. Compiler Error
      Hi CJ, You will need to give these two accounts, IUSR_MachineName and ASPNET, permissions to the directories they are being denied acces to. ...
    2. mxmlc compiler error
      Hi, I am having remoteobjects in my mxml file. I am able to compile my file with D:\SVN\Code\myprojfit\web\WEB-INF\flex\bin>mxmlc -o...
    3. Compiler Error Message: CS0123:
      Hi Everyone...Can somebody help me figure out how to fix this error? I'm out of ideas! Thanks! CS0123: Method...
    4. compiler error code 128
      Help !!!!!!! All the ASP.NET websites on our server are giving the error: The compiler failed with error code 128 the server is running .NET...
    5. Compiler Error Message: The compiler failed with error code 128.
      Hi. I am having trouble running my aspx code. I created two simple webforms, which i try to run from two different directories one is giving me...
  3. #2

    Default Re: Perl compiler? error

    [email]fatted@yahoo.com[/email] (fatted) wrote in message news:<4eb7646d.0307280451.1acadb3f@posting.google. com>...
    > Lets say a friend of mine :) wrote code which had a bad error in it:
    >
    > #!/usr/bin/perl
    > use warnings;
    > use strict;
    >
    > my $words = ["a", "b", "c"];
    > my $num = 612;
    >
    > if($num == 1)
    > {
    > print "moo\n";
    > }
    > elsif (map($_->[0] =~ /\d{6}/, @$words))
    > {
    > print "Don't give up day job\n";
    > }
    >
    > When run this gives the error:
    > Can't use string ("a") as an ARRAY ref while "strict refs" in use at
    > ./test.pl line 8.
    > After about 1/2 hour of head banging, the problem (which was
    > originally in a much larger program) was finally discovered. The code
    > problem wasn't on line 8 :). I have fixed that and slapped my "friend"
    > around the face for stupidity.
    >
    > However this leads to my real question:
    > Is the error message above, an error in the compiler? For instance if
    > the close bracket of an elsif is forgotton, you get an error on the
    > line where the close bracket was left out. *Not* on the line where the
    > start of the IF THEN ELSE syntax began.
    >
    There's no compiler error here, but I think you may have a conceptual
    error. The 'elsif' clause establishes a Boolean context, i.e., either
    a 'true' or 'false' (in Perl's terms) is the only acceptable answer.
    Boolean context should be thought of as a subset of scalar context.
    'map', however, generates a list by performing an operation on each
    element of a source list. Hence, 'map' normally establishes a list
    context. However, since that 'map' is itself within the
    Boolean/scalar context established by 'elsif', it will impose a scalar
    context, i.e., it will ask how many items are in the resulting list.
    If there is a nonzero number of items in the source list, the 'elsif'
    will register as true.

    Now, are you sure that that's what you were aiming for with this code?
    (In short, I'm puzzled by your use of 'map' inside an 'elsif'
    condition.)

    Jim Keenan
    James E Keenan Guest

  4. #3

    Default Re: Perl compiler? error


    "Jeff 'japhy' Pinyan" <pinyaj@rpi.edu> wrote in message
    news:Pine.SGI.3.96.1030728142203.44173A-100000@vcmr-64.server.rpi.edu...
    > On 28 Jul 2003, James E Keenan wrote:
    >
    > >>
    > >There's no compiler error here, but I think you may have a conceptual
    > >error. The 'elsif' clause establishes a Boolean context, i.e., either
    > >a 'true' or 'false' (in Perl's terms) is the only acceptable answer.
    >
    > You're not answering the question, though. The OP is curious why the
    > error is said to stem from line 8, when the expression causing the error
    > is on line 12. This is because of the way Perl compiles the if-else block
    > internally.
    >
    True. I was posting from Google, so I had to guess as to whether other
    responses had been written but not yet posted. My hunch was that someone
    would have made the point you did (Brian did, albeit in less specific terms
    than you), so I felt free to raise another point about the OP's code.
    Thanks.


    James E Keenan Guest

  5. #4

    Default Re: Perl compiler? error

    [email]jkeen@concentric.net[/email] (James E Keenan) wrote in message news:<b955da04.0307281016.17c5355d@posting.google. com>...
    > [email]fatted@yahoo.com[/email] (fatted) wrote in message news:<4eb7646d.0307280451.1acadb3f@posting.google. com>...
    > > Lets say a friend of mine :) wrote code which had a bad error in it:
    > >
    > > #!/usr/bin/perl
    > > use warnings;
    > > use strict;
    > >
    > > my $words = ["a", "b", "c"];
    > > my $num = 612;
    > >
    > > if($num == 1)
    > > {
    > > print "moo\n";
    > > }
    > > elsif (map($_->[0] =~ /\d{6}/, @$words))
    > > {
    > > print "Don't give up day job\n";
    > > }
    > >
    > > When run this gives the error:
    > > Can't use string ("a") as an ARRAY ref while "strict refs" in use at
    > > ./test.pl line 8.
    > > After about 1/2 hour of head banging, the problem (which was
    > > originally in a much larger program) was finally discovered. The code
    > > problem wasn't on line 8 :). I have fixed that and slapped my "friend"
    > > around the face for stupidity.
    > >
    > > However this leads to my real question:
    > > Is the error message above, an error in the compiler? For instance if
    > > the close bracket of an elsif is forgotton, you get an error on the
    > > line where the close bracket was left out. *Not* on the line where the
    > > start of the IF THEN ELSE syntax began.
    > >
    > There's no compiler error here, but I think you may have a conceptual
    > error. The 'elsif' clause establishes a Boolean context, i.e., either
    > a 'true' or 'false' (in Perl's terms) is the only acceptable answer.
    > Boolean context should be thought of as a subset of scalar context.
    > 'map', however, generates a list by performing an operation on each
    > element of a source list. Hence, 'map' normally establishes a list
    > context. However, since that 'map' is itself within the
    > Boolean/scalar context established by 'elsif', it will impose a scalar
    > context, i.e., it will ask how many items are in the resulting list.
    > If there is a nonzero number of items in the source list, the 'elsif'
    > will register as true.
    >
    > Now, are you sure that that's what you were aiming for with this code?
    > (In short, I'm puzzled by your use of 'map' inside an 'elsif'
    > condition.)
    >
    > Jim Keenan
    I'm comparing each value from the list stored in the array (reference)
    to see if it matches the regular expression. If it matches , the
    return value of map will be greater than 0, which will be evaluated by
    elsif as true and run the code in the elsif block. Seems like a
    perfectly good idea to me :)
    My coding problem occured, because earlier in my script the value I
    wanted to compare against a reg exp, was stored in an array reference
    of array references; this doesn't work half as well when trying to
    compare against just an array reference:) Unfortunately I did a copy
    paste, turned my brain off, and was thrown by the runtime error.
    fatted Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139