CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
File is not updating

 



jdp12383
Novice

Feb 6, 2013, 4:03 PM

Post #1 of 7 (609 views)
File is not updating Can't Post

Hi,

I am new to perl and two questions.

1. I am working on program of reading and writing into file. The file contains two lines. I am trying to read the parameters like last visitor name and counter of visitor. I parse the parameters from file, clean the file and append file with the latest values but somehow it is not appending the file.

2. The data.bin file is created in the directory where perl.exe resides. Is there any way I can create it where my .pl file resides?

I really appreciate your guidance resolving this thread.

======data.bin=====

@Params_name=abc
@Params_visitor=0

=================


my $name = "XYZ";

use strict;
use warnings;

my $file = "data.bin";
open (my $IN, "<", $file) or die "$0: Can't open input file $file: $!\n";

my %Params;

while (<$IN>) {
chomp;
if ($_ =~ /^\@Params_/) {
my ($key, $value) = split(/=/, $_);
$Params{$key} = $value;
print "$key=$value\n";
}
}
close($IN);

open (my $FILE, ">", $file) or die "$0: Can't open input file $file: $!\n"; #Clear the file

print $FILE, ">>", "\@Params_name=$name\n";
my $counter = $Params{"\@Params_visitor"} + 1;
print $FILE, ">>", "\@Params_visitor=$counter";

close($FILE);

Thanks,


BillKSmith
Veteran

Feb 6, 2013, 9:08 PM

Post #2 of 7 (597 views)
Re: [jdp12383] File is not updating [In reply to] Can't Post

Lets work on question 2 first. What operating system are you using? How do you run the perl script? Is perl.exe in your path?

I believe that this proceedure will work for any OS.
  • Verify that perl.exe is in your path.

  • Verify that .pl and .bin files are in the working directory.

  • run the perl program with the command: perl yourscript.pl


  • Your file reading and parsing appear to be correct. I am not clear what you mean by "append" . Would you please post the output that you expect from your sample input.
    Good Luck,
    Bill


    jdp12383
    Novice

    Feb 7, 2013, 10:19 AM

    Post #3 of 7 (592 views)
    Re: [BillKSmith] File is not updating [In reply to] Can't Post

    I am working on Windows 7 Enterprise 64-bit. I am using the Mongoose web sever v3.6.

    1. I am running .pl script from the command prompt from c:\Perl64\bin directory. I have not included this path like #!C:\Perl64\bin\. I checked the PATH environment variable and this path is there already.

    2. This is what I am targeting. The .bin file should be created at the same location where my .pl file is but it is not creating it at this location.

    3. I tried executing .pl file from command prompt by going inside that directory and it is creating file there but it is not updating properly (at the end of script file becomes empty). The script output shows as below,

    @Params_name=abc
    @Params_visitor=0
    GLOB(0x22b4e80)>>@Params_name=XYZ
    GLOB(0x22b4e80)>>@Params_visitor=1

    4. This is the trial I am running to test just script file and this script will be executed from .html file and will be in action field of form tag.

    After completion of my script the .pl file, It should change the file as below,

    ===============data.bin=================

    @Params_name=XYZ

    @Params_visitor=1

    ======================================

    Please let me know if you want further information.

    What you think about my other question?

    Thanks,


    (This post was edited by jdp12383 on Feb 7, 2013, 10:20 AM)


    FishMonger
    Veteran / Moderator

    Feb 7, 2013, 11:17 AM

    Post #4 of 7 (588 views)
    Re: [jdp12383] File is not updating [In reply to] Can't Post

    In the print statements you need to remove the comma after the filehandle as well as the ">>".


    Code
    print $FILE "\@Params_name=$name\n";


    Personally, I don't like using all uppercase filehandles and I prefer (and I believe it's best practice) to use braces arount the filehandle.


    Code
    print {$fh} "\@Params_name=$name\n";



    (This post was edited by FishMonger on Feb 7, 2013, 11:19 AM)


    jdp12383
    Novice

    Feb 7, 2013, 11:33 AM

    Post #5 of 7 (581 views)
    Re: [FishMonger] File is not updating [In reply to] Can't Post

    Wow. Awesome FishMonger. I removed comma and >> and it resolved one of my problem.

    One morething I want to know is when I call this script from .html form tag as action field will it create file at this same location where .pl file resides?


    (This post was edited by jdp12383 on Feb 7, 2013, 11:33 AM)


    FishMonger
    Veteran / Moderator

    Feb 7, 2013, 11:52 AM

    Post #6 of 7 (576 views)
    Re: [jdp12383] File is not updating [In reply to] Can't Post

    It will as long as you have the permissions setup correctly. However, it's better to put the file in a directory outside of the scripts directory. In fact it's better to put it outside of the document root so that only your script can access/modify it.


    jdp12383
    Novice

    Feb 7, 2013, 11:58 AM

    Post #7 of 7 (574 views)
    Re: [FishMonger] File is not updating [In reply to] Can't Post

    Thanks FishMonger for your help.

    I appreciate.

     
     


    Search for (options) Powered by Gossamer Forum v.1.2.0

    Web Applications & Managed Hosting Powered by Gossamer Threads
    Visit our Mailing List Archives