[英]How to call a perl script from another perl script and transfer parameter?
我試圖從另一個 perl 腳本調用一個 perl 腳本,並將參數傳遞給它。
例如:
有一個腳本:main.pl
我使用命令行運行此腳本,並為參數“$directory”賦值,然后調用另一個 perl 腳本“sub.pl”。 我想將“$directory”的值從 sub.pl 傳遞給參數“$path”。
(簡而言之,sub.pl 有參數 $path,main.pl 有參數 $directory,我想在 main.pl 中調用 sub.pl,並將 $directory 值傳遞給 $path)
對不起,我的詳細描述......無論如何,哪個功能可以完成這項工作? 謝謝。
您沒有提供您嘗試過的任何代碼示例——我們如何知道您對代碼的看法?
好的,下面我提供了一個主腳本dir_main.pl
和輔助腳本dir_sub.pl
的示例,只是為了演示我將如何做。
這兩個腳本都可以使用參數“--help”(-h)或“--man”(-m)運行,以獲取幫助和描述腳本用法和完整文檔的手冊頁。 腳本dir_sub.pl
有額外的選項 '--debug' (-d) 來打印選項哈希的內容。
用法: perl dir_main.pl --dir c:\\Users
-- Windows
用法: dir_main.pl --dir /usr/home
-- Linux
注意:在 Linux 中,兩個腳本都應該使用以下命令執行chmod og+x dir_main.pl dir_sub.pl
才能從shell
運行而不指定perl
(shell 從shebang
知道腳本應該使用 perl 解釋器運行)
源代碼: dir_main.pl
#!/usr/bin/perl
#
# DESCRIPTION:
# Sample code 'dir_main.pl' written for StackOverflow
#
# DATE:
# Jan 10, 2020
#
# AUTHOR:
# Polar Bear <https://stackoverflow.com/users/12313309/polar-bear>
#
use strict;
use warnings;
use Getopt::Long qw(GetOptions);
use Pod::Usage;
my %opt;
GetOptions(
'dir|d=s' => \$opt{dir},
'help|h' => \$opt{help},
'man|m' => \$opt{man}
) or pod2usage(2);
pod2usage(1) if $opt{help};
pod2usage(-exitval => 0, -verbose => 2) if $opt{man};
system('perl','.\dir_sub.pl','--path',$opt{dir}) if $opt{dir};
exit 0;
=pod
=head1 NAME
program.pl - short description of the program
=head1 SYNOPSIS
program.pl [options]
Options:
--dir,-d input directory
--help,-h brief help message
--man,-m full documentation
=head1 OPTIONS
=over 4
=item B<--dir|-d>
Input directory
=item B<--help|-h>
Print a brief help message and exit
=item B<--man|-m>
Prints the manual page and exit
=back
=head1 DESCRIPTION
B<This program> surve some purpose to produce pre-defined result
=head1 AUTHOR
Polar Bear Jan 10, 2020
=head1 REPORTING BUGS
E-mail L<mailto:bugs@inter.net>
=head1 COPYRIGHT
Copyright information
=head1 SEE ALSO
L<The Perl Home page|http://www.perl.org/>
=cut
源代碼: dir_sub.pl
#!/usr/bin/perl
#
# DESCRIPTION:
# Sample code 'dir_sub.pl' written for StackOverflow
#
# DATE:
# Jan 10, 2020
#
# AUTHOR:
# Polar Bear <https://stackoverflow.com/users/12313309/polar-bear>
#
use strict;
use warnings;
use feature 'say';
use Getopt::Long qw(GetOptions);
use Pod::Usage;
use Data::Dumper;
my %opt;
GetOptions(
'path|p=s' => \$opt{path},
'help|h' => \$opt{help},
'man|m' => \$opt{man},
'debug|d' => \$opt{debug}
) or pod2usage(2);
pod2usage(1) if $opt{help};
pod2usage(-exitval => 0, -verbose => 2) if $opt{man};
print Dumper(\%opt) if $opt{debug};
list($opt{path}) if $opt{path};
sub list {
my $path = shift;
opendir my $dh, $path
or die "ERROR: opendir couldn't open $path";
map{ say $_ } readdir($dh);
close $dh;
}
exit 0;
=pod
=head1 NAME
program.pl - short description of the program
=head1 SYNOPSIS
program.pl [options]
Options:
--path,-p input path to list
--help,-h brief help message
--man,-m full documentation
--debug,-d debug information
=head1 OPTIONS
=over 4
=item B<--path|-p>
Input path to list files
=item B<--help|-h>
Print a brief help message and exit
=item B<--man|-m>
Prints the manual page and exit
=item B<--debug|-d>
Prints the debug information
=back
=head1 DESCRIPTION
B<This program> surve some purpose to produce pre-defined result
=head1 AUTHOR
Polar Bear Jan 10, 2020
=head1 REPORTING BUGS
E-mail L<mailto:bugs@inter.net>
=head1 COPYRIGHT
Copyright information
=head1 SEE ALSO
L<The Perl Home page|http://www.perl.org/>
=cut
第一個問題: sub.pl
是否有從命令行設置$path
的機制?
如果沒有,您就不會這樣做:您不能隨意將內部變量設置為某個值……這會給任何代碼段帶來各種安全風險和危險!
假設您可以調用sub.pl path=/my/path
那么這很容易(但我非常懷疑您指的是前者!)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.