I have an error in a Perl script witch imports pictures in the Piwigo Galery. Since I updated the Piwigo to 2.6.1, the script has an error:
Not a HASH reference at /home/..../piwigo_import_tree.pl line 127
Run Code Online (Sandbox Code Playgroud)
The script is not mine. I have written an email to its author, but I received no answer.
Here is the script:
#!/usr/bin/perl
# usage:
# perl piwigo_import_tree.pl --directory=/Users/pierrick/piwigo/album1
use strict;
use warnings;
use File::Find;
use Data::Dumper;
use File::Basename;
use LWP::UserAgent;
use JSON;
use Getopt::Long; …Run Code Online (Sandbox Code Playgroud) 问题来自下面的代码:
vector<int &> one; //compile failed
Run Code Online (Sandbox Code Playgroud)
我很困惑为什么代码无法编译。
我不知道为什么这行不通?我需要传递矢量引用,以便可以从外部函数对其进行操作。
互联网上对此有几个问题,但我听不懂答复吗?
下面的代码:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
string funct(vector<string> *vec)
{
cout << vec[1] << endl;
}
int main()
{
vector<string> v;
v.push_back("one");
v.push_back("two");
v.push_back("three");
}
Run Code Online (Sandbox Code Playgroud) 我有一个Perl哈希(来自一些遗留代码)但我无法打印出密钥.
if (ref $val eq ref {}) {
print "Keys: " . keys $val . "\n";
Run Code Online (Sandbox Code Playgroud)
例如,这是我得到的输出:
VAL: HASH(0x7ff0898eda70)
Type of argument to keys on reference must be unblessed hashref or arrayref
Run Code Online (Sandbox Code Playgroud)
我已经读过这个参数类型的参数必须是unblessed hashref或arrayref但不知道如何在这种情况下应用它.
有没有办法解决这个问题?
====
UPDATE
我也尝试过:
print "Keys: " . keys %$val . "\n";
Run Code Online (Sandbox Code Playgroud)
但仍然得到 Type of argument to keys on reference must be unblessed hashref or arrayref
更新2
我可以看到我有钥匙,a_key但我无法打印出它的价值.例如调试Carp::REPL我得到:
$ print $val;
1$ HASH(0x7fb1e0828f00)
$ print %$val;
1$ a_keyARRAY(0x7fb1e0828e28)
$ print …Run Code Online (Sandbox Code Playgroud) 我想在perl中可以从散列中获取数组引用或将ref散列转换为ref数组!
例如 :
%trad = ('January','Jan','February','Feb');
$ref = \%trad; # made a reference on hash
$ref2 = [%{$ref}]; # convert ref hash to ref array
Run Code Online (Sandbox Code Playgroud)
以其他方式我们可以写:
$ref2 = [%{\%trad}];
Run Code Online (Sandbox Code Playgroud)
但是这段代码是散列上的ref数组的错觉,因为contruction $ ref = [...]创建了一个对通过Perl复制哈希元素而创建的匿名数组的引用.
您可以通过尝试填充$ ref2指向的数组来查看,这对哈希没有影响!
有没有办法直接在内存区域直接操作哈希,使用真正的引用数组,如C中的cast方法!
我的Perl项目可以在多个系统上运行,但我不希望它们在不知道其他系统正在做什么的情况下单独运行.我需要保持他们的数据文件或多或少同步,这意味着合并他们的内容,所以我需要一个数据结构来包含每台计算机的详细信息以及对另一台计算机的引用.
正是这种引用导致了我的问题.
如果我将它简化为两台PC
use strict;
use warnings;
use Sys::Hostname;
# Peer computers
my $PC1 = {
name => 'PC1',
os => 'Linux',
data => '/its/data/directory',
peer => 'PC2' # But whas is really needed here is a reference to PC2's data structure below
};
my $PC2 = {
name => 'PC2',
os => 'MSWin32',
data => 'X:\\its\\data\\directory',
peer => 'PC1' # But whas is really needed here is a reference to PC1's data structure above
};
my $PEERS = [ …Run Code Online (Sandbox Code Playgroud) 我有以下无法编译的代码:
struct A {
x: i32,
}
impl A {
fn add_assign(&mut self, other: &Self) {
self.x += other.x;
}
fn double(&mut self) {
self.add_assign(self);
}
}
Run Code Online (Sandbox Code Playgroud)
错误是:
error[E0502]: cannot borrow `*self` as mutable because it is also borrowed as immutable
--> src/lib.rs:11:9
|
11 | self.add_assign(self);
| ^^^^^----------^----^
| | | |
| | | immutable borrow occurs here
| | immutable borrow later used by call
| mutable borrow occurs here
Run Code Online (Sandbox Code Playgroud)
如何传递self作为参数add_assign?我已经试过&self, …
当我们写:
fn foo<'a, 'b>(x: &'a u32, y: &'b u32) -> &'a u32 {
x
}
Run Code Online (Sandbox Code Playgroud)
为什么我们不将'as 和'bs 称为生命周期参数而不是通用生命周期参数?这只是一种语法方式,可以根据参数的生命周期向编译器传达对返回引用生命周期的约束。我正在努力了解在此处包含“通用”一词的理由。
我知道有一个类似的话题,但这些答案并没有说明我想知道什么。因此,只要从下面的摘录中可以注意到一个函数和对该函数的引用的行为方式相同,那么同时拥有函数变量和函数指针又有什么意义呢?
#include <stdio.h>
int f(){ return 0;}
int main() {
printf("%p\n",f);
printf("%p\n",&f);
printf("%p\n",f + 1);
printf("%p\n",&f + 1);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
此外, f 和 &f 都可以作为其他函数的参数传递。
看看这段代码:
let some_number: usize = 5;
let some_string = "abc".repeat(&some_number);
Run Code Online (Sandbox Code Playgroud)
我收到编译错误:
预期
usize,发现&usize
我想知道,是否有任何理由要求repeat重复次数具有自有价值?如果我理解正确,如果repeat需要引用,我可以同时传递some_number(Rust 会自动传递引用)或&some_number. 对于程序员来说,体验会更好。