Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: A problem with variables inside a system() call

by jarich (Curate)
on May 14, 2004 at 01:51 UTC ( [id://353251]=note: print w/replies, xml ) Need Help??


in reply to A problem with variables inside a system() call

Do you get the same kind of error if you rewrite your statement?
if($variable < $threshold) { my $command = '/usr/bin/blah'; my $updatestring = << "EOF"; $variable is the value from DB. $variable is less that $threshold EOF my $UPDATE = `$command $updatestring`; } else { ... }
Using this extra variable corrects some issues with your here document and should make the code much more readable.

Does it also fix the error?

I would be intersted in knowing how it's going wrong in the first place... do you have a code example that reproduces the error reliably?

One final thing. In some responses above you suggest you will use system instead. I'm sure you know the difference between using backticks and system, but I thought I should point it out anyway.

system returns $? which you can use to determine success or failure (basically if it's true your command failed). If the command called via system prints to STDOUT then that content will go to the STDOUT of your program. Backticks capture the STDOUT from the command and return that. If you call backticks in a list context each new line of data is returned in each list element. If you call it in scalar context you get a string with embedded newlines.

If you want to give up on backticks but still capture the output of the command then perhaps you might want to use:

open ( COMMAND, " $command $updatestring | ") or die $!; while(<COMMAND>) { .... }

I hope this helps

jarich

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://353251]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (2)
As of 2024-04-24 23:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found