CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
PERL interpreter misunderstand UNIX $

 



NAS
New User

Oct 13, 2017, 3:56 AM

Post #1 of 9 (2419 views)
PERL interpreter misunderstand UNIX $ Can't Post

Hi everybody,

I am a beginner in PERL scripting and I am facing an issue to finalise my first script. The aim is to launch the .pl from UNIX and to implement in the script a command shell that highlight few lines of a text to prompt.

SCRIPT-------------------------------------------------------------------------------------------
#! /usr/bin/perl
use strict;
use warnings;

my $x=qx(<piped shell commands> | egrep --color '.*<pattern>.*|$');
print "$x";
----------------------------------------------------------------------------------------------------

ERROR--------------------------------------------------------------------------------------------
Use of uninitialised value $' in concatenation (.) or string.
----------------------------------------------------------------------------------------------------


Even thought the command shell is in back-quotes the $ is not interpreted as command shell (until the end of the line) but as a PERL character (waiting for variable name). I have tried many solutions as use : system, regex, different typos ...
Nothing to do.

Then, is there a way to say to PERL that this $ belongs to the shell command ?

Thank you a lot for your help.


(This post was edited by NAS on Oct 13, 2017, 4:41 AM)


Laurent_R
Veteran / Moderator

Oct 13, 2017, 9:56 AM

Post #2 of 9 (2408 views)
Re: [NAS] PERL interpreter misunderstand UNIX $ [In reply to] Can't Post

This should not happen with single quotes, which you seem to be using.

Otherwise, one way to insert a literal $ in a string is to escape it, i.e. to prefix it with a backslash.

I would suggest that you construct your shell command as a string in a variable, and pass that variable to the qx// construct. This way, you can print that variable (before calling the shell) and make sure is contains what you need to pass to the system.

Finally, Perl is a fairly powerful language, you really rarely need to shell out. Do what you have to do with Perl if and when you can, don't shell out unless you really have to. The code you've shown is not complete, so I can't really figure out what it does, but it is quite possible that you can do it in pure Perl.


BillKSmith
Veteran

Oct 13, 2017, 2:12 PM

Post #3 of 9 (2402 views)
Re: [NAS] PERL interpreter misunderstand UNIX $ [In reply to] Can't Post

The documentation for the qx operator states:
<quote>
qx/STRING/
`STRING`
A string which is (possibly) interpolated and then executed as a system command ...
</quote>

In your case, $' is recognized as the system variable $PREMATCH and interpolated before the command is executed. $' is uninitialized because you have not done anything to set it, hence the warning. You can remove the special meaning of the dollar sign by escaping it with a backslash. Taking Laurent's suggestion, we can see the difference.

Code
C:\Users\Bill\forums\guru>type nas.pl 
use strict;
use warnings;

my $string = 'pre:post';
$string =~ m/:/; # Set $' to 'post'
print $', "\n";
my $shell_command_1
= qq(<piped shell commands> | egrep --color '.*<pattern>.*|$');
print $shell_command_1, "\n";
my $shell_command_2
= qq(<piped shell commands> | egrep --color '.*<pattern>.*|\$');
print $shell_command_2, "\n";

C:\Users\Bill\forums\guru>perl nas.pl
post
<piped shell commands> | egrep --color '.*<pattern>.*|post
<piped shell commands> | egrep --color '.*<pattern>.*|$'

Good Luck,
Bill


NAS
New User

Oct 14, 2017, 12:58 AM

Post #4 of 9 (2392 views)
Re: [Laurent_R] PERL interpreter misunderstand UNIX $ [In reply to] Can't Post

Hi Laurent,

You well understand what I need to do : check the contain of the command shell to pass to the system (and print in terminal script output).

I tried also before sending this post to escape the $ using a backslash. Unfortunately the shell will only color the <pattern> but not the full line containing the <pattern>.

