trouble reading line by line

Discussion in 'Perl Misc' started by wmconlon, Apr 22, 2005.

  1. wmconlon

    wmconlon Guest

    Really perplexed about reading a file line by line in a cgi script.

    With one cgi parameter a subroutine does:

    sub view {
    my $file = $_[0];
    open(TEXT, "<$file") or die "Can't open $file for preview";
    while (defined ($text = <TEXT>)) {
    ....

    This processes line-by-line.

    Another cgi parameter invokes a different subroutine call which does:

    sub add {
    my $template_path=$_[0];
    my $directory_path=$_[1];
    my $name=$_[2];
    open(TEXT, "<$template_path") or die "Can't open $template_path for
    reading"; # open the template file
    while( defined ($text = <TEXT>)) {
    ....
    and reads the entire file into a single line, even though it is reading
    the same file.

    I guess it has something to do with scalar context, isn't
    $template_path a scalar? I've battled this for a day, so it must be
    something obvious.
     
    wmconlon, Apr 22, 2005
    #1
    1. Advertising

  2. wmconlon wrote:
    > Really perplexed about reading a file line by line in a cgi script.
    >
    > With one cgi parameter a subroutine does:
    >
    > sub view {
    > my $file = $_[0];
    > open(TEXT, "<$file") or die "Can't open $file for preview";
    > while (defined ($text = <TEXT>)) {
    > ...
    >
    > This processes line-by-line.
    >
    > Another cgi parameter invokes a different subroutine call which does:
    >
    > sub add {
    > my $template_path=$_[0];
    > my $directory_path=$_[1];
    > my $name=$_[2];
    > open(TEXT, "<$template_path") or die "Can't open $template_path for
    > reading"; # open the template file
    > while( defined ($text = <TEXT>)) {
    > ...
    > and reads the entire file into a single line, even though it is reading
    > the same file.


    Please post a short but *complete* program that demonstrates the
    behaviour you mention.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Apr 22, 2005
    #2
    1. Advertising

  3. wmconlon

    teodell Guest

    That should work fine. The only thing is the $template_path an actually
    file then? The section with $template_path, $directory_path and $name
    sounds like template_path is a directory. IF it is a directory it will
    slurp it up in one line since directories need opendir, readdir, etc.

    todd
     
    teodell, Apr 22, 2005
    #3
  4. wmconlon

    teodell Guest

    That should work fine. The only thing is the $template_path an actually
    file then? The section with $template_path, $directory_path and $name
    sounds like template_path is a directory. IF it is a directory it will
    slurp it up in one line since directories need opendir, readdir, etc.

    todd
     
    teodell, Apr 22, 2005
    #4
  5. wmconlon

    teodell Guest

    That should work fine. The only thing is the $template_path an actually
    file then? The section with $template_path, $directory_path and $name
    sounds like template_path is a directory. IF it is a directory it will
    slurp it up in one line since directories need opendir, readdir, etc.

    todd
     
    teodell, Apr 22, 2005
    #5
  6. wmconlon

    peter pilsl Guest

    >
    > sub view {
    > my $file = $_[0];
    > open(TEXT, "<$file") or die "Can't open $file for preview";
    > while (defined ($text = <TEXT>)) {
    > ...
    >
    >
    > sub add {
    > my $template_path=$_[0];
    > open(TEXT, "<$template_path") or die "Can't open $template_path for
    > reading"; # open the template file
    > while( defined ($text = <TEXT>)) {
    > ...


    For what you showed us, the two subs does exactely the same. Looks like
    the read files are different or $/ is changed between the calls.

    best,
    peter


    --
    http://www.goldfisch.at/know_list
     
    peter pilsl, Apr 22, 2005
    #6
  7. wmconlon

    wmconlon Guest

    $template_path contains an absolute file path. So it is a file, and
    I've printed it to be sure. And I can print the contents of $text to
    see it as a single line in one case, and the entire file in another.
     
    wmconlon, Apr 22, 2005
    #7
  8. wmconlon

    wmconlon Guest

    Bingo!

    Thx.
     
    wmconlon, Apr 22, 2005
    #8
  9. wmconlon <> wrote:


    > isn't
    > $template_path a scalar?



    Everything that starts with a dollar sign is a scalar.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Apr 22, 2005
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hypo
    Replies:
    6
    Views:
    460
  2. Jeff Rodriguez

    Reading a file line by line... in reverse

    Jeff Rodriguez, Nov 27, 2003, in forum: C Programming
    Replies:
    4
    Views:
    419
    goose
    Nov 27, 2003
  3. reading line by line from file

    , Mar 29, 2006, in forum: C Programming
    Replies:
    20
    Views:
    705
    CBFalconer
    Mar 31, 2006
  4. Lee Sander
    Replies:
    6
    Views:
    314
    Hendrik van Rooyen
    Jun 1, 2007
  5. Steve Howell
    Replies:
    3
    Views:
    297
    George Sakkis
    Jun 2, 2007
Loading...

Share This Page