LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-26-2007, 11:07 PM   #1
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Rep: Reputation: 46
cgi perl and iterating


Hi,

I have some list elements that I'd like to be iterated and turned into HTML elements, like an ordered list.

For example, given the following, is it possible to extend the same code to run over the list and print the list elements?

Code:
print ol(li(a({-href="foo"},"bar"))),"\n";
I've tried something like:
Code:
print "<ol>";
foreach my $s (@some_list) {
print "<li><a href="$s">$s</a></li>";
}
print "</ol>";
, but that doesn't do it.

Any ideas?
Thanks.
 
Old 11-27-2007, 03:07 AM   #2
j-ray
Senior Member
 
Registered: Jan 2002
Location: germany
Distribution: ubuntu, mint, suse
Posts: 1,591

Rep: Reputation: 145Reputation: 145
your code contains " double qoutes that are not escaped within a string. Maybe this ws the problem?
if not
you may do the counting
my $counter=1;
foreach my $s (@some_list) {

print "<li><a href='$s'>$counter"."."." $s</a></li>";
$counter++;
}

Last edited by j-ray; 11-27-2007 at 03:10 AM.
 
Old 11-27-2007, 03:53 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
Code:
use CGI qw/:standard/;
use CGI::Pretty;


@L = qw(one two three four);

@P = map {a({-href=>$_}, $_)} @L;     # make a list of anchors

print li(\@P);                        # pass  array ref to li
Code:
billym.primadtpdev>1.pl
<li>
        <a href="one">one</a>
</li>
<li>
        <a href="two">two</a>
</li>
<li>
        <a href="three">three</a>
</li>
<li>
        <a href="four">four</a>
</li>

owzthat

Code:
print ol( li(\@P));  # for the complete job

Last edited by bigearsbilly; 11-27-2007 at 08:01 AM.
 
Old 11-27-2007, 09:55 AM   #4
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Original Poster
Rep: Reputation: 46
Thanks, you two.
They both would do, and I like the map usage.

My error is actually someplace before that block. I don't think I'm returning values correctly from a function. I'm executing a script using open(), which ends up writing it to STDOUT, and I don't seem to be getting those return values back in my caller.

Still got some way to go ...
Thanks, again.
 
Old 11-27-2007, 05:07 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,360

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Show us the code...
sounds like one of these may be useful: http://perldoc.perl.org/IPC/Open2.html , http://perldoc.perl.org/IPC/Open3.html
 
Old 11-28-2007, 02:13 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
generic open reading...
Code:
open INPUT, "command |" or die;

while (INPUT) {
 
   # process $_ here
}
 
Old 11-28-2007, 10:08 AM   #7
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Original Poster
Rep: Reputation: 46
Thanks again, guys. I'll finally get some time today to understand more how things are to be done in Perl.
This is what I've been trying so far:

Code:
sub retTest {
  my $cmd = "|/absolute/path/to/the/command";
  my @results = ( );
  my @CMD = ( );

  open(CMD, "$cmd some_arg") or die("Can't fork command: $!\n");
  @results = @CMD;
  close CMD;

  return @results;
}

sub printTest {
  print "Make a selection:\n";
  my @results = &retTest();

  #using bigearsbilly's suggestion
  my @a = map {a({-href=>$_}, $_)} @results;
  print ol(li(\@a));
}

&printTest();
I certainly feel queasy with the line:
Code:
@results = @CMD;
, and am going to try your suggestions.

We shall see ...
Thanks, again.

Last edited by h/w; 11-28-2007 at 10:11 AM.
 
Old 11-28-2007, 10:38 AM   #8
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Original Poster
Rep: Reputation: 46
Code:
sub retTest {
  my $cmd = "|/absolute/path/to/the/command";
  my @results = ( );
  my @CMD = ( );

  open(CMD, "$cmd some_arg") or die("Can't fork command: $!\n");
  while(<CMD>) {
    push(@results, $_);
  }
  close CMD;

  return @results;
}
Better, but the output of the command writes to STDOUT, and nothing is pushed onto @results.
 
Old 11-28-2007, 11:16 AM   #9
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Original Poster
Rep: Reputation: 46
Code:
sub retTest {
  my $cmd = "/absolute/path/to/the/command";
  my @results = `$cmd some_arg`;
  return @results;
}
, captures the output. So, the backticks suit my requirement.

But what if we want to make certain that it doesn't fail - there isn't a way to 'die' gracefully when using back-ticks, is there?
And, what would the open(),open2(),open3() equivalent be?

Thanks, again.
 
Old 11-28-2007, 11:59 AM   #10
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Original Poster
Rep: Reputation: 46
Quote:
Originally Posted by h/w View Post
But what if we want to make certain that it doesn't fail - there isn't a way to 'die' gracefully when using back-ticks, is there?
Code:
sub retTest {
  my $cmd = "/absolute/path/to/the/command";
  my @results = `$cmd some_arg` or die("$!\n");
  return @results;
}
 
Old 11-28-2007, 11:00 PM   #11
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Original Poster
Rep: Reputation: 46
Ok, I've hit a bit of a snag.
If I run that script using 'perl -w script.cgi', I see the output of the command that was executed inside the script.
But if I hit up the webpage from a browser, it doesn't seem to be running the command (or if it is, it doesn't show up.)

Strange ...
 
Old 11-29-2007, 02:54 AM   #12
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
for debugging only!
Code:
use diagnostics;
use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
check /var/log/apache/error_log (or similar)
 
Old 11-29-2007, 02:55 AM   #13
j-ray
Senior Member
 
Registered: Jan 2002
Location: germany
Distribution: ubuntu, mint, suse
Posts: 1,591

Rep: Reputation: 145Reputation: 145
maybe a "permission denied" error as the cgi is run by the web user? ->error_log
 
Old 11-29-2007, 12:15 PM   #14
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Original Poster
Rep: Reputation: 46
Thanks again, guys.
My fault for posting without any details or without checking things out.

The issue had to do with relative paths being used in the external shell script, which I had to make absolute when running through httpd.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl CGI:Can't locate CGI.pm supermyself Programming 13 09-10-2007 06:22 AM
CGI/Perl indienick Programming 2 03-06-2006 03:16 PM
perl cgi neil Programming 3 07-07-2004 04:52 AM
cgi perl : I cant get perl to append my html file... the_y_man Programming 3 03-22-2004 05:07 AM
cgi+perl barbanero General 0 04-07-2002 12:48 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:33 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration