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

What i want to do is comment up a version of my Perl BIGTIME for my co-worker to take over it, but not use the commented version in the actual cgi directory where it will be executed - no problem for Perl, this would be text parsing. Up til now, whenever I move the file from where I work on it into the production directory, I run it through a script which find all of my dienice calls, dienice being a sub i wrote to output errors to the browser for ease of debugging ("so what does it say on your screen mister user?" he asked on his cell phone.) and numbers them with the current line number from the file being read using $.. so scripts go from:

# hey I'm a comment open(file, "wibbywoo") or dienice("some bloated error message");
to
# hey I'm a comment open(file, "wibbywoo") or dienice("Line 2: some bloated error message" +);
Now, however, I'll be strippng the comments first, /!^#/, and then outputing the file. So output will no longer come from a file, where I have $. to tell me the line number rather from an array of comment stripped lines.

The Question: aside from

my $counter = "0"; for (@stripped_array) { $_ =~ s/dienice\(\"/dienice\(\"Line $counter/; + $counter++; }
is there a better way? I looked through perlvar for some sort of 'array index' var for the current lexically relavant @_, but nothing seems to do the trick. Admittedly, perlvar has been known to make my philosoper's eyes bleed spontaneously from time to time, so I want to be sure I'm not missing something.

"sometimes when you make a request for the head you don't
want the big, fat body...don't you go snickering."
                                         -- Nathan Torkington UoP2K a.k.a gnat

Replies are listed 'Best First'.
RE: current array index
by Adam (Vicar) on Nov 03, 2000 at 23:53 UTC
    You could just use the __LINE__ literal for your line numbers, or did I miss something?

    Also, I like to use caller in my Error() function (or better yet, in $SIG{__DIE__}) and that will give me function name as well as line number.

      __LINE__ would give me the line numb er of current program, what I want is the index of the array which I am spooling out to a file, i.e. what it's line number _will be_ when it gets there so I can add that line number to any line containing an error trap so the error trap spits out the line it's on.

      I just read up on caller and i don't get it : ) I'm gonna play with it and see what I get...

      thanks!

      "sometimes when you make a request for the head you don't
      want the big, fat body...don't you go snickering."
                                               -- Nathan Torkington UoP2K a.k.a gnat

        If you put a literal __LINE__ in the output (but not inside quotes), then it will be replaced with the line number of the output file, which appears to be what you want. For example, your final output would look like:

        dienice("Line ",__LINE__,": oops\n");

        Alternately, you could use caller inside your dienice() routine. It would give you the line number of where dienice() was called from so you could prepend it to the message and wouldn't have to rewrite the calls to dienice(). For example:

        sub dienice { my $line= (caller)[2]; dienicely( "Line $line: ", @_ ); }

                - tye (but my friends call me "Tye")
RE: current array index
by AgentM (Curate) on Nov 04, 2000 at 01:01 UTC

      well, nothing, just never used it. I'll play with it and see how it goes. I'm not a big module user b/c the job where I worked at first had an outsourced webhost who allowed no modules at all. You could write all the perl you wanted, but no modules could be installed from the outside world. I wasn't smart enough at the time to just toss in em in the cgi-bin and let fly, but I learned a bad habit of not checking for them...which I am slowly breaking by using CGI and DBI ALL the time : )

      "sometimes when you make a request for the head you don't
      want the big, fat body...don't you go snickering."
                                               -- Nathan Torkington UoP2K a.k.a gnat

RE: current array index
by extremely (Priest) on Nov 04, 2000 at 12:34 UTC
    OK I know this is already solved but what is wrong with $.-- when you hit the !/^#/ and do the whole thing in one pass?
    #!/usr/bin/perl # comment while(<>) { if (/^#/) { print; $.--; } else { print "$.:$_"; } }

    Run that on itself.

    --
    $you = new YOU;
    honk() if $you->love(perl)

Re: current array index
by chromatic (Archbishop) on Nov 04, 2000 at 00:21 UTC
    This isn't any better, and could very well confuse your coworker:

    $_[$_] =~ s/dienice\(\"/dienice\(\"Line $counter/ for (0 .. scalar @_);

    That's untested and frankly I'm a little disgusted with it myself.