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:
System call fro Perl script

 



tester_V
Novice

Nov 18, 2017, 11:11 PM

Post #1 of 8 (2114 views)
System call fro Perl script Can't Post

Quick question. How one can call execute Perl script from another Perl script.
The system call some how fails.


Laurent_R
Veteran / Moderator

Nov 19, 2017, 1:03 AM

Post #2 of 8 (2112 views)
Re: [tester_V] System call fro Perl script [In reply to] Can't Post

Hi,

You have to show how you do it, because it works to call Perl from a Perl script with a system call.

As an example, I have here a Perl one-liner which calls the Schwartzian Transform script we discussed yesterday:

Code
$ perl -E 'say "foo"; system ("perl schwartz3.pl"); say "bar";' 
foo
Rbck_flow_log.2017-10-01.1
Rbck_flow_log.2017-10-01.2
Rbck_flow_log.2017-10-01.3
Rbck_flow_log.2017-10-01.4
Rbck_flow_log.2017-10-01.10
Rbck_flow_log.2017-10-01.12
Rbck_flow_log.2017-10-01.13
bar

Having said that, this may not be a very good idea and there may be a better way to do what you want. Please explain what you're trying to achieve.

(Since you're stuck with version 5.8 of Perl, you'll have to replace "-E" with "-e" and "say" with "print" and add line ends. If you're on Windows, you also need to change single quotes with double quotes and vice versa.)


tester_V
Novice

Nov 19, 2017, 1:41 AM

Post #3 of 8 (2109 views)
Re: [Laurent_R] System call fro Perl script [In reply to] Can't Post

Hi Lauren,
I decided to try PM modules you guys suggested on a PC that has internet access and have to tell you those modules making life much easier.
Anyway, I installed <Sort::Naturally> module and a <Tie::File >and it is working amazingly well.
There is a little problem with the <Tie::File > part of the code,
It fails to execute (remove last line from a file) but if I ran it separately it is working fine. I thought maybe it is a good idea to make a separate script and just call it from the main script.
Iím using the same log files for processing.
Here is code that does not work.



Code
#!/usr/local/bin/perl 
use strict ;
use warnings ;
use File::Copy;
use Sort::Naturally ;


my $low = "C:/Sixshot_logs/Flow.txt" ;
my $flow_sorted = "C:/Sixshot_logs/Flow-Sorted.txt" ;


open (my $flow_sorted_fh,'>',$flow_sorted) or die "Cannot open $flow_sorted $!" ;

open (my $low_fh, '<', $low) or die $!;
my @file_list = <$low_fh>;

close $low_fh;

my @list = nsort (@file_list) ;

print $flow_sorted_fh "@list \n" ;

close $low_fh ;
close $flow_sorted_fh ;

use Tie::File ;
use Tie::Array; # Have to use this use Tie::Arrey ##

my $cleand_Flow_f = "C:/Sixshot_logs/Flow-Sorted.txt" ;
tie my @lines_c, 'Tie::File', $cleand_Flow_f or die "can't update $cleand_Flow_f: $!";
delete $lines_c[-1];

exit ;


Laurent_R
Veteran / Moderator

Nov 19, 2017, 2:43 AM

Post #4 of 8 (2107 views)
Re: [tester_V] System call fro Perl script [In reply to] Can't Post

Don't do it this way, this is much too complicated.

It is much easier and simpler to remove the last record from your @list array before writing it to the output file (or to write to the output file everything but the last item.


tester_V
Novice

Nov 19, 2017, 10:22 AM

Post #5 of 8 (2095 views)
Re: [Laurent_R] System call fro Perl script [In reply to] Can't Post

And how to do that?
Googling this topic suggests very complicated code for that.
What you would suggest?

Thank you?


tester_V
Novice

Nov 19, 2017, 10:33 AM

Post #6 of 8 (2093 views)
Re: [tester_V] System call fro Perl script [In reply to] Can't Post

I found it in Google, I "pop" the array list.
IT is working.
Here is the code:

my $filename = "C:/Sixshot_logs/Flow.txt" ;
my $fcopy = "C:/Sixshot_logs/Flow-Copy.txt" ;
my $flow_sorted = "C:/Sixshot_logs/Flow-Sorted.txt" ;
copy $filename, $fcopy or die "Cannot open $filename $!" ;

open (my $flow_sorted_fh,'>',$flow_sorted) or die "Cannot open $flow_sorted $!" ;

open (my $filename_fh, '<', $filename) or die $!;
my @file_list = <$filename_fh>;
my @list = nsort (@file_list) ;
print "@list \n" ;
pop @list;
print "@list \n" ;
print $flow_sorted_fh "@list \n" ;

close $flow_sorted_fh ;
close $filename_fh ;

Code



      
    


Laurent_R
Veteran / Moderator

Nov 19, 2017, 10:34 AM

Post #7 of 8 (2093 views)
Re: [Laurent_R] System call fro Perl script [In reply to] Can't Post

Just one line of code to be added in order to remove the most recent item from the array:


Code
# ... 
my @list = nsort (@file_list);
my $last_item = pop @list; # last item now removed from @list


or not printing the last item into the file:

Code
my @list = nsort (@file_list);  
print $flow_sorted_fh "@list[0..$#list-1] \n";



tester_V
Novice

Nov 19, 2017, 10:55 AM

Post #8 of 8 (2091 views)
Re: [Laurent_R] System call fro Perl script [In reply to] Can't Post

Laurent, thank you for the code and a little education.
I appreciate it and I'm sure other appreciate it too.

 
 


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

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