Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Beginner Question: foreach and $cgi

    Hi,

    I am new to programming Perl & CGI and have just worked through Chapter 9 of Beginning Perl at: http://www.perl.org/books/beginning-perl/

    I wished to modify the code examples to print out all files in a directory and although I have (dubiously) achieved this, I don't understand why the first method below works but second does not.

    This works:
    Code:
    #!/usr/bin/perl
    #list3.plx
    use strict;
    use warnings;
    my @files = </var/www/perl/*>;
    my $file;
    #$| = 1;
    print "Content-type: text/html\n\n";
    print "<html><head><title>Perl File Listing</title></head></body>";
    print "<center><table border=1>";
    #foreach (sort keys %ENV($_) { print "<tr><td>$_</td></tr>" }
    #foreach (sort keys %ENV)a
    foreach $file (@files) 
    {  print "<tr><td>";
       print "$file\n";
       print "</td></tr>";
    }
    print "</table></center></body></html>\n";

    However this does not work (returns error: list2.plx: syntax error at list2.plx line 15, near "foreach ")

    Code:
    #!/usr/bin/perl
    #list2.plx
    # using $cgi-> functions to generate html
    use warnings;
    use strict;
    use CGI::Pretty;
    use CGI::Carp qw(fatalsToBrowser);
    my $cgi=new CGI::Pretty;
    my @files = </var/www/perl/*>;
    my $file;
    print $cgi->header(),
            $cgi->start_html("Perl File Listing"),
            $cgi->table({-border=>1},
            $cgi->Tr($cgi->th(["File"])),
            foreach $file (@files) {$cgi->Tr($cgi->td([$file])), }
            #$cgi->Tr($cgi->td([$file])),
            #map {$cgi->Tr($cgi->td([$_,$ENV{$_}]))} sort keys %ENV
            ),
            $cgi->end_html();
    foreach $file (@files) { print $file . "\n"};
    If someone can explain to me it would be appreciated.

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by randyt View Post
    Hi,

    I am new to programming Perl & CGI and have just worked through Chapter 9 of Beginning Perl at: http://www.perl.org/books/beginning-perl/

    I wished to modify the code examples to print out all files in a directory and although I have (dubiously) achieved this, I don't understand why the first method below works but second does not.

    This works:
    Code:
    #!/usr/bin/perl
    #list3.plx
    use strict;
    use warnings;
    my @files = </var/www/perl/*>;
    my $file;
    #$| = 1;
    print "Content-type: text/html\n\n";
    print "<html><head><title>Perl File Listing</title></head></body>";
    print "<center><table border=1>";
    #foreach (sort keys %ENV($_) { print "<tr><td>$_</td></tr>" }
    #foreach (sort keys %ENV)a
    foreach $file (@files) 
    {  print "<tr><td>";
       print "$file\n";
       print "</td></tr>";
    }
    print "</table></center></body></html>\n";

    However this does not work (returns error: list2.plx: syntax error at list2.plx line 15, near "foreach ")

    Code:
    #!/usr/bin/perl
    #list2.plx
    # using $cgi-> functions to generate html
    use warnings;
    use strict;
    use CGI::Pretty;
    use CGI::Carp qw(fatalsToBrowser);
    my $cgi=new CGI::Pretty;
    my @files = </var/www/perl/*>;
    my $file;
    print $cgi->header(),
            $cgi->start_html("Perl File Listing"),
            $cgi->table({-border=>1},
            $cgi->Tr($cgi->th(["File"])),
            foreach $file (@files) {$cgi->Tr($cgi->td([$file])), }
            #$cgi->Tr($cgi->td([$file])),
            #map {$cgi->Tr($cgi->td([$_,$ENV{$_}]))} sort keys %ENV
            ),
            $cgi->end_html();
    foreach $file (@files) { print $file . "\n"};
    If someone can explain to me it would be appreciated.
    don't close what you don't open,

    Edit: btw, i doubt you can display and see something after you close the html tag, last foreach

    best regards

  • #3
    New to the CF scene
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi oesxyl & thank you for your quick reply.

    I believe that the line you have highlighted is to close the open brackets on line 13: $cgi->table( as script will not run with it removed. I've formatted the code below to hopefully make it a bit clearer.

    Interestingly enough, if I comment out just the foreach $file (@files) line the script will run successfully (and the output from the last line is visible albeit without new lines). But when I uncomment foreach line script returns error.


    Code:
    #!/usr/bin/perl
    #list2.plx
    # using $cgi-> functions to generate html
    use warnings;
    use strict;
    use CGI::Pretty;
    use CGI::Carp qw(fatalsToBrowser);
    my $cgi=new CGI::Pretty;
    my @files = </var/www/perl/*>;
    my $file;
    print $cgi->header(),
            $cgi->start_html("Perl File Listing"),
            $cgi->table(    {-border=>1},
                            $cgi->Tr( $cgi->th(["File"]) ),
                            #foreach $file (@files) 
                            {  $cgi->Tr( $cgi->td([$file]) ), }
                       ),
            $cgi->end_html();
    foreach $file (@files) { print $file . "\n"};

  • #4
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,944
    Thanks
    2
    Thanked 170 Times in 165 Posts
    Code:
    print $cgi->header(),
            $cgi->start_html("Perl File Listing"),
            $cgi->table(    {-border=>1},
                            $cgi->Tr( $cgi->th(["File"]) ),
                            map { join(',', $cgi->Tr( $cgi->td([$_]) )) } @files,
                       ),
            $cgi->end_html();

  • #5
    New to the CF scene
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    resolved: Beginner Question: foreach and $cgi

    Thank you kindly FishMonger. I don't understand why the map function works but I will be doing some further reading. Thanks again.
    Last edited by randyt; 02-01-2011 at 11:57 AM. Reason: update title


  •  

    Posting Permissions

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