swaroop.m has asked for the wisdom of the Perl Monks concerning the following question:

Greetings Monks,
I have a perl script to generate formatted reports out of a text file.
I am using file i/o and regexpressions to do it.
I used PerlCC to convert the perl script into a perl executable
. But when i try to run the exe , it fails by giving out the following error
"Modification of a read-only value attempted at Monthly_Reports.pl line 5. *main::PerlIO::" The line 5 in the perl script is
line 5: my $filename = <STDIN>; line 6: open (FH,$filename); line 7: my @Lines = <FH>; line 8: close FH;
I am just taking the filename and reading from the file.
Please correct me if iam doing something wrong. I have Perl 5.8.4 on Windows machine.
Ne pointers are greatly appreciated
--Swaroop

Replies are listed 'Best First'.
Re: Perl to executable conversion error
by gothic_mallard (Pilgrim) on Oct 07, 2004 at 11:37 UTC

    The code fragment appears to be wrong.

    • If you're reading from <STDIN> then you'll need to chomp() the input otherwise the open will more than likely fail. You may think you're asking it to read file "myfile.txt", but really it'll try to open "myfile.txt\n".
    • Why are you array dereferencing $filename in the open()? It's a scalar value, not an array so what you're doing is reading the file name into a variable called $filename but attempting to read it from an array called @filename (which are different things).

    Try this:

    my $filename = <STDIN>; chomp($filename); open (FH,$filename) or die "Unable to open file: $!"; my @Lines = <FH>; close FH;

    You should also check your return values when attempting to open files otherwise it could silently fail and then fall over later when the code you expect isn't there... The $! is very handy for rooting out why something won't open.

    I didn't manage to get it to throw the error you came up with so perhaps where Perl is reporting isn't actually the root of the problem (which is fairly common with most programming languages!).

    --- Jay

    All code is untested unless otherwise stated.

      Howdy!

      Why is the chomp needed? Remember, open does magical things...

      My test execution of the code as proffered (wrapped with use strict, use warnings, and a print @Lines at the end) yielded no errors.

      Now, there are security implications in using raw input as a filename in a two-argument open, but the code as presented does work.

      yours,
      Michael
Re: Perl to executable conversion error
by TheEnigma (Pilgrim) on Oct 07, 2004 at 11:34 UTC
    $filename is a scalar, but you're using it like an array in line 6. Just say:

     open (FH,$filename) or die "Can't open $filename: $!";

    (I added error checking for you)

    TheEnigma

      Howdy!

      That's just not correct. There is nothing wrong with the syntax of the code as presented.

      What do you mean when you say "you're using it like an array"?

      Update: Ah...that makes sense. Bad OP. No biscuit. By silently changing your node, you make other people look foolish, and you make yourself look not so good either. At least add a postscript that explains your change, since it was significant.

      yours,
      Michael
        He's corrected it since he first posted. It used to say:

        line 6: open (FH,$filename[0]);

        as gothic_mallard also pointed out.

        TheEnigma

Re: Perl to executable conversion error
by prasadbabu (Prior) on Oct 07, 2004 at 16:07 UTC

    The syntax seems to be correct.

    I think the error might be due to conversion of .pl file to exe

    If you want try out perl2exe and perlapp for the conversion of perl file to exe conversion.

    just a try.

    Prasad

      Yes
      there's a problem when i convert a .pl file to .exe,
      Perl2exe in not a free download. It works fine when i make an executable with Perl2Exe, its only when i try using PerlCC that it fails.
      Sorry for the mistake in my eariler post(which i have updated.My Apologies to all esteemed monks!)
      Swaroop
Re: Perl to executable conversion error
by TheEnigma (Pilgrim) on Oct 07, 2004 at 13:40 UTC
    swaroop.m, when you edit your post, it's considered A Good Thing to make a note of your change at the bottom of the post. That leads to less confusion for readers.

    Thanks!

    TheEnigma