有没有办法让子程序在处理时发回数据?例如(此示例仅用于说明) - 子例程读取文件.当它正在读取文件时,如果满足某些条件,则"返回"该行并继续处理.我知道有些人会回答 - 你为什么要那样做?你为什么不......?,但我真的想知道这是否可能.
$ cat test.pl
use strict;
use warnings;
sub route {
print "hello, world!";
}
my %h;
$h{'a'} = 'route';
print "1\n";
$h{a};
print "2\n";
$h{a}();
print "3\n";
"$h{a}".();
$ perl test.pl
Useless use of hash element in void context at test.pl line 12.
Useless use of concatenation (.) or string in void context at test.pl line 18.
1
2
Can't use string ("route") as a subroutine ref while "strict refs" in use at test.pl line 15.
$
Run Code Online (Sandbox Code Playgroud)
什么是正确的打电话方式route()?
我是一个相当特殊的applecripter,并且已经写了很长时间的脚本.我目前正在创建的应用程序涉及使用"数据库事件"应用程序.我试图通过使用子程序设置字段的值.显然,我"不能继续set_duration",我不知道会出现什么问题.目前的源代码如下.
property first_run : true
on run
if first_run then
display dialog "THIS APPLICATION IS DESIGNED TO CLEAN UP THE FOLDER CONTAINING IT." & return & return & "After a certain number of days that you set, every item in that folder that has not been used for that duration will automatically be moved to a folder named \"Unused Items\"." with icon 1 buttons {"Cancel", "OK"} default button 2
set first_run to false
end if
tell application "Database Events"
set …Run Code Online (Sandbox Code Playgroud) 给出一个Perl包Foo.pm,例如
package Foo;
use strict;
sub bar {
# some code here
}
sub baz {
# more code here
}
1;
Run Code Online (Sandbox Code Playgroud)
如何编写脚本来提取每个sub的文本源代码,从而产生一个哈希:
$VAR1 = {
'bar' => 'sub bar {
# some code here
}',
'baz' => 'sub baz {
# more code here
}'
};
Run Code Online (Sandbox Code Playgroud)
我希望文本与包,空白和所有内容完全一致.
谢谢.
我在Perl中运行一个程序,它在一个点上评估从子程序中调用的if语句中的数据,例如
sub check_good {
if (!good) {
# exit this subroutine
# restart program
}
else {
# keep going
}
} # end sub
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是退出并重新启动.我知道我可以exit 0;直接退出,但显然这是不正确的,如果我想回到开头.我尝试调用基本上启动程序的子程序,但是当它运行时它将再次回到这一点.我想把它放在while循环中,但这意味着将整个文件放在循环中,这将是非常不切实际的.
我实际上并不知道这是否可行,所以任何输入都会很棒.
我是Fortran的新手.我正在研究一个研究项目,我正在使用一个开源项目,该项目有多个文件分布在多个文件夹中.我发现每个程序的依赖性,但无法弄清楚如何编译它们.
我的源代码分布在三个文件夹中.a)模块b)接口c)子程序
我想在子程序文件夹中运行一个名为'Main.f90'的程序,该程序依赖于来自模块和接口文件夹的源代码.
我正在使用eclipse进行文件夹结构和makefile进行编译.
对此有任何帮助表示赞赏.
更新: 我按照@MBR和@Stefan发布的答案,由于某种原因VPATH无法在源代码中找到程序,所以我明确地给了我的Makefile中的源文件夹路径.下面是我的make文件脚本.
.PHONY: Mopac_exe clean
# Change this line if you are using a different Fortran compiler
FORTRAN_COMPILER = gfortran
SRC = src
#make main program
Mopac_exe: subroutines mopac.o
$(FORTRAN_COMPILER) mopac.o *.o -O2 -g -o bin/Mopac_exe -I Modules/
#compile all the subroutines
subroutines: interfaces
$(FORTRAN_COMPILER) -c $(SRC)/subroutines/*.F90 -J Modules/Subroutines/ -I Modules/
#compiles all the interfaces
interfaces: modules
$(FORTRAN_COMPILER) -c $(SRC)/interfaces/*.f90 -J Modules/
# build all the modules and generate .mod file in Modules …Run Code Online (Sandbox Code Playgroud) 请看下面的代码:
use strict;
use warnings;
print "subroutine is defined\n" if defined &myf;
myf();
sub myf
{
print "called myf\n";
}
undef &myf;
#myf();
print "now subroutine is defined\n" if defined &myf;
Run Code Online (Sandbox Code Playgroud)
输出是
subroutine is defined
called myf
Run Code Online (Sandbox Code Playgroud)
第一个print语句可以打印,这是否意味着解释器(或编译器?)看得更远并看到子例程定义?如果是这样,为什么它没有看到undef &myf;第二个print陈述?
谢谢
我有下一个程序:
use warnings;
use strict;
BEGIN {
print \&mysub;
}
sub mysub {};
print \&mysub;
Run Code Online (Sandbox Code Playgroud)
它的输出:
CODE(0x118e890)CODE(0x118e890)
Run Code Online (Sandbox Code Playgroud)
该BEGIN块在编译时处理.那时sub mysub编译器还没有看到定义.但是程序仍会打印正确的子程序地址,它在定义时会有.
为什么我这里没有错误?这是某种自动化吗?
什么是Perl 6方法来区分没有显式签名的块中的参数和无参数之间的区别?我没有任何实际用途,但我很好奇.
没有显式签名的块将值放入$_:
my &block := { put "The argument was $_" };
Run Code Online (Sandbox Code Playgroud)
签名实际上是;; $_? is raw.这是一个可选参数.该@_变量不是在块定义的,因为没有明确的签名.
没有参数,$_未定义的地方:
&block(); # no argument
Run Code Online (Sandbox Code Playgroud)
但是也存在一个$_未定义的论证情况.类型对象始终未定义:
&block(Int);
Run Code Online (Sandbox Code Playgroud)
但是,$_其中没有任何东西实际上是一个Any(而不是说,Nil).我无法区分这两种情况:
&block();
&block(Any);
Run Code Online (Sandbox Code Playgroud)
这是一个较长的例子:
my $block := {
say "\t.perl is {$_.perl}";
if $_ ~~ Nil {
put "\tArgument is Nil"
}
elsif ! .defined and $_.^name eq 'Any' {
put "\tArgument is an Any type object"
}
elsif $_ …Run Code Online (Sandbox Code Playgroud) 我已经阅读了http://www.perl101.org/subroutines.html但我只是不了解可选参数.
我想在PDF :: API2中调用以下子.该文件称"-indent"是一种选择.我如何为缩进20传递参数?
这就是我现在所经过的:
$txt->section($str, $contentwidth,$heightmax);
Run Code Online (Sandbox Code Playgroud)
这是子
Run Code Online (Sandbox Code Playgroud)sub section { my ($self,$text,$width,$height,%opts)=@_; my $overflow = ''; foreach my $para (split(/\n/,$text)) { if(length($overflow) > 0) { $overflow .= "\n" . $para; next; } ($para,$height) = $self->paragraph($para,$width,$height,%opts); $overflow .= $para if (length($para) > 0); } if (wantarray) { return ($overflow,$height); } return $overflow; }