www.webdeveloper.com
Results 1 to 4 of 4

Thread: Variable Problem

  1. #1
    Join Date
    Jun 2008
    Posts
    223

    Variable Problem

    I have a script which I use to access some files. The first step of the processs is to enter a word in a form, which then passes me to the file.

    words.txt (the file with the words in)
    pizza
    beer

    Code:
    open (AWDF,"words.txt");
    $first = <AWDF>;
    close (AWDF);
    $theword = "$first";
    
    print "$theword"; # ----- works fine
    The print shows the first word in the file like it should. It even passes to other parts of the script and can be printed (for testing).

    However, when I use word in a form to do the first step in the process of the script - it does not work.

    But it will if the variable is like this!
    Code:
    $theword = "pizza";
    ...and everything works properly.

    I don't understand why the variable will work one way, but not the other. What have I done wrong?

    Any ideas?

  2. #2
    Join Date
    Jun 2008
    Posts
    223
    I think I've just solved it.

    Code:
    open (AWDF,"words.txt");
    $first = <AWDF>;
    close (AWDF);
    chomp($first);
    $theword = "$first";
    It appears that reading the file like this produced some whitespace that I was unaware of. A chomp was inserted and the script now works. I can enter my word and go to the next process okay.

  3. #3
    Join Date
    Mar 2010
    Location
    Singapore
    Posts
    367
    Quote Originally Posted by edatz View Post
    I think I've just solved it.

    Code:
    open (AWDF,"words.txt");
    $first = <AWDF>;
    close (AWDF);
    chomp($first);
    $theword = "$first";
    It appears that reading the file like this produced some whitespace that I was unaware of. A chomp was inserted and the script now works. I can enter my word and go to the next process okay.
    In general, Perl process text file assuming newline is the record separator. It has been debated why the default file input operator left the newline in the variable. There are developers who demand Perl take default action to remove the newline and there are developers who say put the whole line including the newline in and let them decide what to do with it. My experience is usually I don't need the newline at the end.

    chomp function will remove the last character if it is newline. chop function will just blindly remove the last character regardless if it is newline or not.

  4. #4
    Join Date
    Jun 2008
    Posts
    223
    My experience is usually I don't need the newline at the end.
    Mine too. Though there are times it's handy, but I'd rather code that in, instead of wondering if Perl is goning to leave it there or not (sometimes it doesn't appear to happen).

    In this case each word is a whole record and I read the first line only. The file gets rotated daily (ish). My rotation code is a simple thing that has an SSI on each HTML page (where needed) so that the first hit to the site kicks in the rotation. I do this instead of a CRON Job, so it only actions when needed.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles