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

I think this particular perl install or something is stuffed..it's a 5.06 on Win2000 Server. In the following (modified for brievity) snippet:
use strict; use warnings; my @array = qq(bla bla2 bla3); for ( my $cnt = 0; $cnt<=@array; $cnt++) { print $array[$cnt]; }
i get use of uninitialized value and it prints three lines of zero. I've played a bit more with this putting "our" inside the loop, but that only works if i print @array--not $array[$cnt]. it looks like vars are going out of scope magically. where to look in this install / Win2000 Server setup to fix this? Chris

Replies are listed 'Best First'.
Re: vars going out of scope!
by Abigail-II (Bishop) on Apr 23, 2003 at 00:54 UTC
    Strange, I only get the use of uninitialized value, and the elements of the array are printed. The uninitialized value comes from the fact you let $cnt become scalar @array. But the last element of the array has index scalar (@array) - 1. Use either for (my $cnt = 0; $cnt < @array; $cnt) { ... }, or
    foreach my $elem (@array) { print $elem; }

    Abigail

      thanks for your quick post....maybe i should have used original code, which at moment i don't have access to as it's on a customer site with dial-up and someone else is in there at moment. the original code, which i re-wrote to use arrays. the jist of the original code was:
      # no strict or warnings $bla = "this and that"; $file = "c:\filea"; system("echo $bla > $file"); # this works if (1==1) #not the actual test, but it goes in this test { system("echo $bla > $file"); #this never occurs #if run by an exe that calls Perl -U this_script #and passes output to IIS #it does work from cmd prompt }
      is this a perl install issue? stacks? weird stuff!

      Chris

        This is an IIS issue. Your installation of Perl is OK. You have to remember that IIS is running as a different account. If your script doesn't have permissions to accomplish the task, usually your script will just not run that part. When you make system calls w/in your Perl scripts that are run via IIS, you have to remember to set your environment just as if you were running it from the same cmd.exe environment. The easiest way (not the safest) is to use IIS admin tool to set your script up to run as you and then restart the server. If it runs as expected, that means you have to think about the security issues of running it that way, but it gets it working in the short term. This was a problem for Net::Ping users that developed IIS apps to use it... their scripts had to be run as Admin... yuch. Win32::PingICMP uses a dll which is the same that Win uses to allow anonymous ping. Hope that helps :) (been there too) JamesNC :0)
Re: vars going out of scope!
by slife (Scribe) on Apr 23, 2003 at 09:58 UTC

    Although this is not directly related to your problem, I suspect that the line:

    my @array = qq(bla bla2 bla3);

    is not what you want here.

    This assigns the scalar "bla bla2 bla3" to the first element of @array. I imagine you wanted:

    my @array = qw(bla bla2 bla3);.

Re: vars going out of scope!
by aquarium (Curate) on Apr 23, 2003 at 00:50 UTC
    ..sorry about the brackets appearing as a url...this is not very user friendly for posting. Chris