收到:
use Lingua::En::Titlecase:from<Perl5>;
# this line is straight from doc
my $tc = Lingua::EN::Titlecase.new("CAN YOU FIX A TITLE?");
Run Code Online (Sandbox Code Playgroud)
得到这个:
Could not find symbol ''&Titlecase'' in ''GLOBAL::Lingua::EN''
Run Code Online (Sandbox Code Playgroud)
我记得Inline::Perl5大约一个月前,当我踢轮胎时,它为我工作。不确定我做错了什么。文档没有为我提供任何线索
我按照 iOS 博客的说明进行操作:在 React Native 中构建启动屏幕
我能够构建应用程序,并且打开时会显示启动屏幕。然而,启动画面永远不会消失。
这是App.tsx文件:
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
*/
import React from 'react';
import type {PropsWithChildren} from 'react';
import {
SafeAreaView,
ScrollView,
StatusBar,
StyleSheet,
Text,
useColorScheme,
View,
} from 'react-native';
import React, { useEffect } from 'react'; //import useEffect();
import SplashScreen from "react-native-splash-screen";
import {
Colors,
DebugInstructions,
Header,
LearnMoreLinks,
ReloadInstructions,
} from 'react-native/Libraries/NewAppScreen';
type SectionProps = PropsWithChildren<{
title: string;
}>;
function Section({children, title}: SectionProps): JSX.Element { …Run Code Online (Sandbox Code Playgroud) 得到这个简单的循环:
use Config::Simple:from<Perl5>;
my $cfg = Config::Simple.new(syntax => 'ini');
%config{'short_name'} = 'blah';
for %config.kv -> $k, $v {
$cfg.param("%config{'short_name'}.$k", $v);
}
Run Code Online (Sandbox Code Playgroud)
工作正常。但我想更熟悉实现相同目标的其他方法,即使只是为了轻松地阅读其他人的代码。另外,循环似乎是“旧”学校的做事方式,而不是很“像 Raku”,我需要更舒适地以更高级的方式使用函数。
不管怎样,为了伸展我的新 Raku 肌肉,我想出了这句话作为替代方案:
map(-> $k, $v { $cfg.param("%config{'short_name'}.$k", $v) }, %config.kv);
Run Code Online (Sandbox Code Playgroud)
它的可读性较差(至少对于我未经训练的眼睛来说),但它有效。
我的预感是有一些好方法可以使这段代码更加简洁和可读。有兴趣看看我是否是对的。
我有这个类的子类Str:
use Vimwiki::File::TextProcessingClasses;
unit class Vimwiki::File::ContentStr is Str;
method new(Str:D $string) {
self.Str::new(value => $string);
}
# this method fails
method capitalize-headers() {
self = Vimwiki::File::TextProcessingClasses::HeadlineCapitalizer.new.capitalize-headers(self);
}
Run Code Online (Sandbox Code Playgroud)
问题是该capitalize-headers方法因错误而失败,Cannot modify an immutable因为它是一个字符串。我可以避免这个问题的一种方法是简单地不子类化Str并为Str我想要使用的方法编写包装器,如下所示:
unit class Vimwiki::File::ContentStr;
has Str $!content;
method uc() {
$!content = $!content.uc;
}
Run Code Online (Sandbox Code Playgroud)
但这让我想知道,Raku 中是否可能有类似 AUTOLOAD 之类的东西,而不是编写这些包装方法,这样如果方法不存在,则可以默认在属性上调用不存在的方法$!content。
这可能吗?或者是否有更干净的方法来解决不可变对象问题?
我现在一直在盯着这一小时,我正在撒谎.
我试图从网页上抓取一些数据.这是我正在尝试提取的一些数据的片段:
<span itemprop="thumbnail" itemscope itemtype="http://schema.org/ImageObject">
<link itemprop="url" href="http://blahblah.org/video/thumbnail_23432230.jpg">
<meta itemprop="width" content="1280">
<meta itemprop="height" content="720">
</span>
Run Code Online (Sandbox Code Playgroud)
我想通过Web :: Scraper模块从标签中获取href属性的值.这是相关的perl代码:
my $div = scraper {
process 'span[itemprop="thumbnail"] > link', url => '@href';
};
my $res = $div->scrape( $html );
$url = $res->{url};
Run Code Online (Sandbox Code Playgroud)
无论我尝试什么,$ url都会返回undefined.我正在使用Web :: Scraper模块的.36版本.
在获得源代码后调用perl的bash包装函数 .bash_profile
system ('source ~/.bash_profile; osascript -e \'quit app "Chromium"\'');
尽管包装函数被调用并执行得很好,但是我从一个不相关的bash函数中抛出了一个错误:
/Users/me/.bashrc: line 9: syntax error near unexpected token `<'
/Users/me/.bashrc: line 9: ` done < <(find -L "$1" -type f -not -name *.swp -print0 | LC_COLLATE=C sort -dz)'
Run Code Online (Sandbox Code Playgroud)
这是.bashrc文件中的问题功能:
source_dir() {
while IFS= read -r -d $'\0' file; do
source_file "$file"
done < <(find -L "$1" -type f -not -name *.swp -print0 | LC_COLLATE=C sort -dz)
}
Run Code Online (Sandbox Code Playgroud)
仅当通过Perl脚本加载它时,此bash函数直接采购它时不会引发错误。我很好奇为什么。
我正在运行bash版本5.0.2。
我正试图摆脱if我正在编写处理SELECT查询的子程序结束时的语句:
sub select_query {
my ($params, $query, $return_type) = @_;
my $qh = $dbx->prepare($query);
my $param_count = 1;
foreach my $param (@$params) {
$qh->bind_param($param_count++, $param);
}
$qh->execute;
if ($return_type eq 'fetchrow_array') {
return $qh->fetchrow_array;
}
if ($return_type eq 'fetchall_arrayref') {
return $qh->fetchall_arrayref;
}
... AND SO ON ...
}
Run Code Online (Sandbox Code Playgroud)
我熟悉调度表调用不同子程序的想法.我可以用什么代码有效地调用$ qh句柄上的各种dbi方法?
我想知道是否有更多"类似Ruby"的方法来记忆Ruby中带有多个参数的函数.这是我想出的一种方法,但不确定它是否是最好的方法:
@cache = {}
def area(length, width) #Just an example, caching is worthless for this simple function
key = [length.to_s, width.to_s].join(',')
if @cache[key]
puts 'cache hit!'
return @cache[key]
end
@cache[key] = length * width
end
puts area 5, 3
puts area 5, 3
puts area 4, 3
puts area 3, 4
puts area 4, 3
Run Code Online (Sandbox Code Playgroud)
参数用逗号连接,然后用作存储@cache变量的键.
我有一个编译 .bashrc 文件的脚本。它测试某些命令是否可用。它生成如下变量:
command -v cheat 2>&1 >/dev/null
HAS_CHEAT=$?
command -v git 2>&1 >/dev/null
HAS_GIT=$?
Run Code Online (Sandbox Code Playgroud)
如果设置了这些变量,脚本中的其他文件将执行或不执行某些操作。
我遇到的问题是,加载 .bashrc 后,我的环境被这些变量污染了。我不想unset手动处理每个变量。想知道是否有更好的方法来做到这一点。
我用 C、Perl 和 Python 编写了一个简单的程序,它递增一个变量,直到它达到 10 亿。我没想到不同语言之间会有太大差异,但看到巨大差异感到非常惊讶。这些程序简单地数到 10 亿:
在 c:
int main() {
int c = 0;
while (c < 1000000000) {
c++;
}
}
Run Code Online (Sandbox Code Playgroud)
在 Perl 中:
#! /usr/bin/env perl
use strict;
use warnings;
my $x = 0;
while ($x < 1000000000) {
$x++;
}
Run Code Online (Sandbox Code Playgroud)
在 Python 中:
#!/usr/bin/env python
i = 0
while i < 1000000000:
i += 1
Run Code Online (Sandbox Code Playgroud)
使用 zsh/bash 时间函数的运行时间是:
对于 c: 1.78s 用户 0.01s 系统 98% cpu 1.813 总计
对于 perl:29.86s 用户 0.13s 系统 …