我在subroutine中创建我的SQL数据库连接时遇到了一个问题。下面的代码保存在服务器上,文件名为"ALabel“
package ALabel;
use strict;
use warnings;
use DBI;
#!/usr/bin/perl
use lib '/var/www/cgi-bin/ssiweb824/Navik';
sub ALabelPipe
{
my $sth;
my $sql;
my @idarr=@_;
my $id=@idarr[0];
my $databasename=@idarr[1];
my $co
我将一个标量变量传递给子例程,在该子程序中,我要测试该参数是否与regex匹配:
use strict;
use warnings;
use Data::Dumper;
print "Please enter your first name followed by your last name. For example: John Smith.\n";
chomp(my $name = <STDIN>);
&analyze_name($name);
sub analyze_name {
if ($_ =~ /^\w+\s+\w+$/) {
我创建对象,但不知道创建后如何轻松地使用它们(请设置/获取和运行方法)。下面的代码可以工作,但之后我无法获得对象来使用它,我如何做到这一点?
# My array of coffeecup names
my @coffeeCupNames = ("Espresso", "Sumatran", "Java");
# Create an array to hold my objects once they are created
my @objects = ();
# Create a new coffee cup object with the
#!perl6
use v6;
my $list = 'a' .. 'f';
sub my_function( $list ) {
for ^$list.elems -> $e {
$list[$e].say;
}
}
my_function( $list );
首先,我尝试了perl5风格,但它不起作用:
for @$list -> $e {
$e.say;
}
# Non-declarative sigil is missing its name at line ..., near "@$li
我有一个字符串,它的中间部分将根据条件进行更改。就像这样:
"You have x number of problems";
X的值根据某些if条件有不同的文本。我可以做的一种方法是将“您有”存储在一个变量中,将“问题数”存储在另一个变量中,然后执行类似于sprintf("%s%d%s", $firstpart, $x, $secondpart);的操作。
有更好的方法吗?
是否有可能获得指向引用的变量的名称?下面是一个例子:
private void FillArray(int[] array, int count)
{
array = new int[count];
for (int i = 0; i < array.Length; i++)
{
array[i] = i;
}
Debug.WriteLine("Array {0} is now filled up with {1} values", array.Name, count);
}
对于要在上述方法中使用的数组,没有属
在Unix中,我有一个文件,其内容如下
cat abc
;file1_$HOME;file2_$HOME
我的shell脚本是包含以下内容的TRY.ksh:
#$HOME is exported and has a value here
echo $HOME
while read line
do
echo $line
done < abc
这里的echo $line prints- ;file1_$HOME;file2_$HOME而不是取代$HOME的价值,如何才能实现呢?
我似乎在尝试访问另一个包中定义的标量时被卡住了,并且已经将一个示例缩小到一个简单的测试用例,在那里我可以重现问题。我希望它能够使用我们的机制访问在包'Example‘中定义的列表的引用,然而,Dumper显示该变量在example.pl中始终是未定义的:
Example.pm如下所示:
#!/usr/bin/perl -w
use strict;
use warnings;
use diagnostics;
package Example;
use Data::Dumper;
my $exported_array = [ 'one', 'two',
所以我有两个变量
$x = q(foo);
$y = q(bar);
我的目标是在第三个变量中使用它们,在它们之间加上下划线,即foo_bar。有很多方法可以做到这一点,但我想使用qq
所以
$z = qq($x_$y);
这会产生以下错误
Global symbol "$x_" requires explicit package name at test.pl line 45.
Execution of C:\test.pl aborted due to compilation errors.
因此,我不得不在变量x中使用花括号来使其正常工作。
$z = qq(${x}_$y
我看到了脚本(见下文),但找不到有关"-n“的更多信息。
my $numeric =0;
my $input = shift;
if ($input eq "-n") {
$numeric =1;
$input = shift;
}
my $output = shift;
open INPUT, $input or die $!;
open OUTPUT, ">$output" or die $!;
my @file = <INPUT>;
if ($numeric) {
@file = sort { $a <
perl如何识别变量的结尾?
例如,下面的代码:
use warnings;
my $a = 10;
print "Value of a is $a:::";
输出:
Use of uninitialized value $a:: in concatenation (.) or string at tryprint.pl line 6.
Value of a is :
为什么它考虑$a::而不考虑$a:或$a:
这是可行的:
print "Value of a is $a\:::";
打印:
Value of a is 10:::
对于这个微小的代码,它总是在全局代码块中打印@x的值。如果我无意中没有'my @x‘而不初始化子例程中的变量,如何发出警告。子程序中的局部变量不是@x。
use strict;
use warnings;
my @x=qw/1 2 3/;
&mysub();
sub mysub{
print "@x";
}
我有一个小程序,它基本上处理爆炸命中列表,并通过包含每个爆炸列表的散列来迭代blast结果(作为散列键),以检查blast结果之间是否存在重叠。
这涉及到以同样的方式将每个blast输入文件作为$ARGV处理。根据我想要实现的目标,我可能想比较一下基因重叠的2,3或4个爆炸列表。我想知道如何将基本处理块作为子程序来编写,无论存在多少$ARGV参数,都可以迭代它。
例如,如果我输入2个爆炸列表,下面的内容就很好了:
#!/usr/bin/perl -w
use strict;
use File::Slurp;
use Data::Dumper;
$Data::Dumper::Sortkeys