68 users online (0 members and 68 guests)  


  Results 1 to 3 of 3

Related

  1. Opening a file to a constant "size".    Forum: Javascript Forum
    Replies: 2
  2. Replies: 8
  3. Replies: 1
  4. Replies: 5
  1. #1
    RonLusk's Avatar
    New User

    Status
    Offline
    Join Date
    Dec 2007
    Posts
    2

    Can't open (FILE,"$filename")

    I have a bug that is driving me nuts!

    I'm trying to create a .txt file and write to it and keep getting the dreaded
    "Premature end of script headers: ..." error. The problem occurs on line 98 of the
    truncated code below.

    I got the same error when I inserted the -e test after declaring the $filename variable
    at line 96:
    if (-e "filename.ext") {
    print #alert to display results
    }

    I use the same coding (lines 99 - 108) in another perl script to write to a log file
    and it works fine. The only discernable differences are that the name of this file is
    being created dynamically and the extension is .txt instead of .log.

    Any light that anyone can shed on this dilemma will be greatly appreciated.


    Line
    No Code
    ###################################################################################
    1 #!/usr/bin/perl -w
    2
    3 require 'cookie.lib'; # From Matt's Script Archive - works like a charm
    4
    5 &checkOrder;
    6
    7 #############################################################################
    8
    9 sub checkOrder { # This sub-routine either gets or
    10 &GetCookies('KanKanOrder'); # creates the variable $orderNum
    11 if ($Cookies{'KanKanOrder'}) { # used to dynamically name the
    12 $orderNum = $Cookies{'KanKanOrder'}; # text file I'm trying to
    13 &main; # create/open on line 96.
    14 } #
    15 else { # $orderNum is simply a four digit
    16 if ($orderNum eq "") { # number with no leading or trailing
    17 &setOrderNum; # spaces or other characters.
    18 &main; #
    19 } #
    20 } #
    21 } #
    22
    23 ###############################################################################
    24
    25 sub main {
    26
    27 &getThisUser; # sets $the_user - no proplem
    28
    29 # lines 29 - 34 create $date -
    no problem
    35
    36 &parse_form; # no problem
    37
    38 $vendor = $FORM{'vendor'};
    39 # lines 39 - 73 creates 35 additional variables with no problem
    74 # lines 74 - 90 performs error check on critical form input $quantity with no problem
    91
    92 &prepare_data; # massages input fields to create variables used in line below
    93
    94 $newline = $the_date."|".$the_user."|".$vendor."|".$itemnumber."|".$itemname."|".
    $thisItemOption."|".$formattedPriceEach."|".$quantity."|".$formattedLineTotal."\n";

    # $newline correctly returns:
    # 11/12/07|Acme Lawn and Garden|Athens|01-0050|Small Square Table Set|
    # 1 unfinished at $99.50, 2 Forest Tan at $135.00 each|$99.50|3|$369.50

    95
    96 $filename = $orderNum.".txt";

    #filename correctly returns: 1007.txt

    97
    98 open (FILE,"$filename") || die "Cant open $filename\n"; # This is where the
    99 @LINES = <FILE>; # whole thing comes
    100 close (FILE); # crashing down!
    101 $SIZE = @LINES; #
    102 open (FILE,">$filename") || die "Cant open $filename\n"; # The basic coding is
    103 print FILE $newline; # the same as the sub-
    104 for ($i=0;$i<=$SIZE;$i++) { # routine below (setOrderNum),
    105 print FILE $LINES[$i]; # which works fine
    106 } #
    107 close(FILE); #
    108
    109 exit;
    110 }
    111
    112 ###############################################################################
    113
    114 sub setOrderNum {
    115 $filename = "order_number.txt";
    116 $Cookie_Path = '/';
    117 $Cookie_Domain = 'kankaninc.com';
    118
    119 open (FILE,"$filename") || die "Cant open $filename\n";
    120 $orderNum = <FILE>;
    121 close (FILE);
    122 $nextOrderNum = ++$orderNum;
    123 open (FILE,">$filename") || die "Cant open $filename\n";
    124 print FILE $nextOrderNum;
    125 close (FILE);
    126
    127 &SetCookies(KanKanOrder, $orderNum);
    128 }
    129
    130 ###############################################################################
    131
    132 # Lines 132 - 699 are the sub-routines getThisUser, prepare_data, and parse_form.
    # All of which perform as expected


  2. #2
    SoftNet-WD's Avatar
    New User

    Status
    Offline
    Join Date
    Dec 2007
    Posts
    2

    Talking Re: Can't open (FILE,"$filename")

    miss type on line 119 missing "<"

  3. #3
    RonLusk's Avatar
    New User

    Status
    Offline
    Join Date
    Dec 2007
    Posts
    2

    Re: Can't open (FILE,"$filename")

    Thanks for pointing out the error.

    Finally got the problem solved. Turns out it was a permissions issue.



Tags for this Thread