Mailing List Archive

Support open source code!


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: tlug: Easy (?) perl syntax question



Darren Cook wrote:
> 
> I'm trying to get a cgi variable (called "formnum") into a string.  If
> the variable is not in the input the string should be blank.
> 
> 1.This code gives a server error:
>   $formnum="";
>   if($data{'formnum'} eq "1")$formnum="1";

Hmm let's see this should do it....

   $formnum="";
   $formnum = "1" if $data{'formnum'} eq "1";

if you feel more comfortable with lots of parenthesis you can write it
this way
   $formnum = "1" if ($data{'formnum'} eq "1");

or 
   if ($data{'formnum'} eq "1"){ $formnum = "1" }

they all do the job....

 
> 2.This sets to "1" correctly, but the extension does not get appended
> to $filename. It seems there is something in the $formnum that
> terminates the string early??
>   $formnum=$data{'formnum'};
>  $filename= $BASE . "/" . $formnum . $extension;


Where do you get the value for $extension? Are you overwriting the
$extension somewhere in the script?  
Finding the problem here is a bit more involved. Try to print $extension
to STDOUT. That will show you if $extension is empty or contains data
[1]. 

Since the syntax is correct here you probably overwrote $extension
somewhere earlier in the script. Stuff like...

$extension = s/.*(\..*)$/$1/; # $extension contains 1 now

instead of
$extension =~ s/.*(\..*)$/$1/; #$extension contains e.g. ".html" 
           ^^
...could be the reason here.


# [1] checking $extension 
$formnum=$data{'formnum'};
print "extension = $extension<br>"; 
$filename= $BASE . "/" . $formnum . $extension; 

> I'm obviously misunderstanding something fundamental here. Can anyone
> tell me what it is?
> (To add to that, I went through the perl online manuals, and as far as I
> can see my syntax is correct. So now I'm really confused!)

Sometimes the solution somewhere totally different then you expect it. I
spent hours trying to fix the wrong subs in a scripts and the problem
was somewhere else (it was a Monday morning, though ;)

hope that helps...

urs

-- 
u r s  r o e s c h             h a c h i o j i,  t o k y o
----------------------------------------------------------
e# urs@example.com                   aim# uru uru 71
w# http://www.digital-mines.com              icq# 13780972
---------------------------------------------------------------
Next Nomikai: 20 November, 19:30 Tengu TokyoEkiMae 03-3275-3691
Next Meeting: 12 December, 12:30 Tokyo Station Yaesu central gate
---------------------------------------------------------------
Sponsor: PHT, makers of TurboLinux http://www.pht.co.jp

Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links