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

I have completed a script that works on my PC when all files are in the same directory. However, when I upload..stops working. I think it's something to do with the pathnames...
Basic premise:

1) script reads existing db created by formmail clone into array

(formmail script writes correctly to path of /home/hampton1/data/student.dat)

2) parse each line to create two more arrays

3) write out a second file with the values in the arrays.

4) Last read the newly created second file and write a third file with js in it.

The new files created in this script are not being created so js in html page is throwing up.

Newbie to perl but longtime DB programmer... big learning curve!
#!/usr/bin/perl use CGI use File::Copy; use strict; use warnings; # define names of student reg info file,student count file and name of + js file to be sritten out $sdata="/home/hampton1/data/student.dat"; $scnt="/home/hampton1/data/student_count.dat"; $jscd="/home/hampton1/data/jscd.js"; # open existing student reg file open(SDATA,"$sdata")|| die("Could not open file!"); # Put file into array @line = <SDATA>; # delete existing count & js file - so they can be to be recreated if (-e "/home/hampton1/data/student_count.dat") { unlink($scnt); } if (-e "/home/hampton1/data/jscd.js") { unlink($jscd); } # create file(> symbol is overwriting) open (SCNT,">$scnt"); # set all permissions chmod 0777, $scnt or die "Couldn't chmod $scnt: $!"; # create file(> symbol is overwriting) open (JSCD,">$jscd"); # set all permissions chmod 0777, $jscd or die "Couldn't chmod $jscd: $!"; # Parse fields in first line ($empty, $dtereg, $timereg, $c1,$c1tot, $c2, $c2tot) = split(/\|/,$lin +e[0]); # Load c1tot array from first record @clsid=$c1; @cqty = $c1tot; if ($c2 ne " ") { push (@clsid,$c2); push (@cqty,$c2tot); } @line = pop(@line); close SDATA; foreach $listitem (@line) { # remove CR/LF chop $listitem; # parse fields in each subsequent line ($empty, $dtereg, $timereg, $c1,$c1tot, $c2, $c2tot) = split(/ +\|/,$listitem); # see if class exists in array already if (grep {$_ eq $c1} @clsid) { $idx = indexArray($c1,@clsid); $cqty[$idx]+= +$c1tot; } # else add to array else { push (@clsid,$c1); push (@cqty,$c1tot); $idx = indexArray($c1,@clsid); } # See if class (2) exists in array already if (grep {$_ eq $c2} @clsid) { $idx = indexArray($c2,@clsid); $cqty[$idx]+= +$c2tot; } # else add class 2 to array else { push (@clsid,$c2); push (@cqty,$c2tot); $idx = indexArray($c2,@clsid); } } $inum = 0; # find last element # (# elements in array0 $idxcid = $clsid; # print result to student count file while ($inum <= $idxcid) { print SCNT "$clsid[$inum]\|"; print SCNT "$cqty[$inum]\n"; $inum = ++$inum; } close SCNT; sub indexArray{for(1..@_){$_[0]eq$_[$_]&&return$_-1}-1} # print results to javascript file print JSCD "function getarray ()\n"; print JSCD "{\n"; print JSCD "jclsid = []\n"; $inum = 0; # find last element # (# elements in array) $idxcid = $clsid; while ($inum <= $idxcid) { print JSCD "jclsid[$inum] = \"$clsid[$inum]\"\n"; $inum = ++$inum; } print JSCD "jclscnt = []\n"; $inum = 0; # find last element # (# elements in array) $idxcid = $clsid; while ($inum <= $idxcid) { print JSCD "jclscnt[$inum] = $cqty[$inum]\n"; $inum = ++$inum; } print JSCD "}\n"; close JSCD; # copy js file to public_html dir where all other js file are # is this needed??? $newjscd ='/home/hampton1/public_html/jscd.js"; copy($jscd, $newjscd) or die "File cannot be copied.";
script is in /public_html/cgi-bin of course but have I specified the right path to the other files? Any assistance would be greatly appreciated! JV

Replies are listed 'Best First'.
Re: specifying path to files
by toolic (Bishop) on Oct 23, 2007 at 16:52 UTC
    I am not sure what the solution to your specific problem is, but you should consider the following:

    There is at least 1 typo in your posted code. use CGI needs a semicolon.

    Using the strictures can help prevent other simple mistakes:

    use warnings; use strict;
    This will force you to declare all yor variables using my, which will be well worth the effort and time.

    The following line looks odd:

    @line = pop(@line);
    pop actually modifies the array.

    The following line also looks odd:

    $inum = ++$inum;
    ++$inum may be sufficient.

    Declaring a sub in the middle of your main code is a little confusing.

    Your sub indexArray, while it may earn you a low Perl golf score, should probably not be so compact. It would benefit greatly by some added whitespace.

    I hope this helps.

Re: specifying path to files
by naikonta (Curate) on Oct 23, 2007 at 16:35 UTC
    You hardcode your pathnames. Do you have directory /home/hampton1/data in the target server? Putting $! in die statement could tell you right away.
    open(SDATA,"$sdata")|| die("Could not open file: $!\n");
    Also,
    unlink $scnt;
    would be sufficient instead of
    if (-e "/home/hampton1/data/student_count.dat") { unlink($scnt); }
    since you already assigned $scnt to /home/hampton1/data/student_count.dat, unless you care about why unlink didn't remove the file (which is another matter).

    Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

      Yes I have created data folder and set all permissions.

      It's screwy- formmail writes out file (actually appends) just fine.

      Pls excuse redundent (unnecessary coding) - old habits from DB prgramming days.

      JV
Re: specifying path to files
by thezip (Vicar) on Oct 23, 2007 at 16:39 UTC

    Hello jvallee,

    Before I begin poring through the potentialities of your code, I must insist that you enforce strictures via use strict; and use warnings;

    Once that is done, go ahead an try to compile it, just to make sure there aren't any hidden issues lurking... Let us know when you have done this by updating your node. Thanks.


    Where do you want *them* to go today?