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:
Strange line in the code and not declared variable

 



digioleg54
User

Apr 20, 2017, 12:39 PM

Post #1 of 14 (3267 views)
Strange line in the code and not declared variable Can't Post

I have a line

Code
my ($:)=($ENV{LOG_HOME}||$ENV{OUTPUT_DIR}||".")."/".uc($pid).".PIN"; 

`echo $short_desc > $f_pin`;



Code
$f_pin


Was never declared. Does it means that it automatically takes value of

Code
 $:

?

And also, what such line means?

Thanks


(This post was edited by digioleg54 on Apr 20, 2017, 12:39 PM)


FishMonger
Veteran / Moderator

Apr 20, 2017, 12:52 PM

Post #2 of 14 (3260 views)
Re: [digioleg54] Strange line in the code and not declared variable [In reply to] Can't Post

DON'T assign $: like that. $: is one of Perl's built-in global vars.

Is $short_desc a shell var or is it a perl var which was declared and assigned earlier in your script?

Is $f_pin supposed to be a shell var or a perl var?

If it's a perl var, then you need to declare it with the my keyword.


(This post was edited by FishMonger on Apr 20, 2017, 12:52 PM)


FishMonger
Veteran / Moderator

Apr 20, 2017, 12:55 PM

Post #3 of 14 (3258 views)
Re: [digioleg54] Strange line in the code and not declared variable [In reply to] Can't Post

Why are you shelling out (via the ` ` backtics) to the echo command?

So far, I've never heard of a good reason to do that.


digioleg54
User

Apr 20, 2017, 1:06 PM

Post #4 of 14 (3256 views)
Re: [FishMonger] Strange line in the code and not declared variable [In reply to] Can't Post

I just make shell to Perl conversion, where shell starting Perl. And this was existing on UNIX Perl scripts. Now I make them work as CGI on apache. That is why I am looking to convert them. $short_desk was declared and created in the other Perl script , which is using sub from package get_pin.

Code
$short_desc= ctm="14:46:17" pid="13024" sleep4="20" until="22:00:00" 

if ($short_desc=~/(sleep4)|(sleep[\s\-]for)|(until)/) {
my ($:)=($ENV{LOG_HOME}||$ENV{OUTPUT_DIR}||".")."/".uc($pid).".PIN";
`echo $short_desc > $f_pin`;
}


Both are Perl variables, but $f_pin was never declared, I checked it. May be in our packages, I will double check it now

What should I use instead?

Thanks


(This post was edited by digioleg54 on Apr 20, 2017, 1:08 PM)


digioleg54
User

Apr 20, 2017, 1:13 PM

Post #5 of 14 (3251 views)
Re: [FishMonger] Strange line in the code and not declared variable [In reply to] Can't Post

$f_pin was never declared. Perl for UNIX was created 16 years ago. We are using now version 5.22, but in reality nobody wants to install full version. I just copy packages, which I need and use theem on apache. BTW, was trying to get POSIX package, but it says that POSIX object version 1.53_01 does not match $POSIX::VERSION 1.65 Wher can I get this version, I don't know. And POSIX::strftime::GNU does not work


FishMonger
Veteran / Moderator

Apr 20, 2017, 1:16 PM

Post #6 of 14 (3248 views)
Re: [digioleg54] Strange line in the code and not declared variable [In reply to] Can't Post


Code
my $short_desc = q/ctm="14:46:17" pid="13024" sleep4="20" until="22:00:00"/; 

if ($short_desc =~ /(sleep4)|(sleep[\s\-]for)|(until)/) {
my $path = ($ENV{LOG_HOME}||$ENV{OUTPUT_DIR}||".");
my $file = $path . "/" . uc($pid) . ".PIN";
my $f_pin = $short_desc;
}


If any of those 3 vars inside the conditional block need to be used elsewhere in the script, then they will need to be declared prior to the block and assigned values inside the block.


digioleg54
User

Apr 20, 2017, 1:23 PM

Post #7 of 14 (3245 views)
Re: [FishMonger] Strange line in the code and not declared variable [In reply to] Can't Post

why do we need my $file then? don't you think, that it would the file *.PIN, where we have to put $short_desc?we don't use $f_pin nowhere in the script


FishMonger
Veteran / Moderator

Apr 20, 2017, 1:25 PM

Post #8 of 14 (3243 views)
Re: [digioleg54] Strange line in the code and not declared variable [In reply to] Can't Post


In Reply To
$f_pin was never declared. Perl for UNIX was created 16 years ago. We are using now version 5.22, but in reality nobody wants to install full version. I just copy packages, which I need and use theem on apache. BTW, was trying to get POSIX package, but it says that POSIX object version 1.53_01 does not match $POSIX::VERSION 1.65 Wher can I get this version, I don't know. And POSIX::strftime::GNU does not work


Sounds like you have a really messed up system.

Based on your comment here and other ones in some of your other threads, it's not worth my time to help you mickey mouse things together just because you and your server admins don't want to properly manage the server.


digioleg54
User

Apr 20, 2017, 1:26 PM

Post #9 of 14 (3242 views)
Re: [FishMonger] Strange line in the code and not declared variable [In reply to] Can't Post

I also had a line

Code
#$_ = `head -1 $finfo`;


instead I put:

Code
($_) = (my $firstline) = path($finfo)->lines( { count => 1 }); 
(/(recs_processed\=\")(\d+)(\")/ || /(REC\-CNT\=)(\d+)/) && ($recs_processed = $2);


Why they use $_??? I don't understand how it works on production.


digioleg54
User

Apr 20, 2017, 1:31 PM

Post #10 of 14 (3240 views)
Re: [FishMonger] Strange line in the code and not declared variable [In reply to] Can't Post

You know, may be you are right, but anyway I have to do it. I am the only resource. One is in India, but when I talked to him, he doesn't want to change anything. It is not admin. Company doesn't want to spend money. They have paid huge fine to government for money laundering. I don't want even talk about that


FishMonger
Veteran / Moderator

Apr 20, 2017, 1:33 PM

Post #11 of 14 (3238 views)
Re: [digioleg54] Strange line in the code and not declared variable [In reply to] Can't Post


In Reply To
why do we need my $file then? don't you think, that it would the file *.PIN, where we have to put $short_desc?we don't use $f_pin nowhere in the script


$file is not really needed, but splitting out your original statement into assigning $path and $file makes the code more readable and maintainable.

If you don't use $f_pin, then why did you have it in your code and asking how to fix it?

Your limited and confusing descriptions of the problem and code examples don't make much sense which makes it's very difficult to help you.


FishMonger
Veteran / Moderator

Apr 20, 2017, 1:46 PM

Post #12 of 14 (3235 views)
Re: [digioleg54] Strange line in the code and not declared variable [In reply to] Can't Post


In Reply To
I also had a line

Code
#$_ = `head -1 $finfo`;


instead I put:

Code
($_) = (my $firstline) = path($finfo)->lines( { count => 1 }); 
(/(recs_processed\=\")(\d+)(\")/ || /(REC\-CNT\=)(\d+)/) && ($recs_processed = $2);


Why they use $_??? I don't understand how it works on production.


Who (which support forum) is providing you with that code? Or was that written by someone from your company?


digioleg54
User

Apr 20, 2017, 1:56 PM

Post #13 of 14 (3233 views)
Re: [FishMonger] Strange line in the code and not declared variable [In reply to] Can't Post

I found this

Code
(my $firstline) = path($finfo)->lines( { count => 1 });

on stackoverflow.com

Code
use Path::Tiny;


has path. However, I probably has to take of $_. I don't know why they used it 16 years ago.

Thank you for your help


FishMonger
Veteran / Moderator

Apr 21, 2017, 7:12 AM

Post #14 of 14 (3212 views)
Re: [FishMonger] Strange line in the code and not declared variable [In reply to] Can't Post

Using Path::Tiny is overkill for what you need, especially since you don't understand what that statement is doing.

Basically (without the gory details) $_ is a copy of $firstline and is what the regex's in the next statement are binding to.


Code
(/(recs_processed\=\")(\d+)(\")/ || /(REC\-CNT\=)(\d+)/) && ($recs_processed = $2);

Is the somewhat shorthand of:

Code
($_ =~ /(recs_processed\=\")(\d+)(\")/ || $_ =~ /(REC\-CNT\=)(\d+)/) && ($recs_processed = $2);

which is a shorthand of:

Code
($firstline =~ /(recs_processed\=\")(\d+)(\")/ || $firstline =~ /(REC\-CNT\=)(\d+)/) && ($recs_processed = $2);


 
 


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

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