perl scalar variable in backquoted string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl scalar variable in backquoted string
# 1  
Old 10-04-2007
perl scalar variable in backquoted string

hi

I've been searching all over the internet to simply do the following:

Code:
$tempfile = "/usr/school/tempfile.dat";

$myvar = param('add'); ###add is the variable assigned to a popup menu

`ls -l $myvar * >> $tempfile` ###I also tried `ls -l ${myvar}* >>$tempfile`

open(ADDLIST, "<tempfile");
@addvar = (" ", <ADDLIST>, $myvar);
unlink("$tempfile");

return @addvar

Lets say if $myvar = "ab"
the output lists all the contents of ls -l and the string ab. But what I want is to see the list of ls -l ab* and the string ab.

Can someone please help me, thanks!

Last edited by mehdi9; 10-04-2007 at 05:12 PM..
# 2  
Old 10-04-2007
ok so I tried the echo function:

Code:
`echo my variable $myvar >> $tempfile`;

unforutunatly nothing comes up, not even the "my variable" part

I tried the following

Code:
$myvar2 = "abcdefghi";

`echo my variable two is $myvar2 >> $tempfile`;

and it comes up in the array.

so my question is, how can I gather the exact word the 'add' popup menu is selected on?

if you look at the array @addvar, the entry $myvar (which equals to param('add')) comes up, however when used in the backquoted string it doesn't work. Can someone please clarify why this is. Many thanks.
# 3  
Old 10-04-2007
Quote:
Originally Posted by mehdi9
ok so I tried the echo function:

Code:
`echo my variable $myvar >> $tempfile`;

The documentation says:

Quote:
A string which is (possibly) interpolated and then executed as a system command with /bin/sh or its equivalent. Shell wildcards, pipes, and redirections will be honored. The collected standard output of the command is returned; standard error is unaffected.
So the output will not be automatically printed. You have to print() it.

For the rest of your question, I do not quite understand the exact question you have. I have tried executing ls -l with wildcards with backticks but it is working. Please consider rephrasing your question and give us further elaborations.
# 4  
Old 10-05-2007
ok my real work does not involve wildcard or anything, the following is what I am doing:

I am making webpage using cgi, I have a popup menu named "add" and in the menu it has a list of filenames.

In each of these files that are listed, there is another list containing information on a network. These files are updated daily.

so I made a submit button, when it is pressed; right next to it a new popup menu appears. The contents in the popup menu is the list of network data stored in file that was first selected on the "add" popup menu.

So my biggest dellima is to find out what filename was selected in the add popup menu. Than I want to use the filename, put it into a directory format than access it and using pg for example I would gather all the network information. From there I put the network information in an array and refrence it to the new popup menu under the -value item.

So right now this is something similar to what I am doing:

Code:
sub getValues {

$myvar2 = param('add');

`ls -l >> $tempfile`;
`pg  $myvar2 >> $tempfile`;
`echo hello there >> $tempfile`;

open(ADDLIST, "<tempfile");
@addvar = (" ", <ADDLIST>, $myvar2);
unlink("$tempfile");

return popup_menu(-name=>'add2',-values=>\@addvar,-default=>'');

}

when I run the code I would be able to see the following values in the new popup menu:

the contents of ls -l
hello there
the value of $myvar2

however I can not see the list from $myvar2 itself. I do not understand why this is.

I also tried to explicitly state a filename instead of $myvar2 and it works perfectly well. Looking forward to your solutions, and many thanks again.
# 5  
Old 10-05-2007
i suggest you use perl's native method of listing files eg readdir(), opendir()...instead of calling external shell commands. You can use format to format your output. For more info: perldoc -f opendir, perldoc -f readdir , perldoc -f format
# 6  
Old 10-05-2007
Quote:
Originally Posted by mehdi9
however I can not see the list from $myvar2 itself. I do not understand why this is.

I also tried to explicitly state a filename instead of $myvar2 and it works perfectly well. Looking forward to your solutions, and many thanks again.
Then this is more of a Perl CGI question. It sounds like it has nothing to do with backticks at all, so have you put in some debug statements to test if $myval2 is always empty or actually contains the value you expect as you run it in browser? I suppose you are referring to a form parameter 'add' retrieved via CGI->param() method. In case this is true, as I do not see the HTML which triggered the CGI script in action, it will be impossible to tell what the problem could be. The HTML (or in more complex cases, Javascript) will certainly affect how form parameters are being sent. So, to create a Web-based application, you cannot blindly use Perl (or other language)'s API to generate controls without understanding HTML, and you must verify the generated HTML to ensure they work in the way you expect.

Hopefully I did not misunderstand your question, but I guess you should probably look up some primer on CGI programming to help you get a more concrete idea of what you need to do.
# 7  
Old 10-09-2007
okay

so I finally solved the problem,

essentially cgi adds hidden html tags that need to be decoded and removed/subsituted using regular expressions.

one needs to translate the cgi variable name, then compress multiple <P> tags. Turn off all html tags, remove unneeded carriage returns...etc.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Check if string variable is a subset of another string variable

Below is my ksh shell script where I need to check if variable fileprops is a subset of $1 argument. echo "FILE PROPERTY: $fileprops" echo "PARAMETER3: $1" if ; then echo "We are Good. $line FILE is found to be INTACT !! " else echo... (2 Replies)
Discussion started by: mohtashims
2 Replies

2. Shell Programming and Scripting

Perl : converting file to different scalar elements

I have a text file containing 2 exec statements as below and trying to store the below 2 execs into 2 different scalar variables in perl. /* ICD Dist, Total */ /* need to export to Excel, sheet=ICD_Dist__Total */ exec( 'select sum(count(*)) cast(count(*)*100.0/sum(count(*)) over() as... (7 Replies)
Discussion started by: scriptscript
7 Replies

3. Shell Programming and Scripting

PERL : pattern matching a string stored in a variable

I have two variables, my $filename = "abc_yyyy_mm_dd.txt"; my $filename1 = " abc_2011_11_07.txt"; I need to perform some operations after checking if $filename has $filename1 in it i have used the below code, if($filename =~ /^$filename1/) { ---- -- } (2 Replies)
Discussion started by: irudayaraj
2 Replies

4. Shell Programming and Scripting

String variable to numeric conversion in perl

Hi guys I am having this strange issue.Well my requirement is like below Compare two values between flat file and oracle DB Via perl script I am easily getting the rowcount Now I connect sql plus via perl and the column value that returns is string my $sqlplus_settings = ''; my... (7 Replies)
Discussion started by: Pratik4891
7 Replies

5. Shell Programming and Scripting

Problem with KSH script using scalar variable

Hi Guys, I'm having a hard time bringing out my desired output from my korn shell script. This particular statement from my script its seems not working perl -ne 'print if $_ lt ${date1}' . My complete script as shown below. Please help. Code: #!/usr/bin/ksh ... (5 Replies)
Discussion started by: victorneri
5 Replies

6. Shell Programming and Scripting

perl DBI: populate a scalar from a select statement

hi every resource i see regarding DBI refers to retrieving data from a database into and array or a hash, but i havent seen anything on how to pull out a single value to a scalar in my database i have a field called "forcewrite" with a value of "6". I am trying to connect to the database,... (2 Replies)
Discussion started by: hcclnoodles
2 Replies

7. UNIX for Advanced & Expert Users

Scalar i500 can see tape drives but not autochanger

We have a new i500 and have 4 TLO4 drives which are connected directly to 2 FC cards on the host. The host can communicate with the autochanger through whichever drive that is set as control path. When I run inquire, all 4 drives are listed but auto changer doesn't show up. Does anyone know... (1 Reply)
Discussion started by: tecky
1 Replies

8. Shell Programming and Scripting

scalar variable assignment in perl + { operator

When reading over some perl code in a software document, I came across an assignment statement like this $PATH = ${PROJECT}/......./.... In this particular form of scalar variable assignment, what does the curly braces operators do ? Also, what is the benefit in doing scalar assignment this... (3 Replies)
Discussion started by: JamesGoh
3 Replies

9. Shell Programming and Scripting

Can't modify not in scalar assignment compilation error

My perl code is below. Its just a simple assignment and printing out of the value. $match = "my name is"; print "printing $match\n" ; Does anyone know where is the error? (2 Replies)
Discussion started by: new2ss
2 Replies

10. Shell Programming and Scripting

Query string assingment for other variable in PERL

Hi friends, I want to use query string value to another variable but it gives following error when I use it with print out command, Insecure dependency in open while running with -T switch at C:/Program Files/Apache Group/Apache2/cgi-bin/cdr_findere.pl line 119. My code as follows, my... (0 Replies)
Discussion started by: maheshsri
0 Replies
Login or Register to Ask a Question