How to include file in Raku - perl-module

I have two Raku files:
hello.p6:
sub hello
{
say 'hello';
}
and main.p6:
require 'hello.p6';
hello();
But don't work. How to can include the first file in the main script?

Just for the record, the proper solution is to use a module:
File Hello.pm6
module Hello;
sub hello() is export {
say 'hello';
}
File hello.p6:
use v6;
use lib '.'; # to search for Hello.pm6 in the current dir
use Hello;
hello;

Using explicit file syntax and explicit export list seems to work for me in Rakudo:
main.p6:
require Hello:file('Hello.p6') <hello>;
hello();
hello.p6:
sub hello {
say 'hello';
}
Source: http://perlcabal.org/syn/S11.html#Runtime_Importation

Related

execute bash command in a gradle function

I want to create a generic function in gradle that executes a command. This function is called from a task.
The function executeCommand is triggered from the task copyFile but it seems that the commandLine commands are not executed. I did this because I need a generic ececuteCommand functionality that is triggered from multiple jobs.
def executeCommand(execCmd) {
try {
exec {
println("execute $execCmd in .")
commandLine 'bash', '-c', "ls -la"
commandLine 'bash', '-c', "${execCmd}"
}
}
catch(Exception e){
println("Exception: $e")
}
}
task copyFile {
doLast {
if(project.hasProperty('file')) {
ext.myFile = file
def execCmd="cp ${myFile} ."
executeCommand(${execCmd})
}
else {
println("Please specifiy argument files -Pfile=SRC_PATH")
}
}
}
There is a syntax error in your script, you should normally have an error as follows during execution:
* What went wrong:
Execution failed for task ':copyFile'.
> Could not find method $() for arguments [build_djiuilz6w3giaud8hgmf0oze7$_run_closure2$_closure5$_closure6#57fdda61] on task ':copyFile' of type org.gradle.api.DefaultTask. (normally you should have an error when trying to execute it : **
you need to replace the following statement in your copyFile.doLast{ } block:
executeCommand(${execCmd})
with:
executeCommand( execCmd)
// or: executeCommand( "${execCmd}" )
NOTE: in the exec {} block of your executeCommand function, there are two calls to commandLine function: only the second one will have effect so the command 'ls -al' will never be executed.
The rest of your script seems valid and should work as expected.

Is there a macro or similar workaround to include the source folder (src) path at compile time?

Is there a Rust macro or a similar workaround to include the path of the 'src' folder created via cargo new in my source file as a string literal at compile time or specifically when doing cargo build?
I have successfully done something similar where I use include_str! to include file content but I need to know if it is possible to directly include the src path in the code.
No, but you can get close using file!:
const FILE: &'static str = concat!(env!("CARGO_MANIFEST_DIR"), "/", file!());
fn main() {
use std::path::Path;
println!("FILE: {:?}", FILE);
println!("src path: {:?}", Path::new(FILE).parent());
}
Outputs, on the playground:
FILE: "/playground/src/main.rs"
src path: Some("/playground/src")

Calling bash function from a bash function which takes arguments like any other language? [duplicate]

This question already has answers here:
How to call a function in shell Scripting?
(7 answers)
Closed 6 years ago.
I am new to bash scripting and trying to find a way to call a bash function from another bash function that takes one or many arguments passed like we have in other languages ?
for example.
function b()
{
echo "$1 World!"
}
function a()
{
b("Hello!")
}
with call for function "a" would give output of Hello World! (I am not sure if this will work). Any help is appreciated.
Thank you
Just add the parameters after the function call, separated by white space (Just like the parameters of a main function in a C or Java binary).
The following script:
#!/bin/bash
function b()
{
echo "$1 World!"
}
function a()
{
b "Hello!"
}
a
will output Hello! World!.
Surround the parameter with double-quotes if needed.
example:
$ b x y
x World!
$ b "x y"
x y World!

command line wildcards in Julia on Windows

How can I expand wildcard commandline arguments in Julia?
The shell doesn't seem to expand them before they get there.
If I call my script as julia script.jl *.dat, my output is just *.dat
for arg in ARGS
println(arg)
end
If I write the equivalent program in Java:
public class rejig {
public static void main(String[] args) throws Exception {
for(int i = 0; i < args.length; i++) {
System.out.println(args[i]);
}
}
}
and call it as java rejig *.dat, I get a listing of all the DAT files in the current directory.
My searching along the lines of "command line", "wildcards", and the like hasn't got me very far.
How do I get Julia to give the same output as the Java code?
I wrote a pure-Julia implementation of Glob (aka fnmatch or wildcard commandline expansion) at https://github.com/vtjnash/Glob.jl, which also available via Pkg.add("Glob").
This can be used for platform-independent wildcard expansion, such as your *.dat example.
As explained in the comment, the shell is the program which expands the wildcards. This expansion is called glob expansion and there are functions in the standard C library which do it (and the shell probably uses itself).
Practically, here is an example of interfacing with the standard libc to expand wildcards:
type GlobType
pathc::Int64
names::Ptr{Ptr{UInt8}}
slots::Int64
extra1::Int64
extra2::Int64
end
function parseglob(gb::GlobType)
i=1
res = UTF8String[]
while i<=gb.pathc
p = unsafe_load(gb.names,i)
if p==C_NULL return res ; end
push!(res,bytestring(p))
i+=1
end
res
end
function glob(filepattern::AbstractString)
gb = GlobType(0,C_NULL,0,0,0)
retval = ccall((:glob,"libc"),Cint,
(Ptr{UInt8},Cint,Ptr{Void},Ptr{GlobType}),
filepattern,0,C_NULL,&gb)
res = ( retval==0 ? parseglob(gb) : UTF8String[] )
ccall((:globfree,"libc"),Void,(Ptr{GlobType},),&gb)
res
end
# glob("*.jl") # ["glob.jl"] on my machine
the library routine has many flags and options which might be of interest to you.

How do I read a single character from STDIN using Perl on Windows?

Using Perl, how do I capture a single character from STDIN without needing the user to hit enter (similar to C's getch() function)?
Perl has a getc() function, but according to the perlfunc:
However, it cannot be used by itself to fetch
single characters without waiting for
the user to hit enter.
The perlfunc docs do provides a way to read a single character using getc() but it requires manipulating the terminal settings using stty. The script I'm writing needs to work on Windows (without cygwin, msys, etc.) - so that's not an option.
From perlfaq5's answer to How can I read a single character from a file? From the keyboard?
You can use the builtin getc() function for most filehandles, but it won't (easily) work on a terminal device. For STDIN, either use the Term::ReadKey module from CPAN or use the sample code in getc in perlfunc.
If your system supports the portable operating system programming interface (POSIX), you can use the following code, which you'll note turns off echo processing as well.
#!/usr/bin/perl -w
use strict;
$| = 1;
for (1..4) {
my $got;
print "gimme: ";
$got = getone();
print "--> $got\n";
}
exit;
BEGIN {
use POSIX qw(:termios_h);
my ($term, $oterm, $echo, $noecho, $fd_stdin);
$fd_stdin = fileno(STDIN);
$term = POSIX::Termios->new();
$term->getattr($fd_stdin);
$oterm = $term->getlflag();
$echo = ECHO | ECHOK | ICANON;
$noecho = $oterm & ~$echo;
sub cbreak {
$term->setlflag($noecho);
$term->setcc(VTIME, 1);
$term->setattr($fd_stdin, TCSANOW);
}
sub cooked {
$term->setlflag($oterm);
$term->setcc(VTIME, 0);
$term->setattr($fd_stdin, TCSANOW);
}
sub getone {
my $key = '';
cbreak();
sysread(STDIN, $key, 1);
cooked();
return $key;
}
}
END { cooked() }
The Term::ReadKey module from CPAN may be easier to use. Recent versions include also support for non-portable systems as well.
use Term::ReadKey;
open(TTY, "</dev/tty");
print "Gimme a char: ";
ReadMode "raw";
$key = ReadKey 0, *TTY;
ReadMode "normal";
printf "\nYou said %s, char number %03d\n",
$key, ord $key;
You want this module: Term::ReadKey.

Resources