Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Running 2 scripts at once

by Dipseydoodle (Acolyte)
on Nov 13, 2012 at 21:50 UTC ( #1003711=perlquestion: print w/ replies, xml ) Need Help??
Dipseydoodle has asked for the wisdom of the Perl Monks concerning the following question:

UPDATE!!!!

I thank you all! I finally broke my self of those horrible habits and have found the wonderfull miracles of the FH, mkdir calls etc... I'm no longer using system()!

Hello dear monks. I am currently working on creating a small Bulletin Board System with simple implimentations such as a Bulletin Board, IM, and the abbility to run the lynx browser. Am am doing this all in PERL and BASH. Untill now everything was going just fine. I have this code to read the current chat which resides in a txt file called chatlog.txt

#!/usr/bin/perl system("clear"); $on = "true"; while ($on = "true") { system("clear"); system("cat chatlog.txt"); system("sleep 3"); }

This works just as expected! BUT, then I have this piece of code!

#!/usr/bin/perl system("clear"); $on = "true"; while ($on = "true") { system("read $in"); system("echo "$in" >> chatlog.txt); }

Now this should work just fine. When I run this kind of thin in bash directly throught the terminal it does, but when I run it from the file it dosen't echo the $in to the txt file. Please try and see for your selves... If somebody could solve this I'd apreciate it.

Comment on Running 2 scripts at once
Select or Download Code
Re: Running 2 scripts at once
by choroba (Abbot) on Nov 13, 2012 at 22:05 UTC
    system("echo "$in" >> chatlog.txt);
    You double quotes are not balanced. Moreover, if you want to use redirection, you cannot use the LIST variant of system.
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Running 2 scripts at once
by muba (Priest) on Nov 14, 2012 at 00:56 UTC

    Also, be aware that while ($on = "true") assigns. If you want to check if $on is equal to some value, you'd use == for numerical comparisons and eq for string comparisons. See perlop. So what you want is while ($on eq "true") .

Re: Running 2 scripts at once
by rjt (Deacon) on Nov 14, 2012 at 01:11 UTC

    Your first example could be reduced to:

    #!/usr/bin/perl system('tail -F chatlog.txt');

    As you might imagine, you don't need a Perl script to run a one-line shell command.

    Your second example isn't syntactically valid (unbalanced quotes), but that may have just been a copy/paste error. However, the more serious problem is that system("read $in") won't go anywhere. read(1) assigns its result to shell variables (Bash, in your case). No process (including Perl) can access shell variables from a subshell, at least not without some additional effort.

    There is a pure Perl way to do what you want to do (in both cases). For the second case, I suggest you look at the Perl documentation for open, print, and I/O Operators, for a basic way to open a text file for append ('>>'), accept lines of input from a user and print those lines to your text file.

    Finally, twice you have used this construct:

    $on = "true"; while ($on = "true") { ... }

    There are two problems with this. First, your condition for while, $on = "true" will always evaluate to TRUE. This is because you are assigning the value "true" to the variable $on every time through the loop, and the assignment operator returns the value assigned. Since you are comparing strings, you would want to use $on eq 'true' to do what you want, which is compare the value of $on to the string 'true'. (If you were comparing numbers, you would use == instead of eq).

    Second, you actually don't need this clumsy construct and extra variable at all. You could equivalently write:

    while (1) { ... }

    Or, probably, once you get the hang of <>, something like below, but don't highlight the text below if you want to figure this out on your own:

    Keep at it, though. It gets logarithmically easier the more you learn. :-)

Re: Running 2 scripts at once
by Anonymous Monk on Nov 15, 2012 at 08:37 UTC

    You seem to be trying to write a Bash script in Perl. You should at least be aware of what happens with system() and what are the rules about subshells.

    But anyway, why don't you write it in Bash instead? It should be less typing. Not sure if there's a bashmonks.org somewhere.

      Actually, funny you say that!

      The original BBS was written in bash originally! however I had to use cd alot and that got really annoying having to figure where to CD everything too! lol

      I guess I'l give bash another shot.

        You can do pretty large systems in Bash, but Perl is much more powerful, saner, safer, and lighter than any shell language once you've learnt how to use it. I will continue to recommend you stick to Bash or maybe Zsh (somewhat more on the sane side than Bash) until you learn Perl enough to not shell out for every small task.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1003711]
Approved by Tanktalus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (4)
As of 2014-10-22 02:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (112 votes), past polls