sami.strat has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to write (learn) PERL based HTML, and include O/S based variables in my code. For example, check out the following snippet:
#!/bin/perl use strict; use warnings; my $z = system("ps -ef | grep syslog | grep -v grep | wc -l"); use CGI; print CGI::header(); print "<html>"; print "<head><title>Test</title></head>"; print "<body>"; print "<h1>Number of processes - $z</h1>"; print "</body>"; print "</html>";
So, the html portion works separately, the variable works separately. Combining them results in a supernova of galactic proportion.

Any advice, alternatives, hints, etc... would be greatly appreciated.

Thanks

Replies are listed 'Best First'.
Re: html && OS variables
by huck (Prior) on Jul 31, 2017 at 23:55 UTC

    my $z = `ps -ef | grep syslog | grep -v grep | wc -l`;

    system doesnt return the STDOUT instead it returns "the exit status of the program as returned by the wait call.". So the result of the pipes was showing up before any "Content-Type: " output

Re: html && OS variables
by haukex (Archbishop) on Aug 01, 2017 at 07:12 UTC

    huck is correct about what is wrong with using system for this task. The rest of my answer is almost identical to here, including the link to this guide:

    If and only if the external command you want to run is always a fixed string, then one way to do what you want would be:

    use IPC::System::Simple qw/capture/; my $output = capture('ps -ef | grep syslog | grep -v grep | wc -l');

    However, as I said, this is only a good idea for fixed strings, because otherwise you may introduce security issues! I wrote about the topic of running external commands at length here.

Re: html && OS variables
by karlgoethebier (Abbot) on Aug 02, 2017 at 08:17 UTC
    "...alternatives..."

    Avoiding to shell out might be one:

    #!/usr/bin/env perl # $Id: procs.pl,v 1.2 2017/08/02 07:39:39 karl Exp karl $ # http://perlmonks.org/?node_id=1196387 use strict; use warnings; use feature qw(say ); use Proc::ProcessTable; use Data::Dump; my $cmndline = qr(syslog); my @processes = @{ Proc::ProcessTable->new->table }; for my $process (@processes) { say $process->cmndline; } # dd \@processes; my $found = grep { $_->cmndline =~ /$cmndline/ } @processes; say $found; __END__

    See also Proc::ProcessTable.

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Re: html && OS variables
by NetWallah (Canon) on Aug 02, 2017 at 03:06 UTC
Re: html && OS variables
by sami.strat (Novice) on Aug 01, 2017 at 01:55 UTC
    so can someone provide an example, link to accomplish this? TIA

      i did