As an add-on I want to say that this .pl script will help me through a shell alias to make a personalized monitoring status of all my LSF jobs on different fair share resources and to kill some jobs if wanted in a parallel way. Since I am always working from shell I need to feedback to shell the outputs of my .pl to monitor sometimes the progress of my targets and kill some to run on less saturated queues.

PS: trying to understand BillKSmith script and will come with a feedback on Monday.

Thank you both for your precious time !


BillKSmith
Veteran

Oct 14, 2017, 8:49 AM

Post #5 of 9 (2378 views)
Re: [NAS] PERL interpreter misunderstand UNIX $ [In reply to] Can't Post

Perhaps a summary of my previous post will help you.

Given the documentation of the operator qx, you should expect the warning message that you posted. Escaping the '$' with a '\' fixes that problem.

You now report that you already tried that, but then had a problem with color. I suspect that this is a UNIX problem. Does your command work as expected if you type it on the command line?

I do not have any practical experience to offer, but I did google a man page for egrep. The meaning of --color depends on environmental variables (GREP_COLOR and GREP_COLORS). Are they set correctly?
Good Luck,
Bill


NAS
New User

Oct 17, 2017, 7:05 AM

Post #6 of 9 (2369 views)
Re: [BillKSmith] PERL interpreter misunderstand UNIX $ [In reply to] Can't Post

Hello BillKSmith,

yes this the following command works fine on UNIX :

<piped shell commands> | egrep --color '.*<pattern>.*|$'

and the environment variable are set by default to generate red colour.

I tried something else functionning under UNIX. I was surprised also that it does not function on PERL:

<piped shell commands> | egrep --color '.*<pattern>.*|^'

using this syntax without $ is an improvement since the semantic checker of PERL colour the ^ in the same colour as the rest of the command which I wanted to pass to the system (see attached file).

Then I keeping this issue in mind while doing other staff. Let you know when I get the solution.

PS: sorry to be late for answering your asking.

Thank you again for your time.
NAS
Attachments: Capture_PERL_Dollar_issue.PNG (105 KB)


BillKSmith
Veteran

Oct 17, 2017, 11:45 AM

Post #7 of 9 (2363 views)
Re: [NAS] PERL interpreter misunderstand UNIX $ [In reply to] Can't Post

Your semantic checker is telling you the same thing that I have in my last two posts. Perl interprets $' as a variable (the special variable $POSTMATCH). Use the backslash! Your checker will confirm that this problem is fixed. The remaining problem is not a perl problem. I understand that your command works from the command line, but not from perl. This strongly suggests that the environment is not the same. The usual suspects are environmental variables and permissions. I am not able to offer any more help.
Good Luck,
Bill


NAS
New User

Oct 18, 2017, 6:12 AM

Post #8 of 9 (2351 views)
Re: [BillKSmith] PERL interpreter misunderstand UNIX $ [In reply to] Can't Post

Hi BillKSmith,

I do not know if my last post has gone. Regardless, I get around the issue of passing the full command (<piped shell commands> | egrep --color '.*<pattern>.*|$' ) to the system by doing the last part after the pipe in few PERL command lines :

SCRIPT--------------------------------------------------------------------------------
use Term::ANSIColor qw(:constants);

my @array=qx(<piped shell commands>);

my $size_array = @array;
my (@index)=grep {$array[$_] =~ /<pattern>/} (0 .. $#array);

my $i=0;
while ($i <= $size_array-1) {
if ($i != $index[0]) {
print "$array[$i]";
}
else {
print BOLD, RED, "$array[$i]", RESET;
}
$i++;
}
-----------------------------------------------------------------------------------------

I grant you this method is not the most elegant. Besides, I remain open mind to any other proposals.

Thank you again.
NAS


BillKSmith
Veteran

Oct 18, 2017, 7:44 AM

Post #9 of 9 (2349 views)
Re: [NAS] PERL interpreter misunderstand UNIX $ [In reply to] Can't Post

I should have thought of that myself. I think that it is a big improvement to use perl's grep rather than the shell's egrep.
Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives