Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why does this code not work? 1

Status
Not open for further replies.

lcs01

Programmer
Aug 2, 2006
182
US
Hi, Experts,

Here is a small piece of my perl code:

Code:
use strict;
use CGI::Pretty qw( :html3 );

my $q = new CGI::Pretty;
my @arr = ('a', 'b', 'c', 'd');

my (@cArr, %cLable);
my $select = "Pick One";
push @cArr, "'$select'";
$cLable{$select} = "0";
my $i = 1;
foreach (@arr) {
  push @cArr, "'$_'";
  $cLable{$_} = $i;
  $i++;
}
[COLOR=blue]my $dropdown = qq/$q->popup_menu({-name=>'cname',-values=>\@cArr,-default=>"$select",-labels=>\%cLable});/;[/color]
[COLOR=red]my $dropdownInHTML = eval($dropdown);
print "The output of 'eval()': #$dropdownInHTML#\n";
[/color]

The code above is named as 'tt.pl'. And here is the output:
Code:
% ./tt.pl
The output of 'eval()': ##

Why does 'eval()' not return a string of html code, somewhat like this:

Code:
<select ......>
...
...
</select>

I guess there must be something wrong in the line highlighted with blue color. But I don't know what it is.

Thank you for your help.
 
You haven't escaped the $q variable. Or escaped the the backslashes or escaped the other variables.

But why bother using eval in the first place? Any of the following would work

Code:
[gray][i]# Double Quoting[/i][/gray]
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$dropdown[/blue] = [red]qq{[/red][purple]\$q->popup_menu({-name=>'cname',-values=>[purple][b]\\[/b][/purple]\@cArr,-default=>"\$select",-labels=>[purple][b]\\[/b][/purple]\%cLable});[/purple][red]}[/red][red];[/red]
[black][b]my[/b][/black] [blue]$dropdownInHTML[/blue] = [url=http://perldoc.perl.org/functions/eval.html][black][b]eval[/b][/black][/url][red]([/red][blue]$dropdown[/blue][red])[/red][red];[/red]

[gray][i]# Single Quoting[/i][/gray]
[black][b]my[/b][/black] [blue]$dropdown[/blue] = [red]q{[/red][purple]$q->popup_menu({-name=>'cname',-values=>\@cArr,-default=>"$select",-labels=>\%cLable});[/purple][red]}[/red][red];[/red]
[black][b]my[/b][/black] [blue]$dropdownInHTML[/blue] = [black][b]eval[/b][/black][red]([/red][blue]$dropdown[/blue][red])[/red][red];[/red]

[gray][i]# No Eval[/i][/gray]
[black][b]my[/b][/black] [blue]$dropdownInHTML[/blue] = [blue]$q[/blue]->[maroon]popup_menu[/maroon][red]([/red][red]{[/red]-[purple]name[/purple]=>[red]'[/red][purple]cname[/purple][red]'[/red],-[url=http://perldoc.perl.org/functions/values.html][black][b]values[/b][/black][/url]=>\[blue]@cArr[/blue],-[purple]default[/purple]=>[red]"[/red][purple][blue]$select[/blue][/purple][red]"[/red],-[purple]labels[/purple]=>\[blue]%cLable[/blue][red]}[/red][red])[/red][red];[/red]

- Miller
 
Your welcome.

Please note though that there are other bugs in your code. The labels that you created won't be used because their values do you match the values you assigned to @cArr. I would suggest that you do something like the following to create this html elment.

Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]CGI::Pretty[/green] [red]qw([/red][purple] :html3 [/purple][red])[/red][red];[/red]

[black][b]use[/b][/black] [green]strict[/green][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$q[/blue] = new CGI::Pretty[red];[/red]

[black][b]my[/b][/black] [blue]@cArr[/blue] = [maroon]hashKeys[/maroon][red]([/red][black][b]my[/b][/black] [blue]%cLabel[/blue] = [red]([/red]
	[fuchsia]0[/fuchsia]	=> [red]"[/red][purple]Pick One[/purple][red]"[/red],
	[fuchsia]1[/fuchsia]	=> [red]"[/red][purple]a[/purple][red]"[/red],
	[fuchsia]2[/fuchsia]	=> [red]"[/red][purple]b[/purple][red]"[/red],
	[fuchsia]3[/fuchsia]	=> [red]"[/red][purple]c[/purple][red]"[/red],
	[fuchsia]4[/fuchsia]	=> [red]"[/red][purple]d[/purple][red]"[/red],
[red])[/red][red])[/red][red];[/red]

[black][b]my[/b][/black] [blue]$dropdownInHTML[/blue] = [blue]$q[/blue]->[maroon]popup_menu[/maroon][red]([/red][red]{[/red]
	-[purple]name[/purple]		=> [red]'[/red][purple]cname[/purple][red]'[/red],
	-[url=http://perldoc.perl.org/functions/values.html][black][b]values[/b][/black][/url]		=> \[blue]@cArr[/blue],
	-[purple]default[/purple]	=> [fuchsia]0[/fuchsia],
	-[purple]labels[/purple]		=> \[blue]%cLabel[/blue],
[red]}[/red][red])[/red][red];[/red]

[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple]The output is: #[blue]$dropdownInHTML[/blue]#[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]

[url=http://perldoc.perl.org/functions/sub.html][black][b]sub[/b][/black][/url] [maroon]hashKeys[/maroon] [red]{[/red]
	[url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]Incorrect number of values[/purple][red]"[/red] [olive][b]if[/b][/olive] [blue]@_[/blue] [blue]%[/blue] [fuchsia]2[/fuchsia][red];[/red]
	[blue]@_[/blue] ? [red]([/red][url=http://perldoc.perl.org/functions/map.html][black][b]map[/b][/black][/url] [red]{[/red][blue]$_[/blue][red][[/red][fuchsia]2[/fuchsia][blue]*$_[/blue][red]][/red][red]}[/red] [red]([/red][fuchsia]0..[/fuchsia][red]([/red][blue]@_[/blue]/[fuchsia]2[/fuchsia]-[fuchsia]1[/fuchsia][red])[/red][red])[/red][red])[/red] : [red]([/red][red])[/red][red];[/red]
[red]}[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[/ul]
Core (perl 5.8.8) Modules used :
[ul]
[li]CGI::pretty - module to produce nicely formatted HTML code[/li]
[/ul]
[/tt]

- Miller
 
I think maybe your code is missing an http header Miller.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Thanks Kev. It's just a code snippit meant to replace the code snippit that he provided.

- Miller
 
ahh, my bad [smile]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Thank you again, Miller.

Though I have already noticed the bugs and made changes before I read your posts, your code is indeed sleek. I love it.
 
Well, if you can minimize the number of value definitions that is always to your benefit. It obviously minimizes potential bugs, and also just makes coding faster and easier to read.

I would change one thing in my final code suggestion though. Instead of die'ing, the hashKeys function should actually Carp::croak. A die is rather uninformative, but a croak would let you know exactly where the error in expected parameters was.

I would also consider renaming the function to orderedHashKeys, but that may be too long of a name for you to want to type it reguarly. Irregardless, good luck with your coding.

- Miller
 
Thank you so much, Miller.

But I have one more question:

Why do we need to check if @_ % 2; in hashKeys()? Would this be always true?

Thanks again.
 
% is the modulus operator. We want to enforce the passing of key-value pairs. @ % 2 will return true iff the number of parameter is odd, and so therefore we die (or croak) to report the error.

- Miller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top