小编Ste*_*ieD的帖子

Inline::Perl5 无法与 Lingua::En::Titlecase Perl 模块一起使用

收到:

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大约一个月前,当我踢轮胎时,它为我工作。不确定我做错了什么。文档没有为我提供任何线索

raku

4
推荐指数
1
解决办法
103
查看次数

启动画面不隐藏

我按照 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)

splash-screen ios react-native react-native-splash-screen

4
推荐指数
2
解决办法
3363
查看次数

这个循环还有哪些其他方法可以重写?

得到这个简单的循环:

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)

它的可读性较差(至少对于我未经训练的眼睛来说),但它有效。

我的预感是有一些好方法可以使这段代码更加简洁和可读。有兴趣看看我是否是对的。

raku

3
推荐指数
1
解决办法
139
查看次数

解决我的 Str 子类中的“无法修改不可变”问题?

我有这个类的子类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

这可能吗?或者是否有更干净的方法来解决不可变对象问题?

raku

3
推荐指数
1
解决办法
248
查看次数

Web :: Scraper在HTML文档的<body>中找不到<link>或<meta>元素

我现在一直在盯着这一小时,我正在撒谎.

我试图从网页上抓取一些数据.这是我正在尝试提取的一些数据的片段:

<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 scraper

2
推荐指数
1
解决办法
223
查看次数

来自Perl脚本的Bash配置文件抛出错误

在获得源代码后调用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。

bash perl

2
推荐指数
1
解决办法
55
查看次数

如何为方法编写调度表?

我正试图摆脱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方法?

perl

1
推荐指数
1
解决办法
170
查看次数

使用Ruby中的多个参数记忆函数

我想知道是否有更多"类似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变量的键.

ruby memoization

1
推荐指数
1
解决办法
454
查看次数

防止bash变量污染环境

我有一个编译 .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手动处理每个变量。想知道是否有更好的方法来做到这一点。

bash

1
推荐指数
1
解决办法
349
查看次数

c/perl/python 中简单程序之间的执行时间过长的原因是什么?

我用 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 系统 …

c python perl execution

1
推荐指数
1
解决办法
97
查看次数