Ask a Question related to PERL Miscellaneous, Design and Development.
-
fatted #1
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
-
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. ... -
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... -
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... -
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... -
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... -
James E Keenan #2
Re: Perl compiler? error
[email]fatted@yahoo.com[/email] (fatted) wrote in message news:<4eb7646d.0307280451.1acadb3f@posting.google. com>...
There's no compiler error here, but I think you may have a conceptual> 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.
>
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
-
James E Keenan #3
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...True. I was posting from Google, so I had to guess as to whether other> 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.
>
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
-
fatted #4
Re: Perl compiler? error
[email]jkeen@concentric.net[/email] (James E Keenan) wrote in message news:<b955da04.0307281016.17c5355d@posting.google. com>...
I'm comparing each value from the list stored in the array (reference)> [email]fatted@yahoo.com[/email] (fatted) wrote in message news:<4eb7646d.0307280451.1acadb3f@posting.google. com>...> There's no compiler error here, but I think you may have a conceptual> > 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.
> >
> 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
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



Reply With Quote

