subroutine

Can I call a superclass sort with a subclass compare in perl?

不想你离开。 提交于 2020-01-03 03:13:32
问题 I want to use a superclass sort which uses a subclass compare function. I've tried to distill the nature of the question in the following code. This isn't the "production" code, but is presented here for illustration. It's tested. #!/usr/bin/perl # $Id: foo,v 1.10 2019/02/23 14:14:33 bennett Exp bennett $ use strict; use warnings; package Fruit; use Scalar::Util 'blessed'; sub new { my $class = shift; my $self = bless({}, $class); $self->{itemList} = []; warn "Called with class ", blessed

Can a input argument of a Fortran subroutine be deallocated and allocated in the body of the subroutine?

孤街浪徒 提交于 2020-01-03 02:11:47
问题 UPDATE: My modified code looks like this: program run_module_test use module_test implicit none TYPE(newXYZ), allocatable, intent(inout) :: xyzArray(:) call update(xyzArray) write(6,*)'xyzArray',xyzArray end program run_module_test module module_test implicit none TYPE :: newXYZ real(4) :: x, u real(4) :: y, v real(4) :: z, w real(4),dimension(3) :: uvw END TYPE integer(4) :: shape = 3 contains subroutine update(xyzArray) integer(4) :: i TYPE(newXYZ), allocatable, intent(inout) :: xyzArray(:)

Can a input argument of a Fortran subroutine be deallocated and allocated in the body of the subroutine?

情到浓时终转凉″ 提交于 2020-01-03 02:11:20
问题 UPDATE: My modified code looks like this: program run_module_test use module_test implicit none TYPE(newXYZ), allocatable, intent(inout) :: xyzArray(:) call update(xyzArray) write(6,*)'xyzArray',xyzArray end program run_module_test module module_test implicit none TYPE :: newXYZ real(4) :: x, u real(4) :: y, v real(4) :: z, w real(4),dimension(3) :: uvw END TYPE integer(4) :: shape = 3 contains subroutine update(xyzArray) integer(4) :: i TYPE(newXYZ), allocatable, intent(inout) :: xyzArray(:)

Should I call Perl subroutines with no arguments as marine() or marine?

只愿长相守 提交于 2020-01-02 01:03:37
问题 As per my sample code below, there are two styles to call a subroutine: subname and subname() . #!C:\Perl\bin\perl.exe use strict; use warnings; use 5.010; &marine(); # style 1 &marine; # style 2 sub marine { state $n = 0; # private, persistent variable $n $n += 1; print "Hello, sailor number $n!\n"; } Which one, &marine(); or &marine; , is the better choice if there are no arguments in the call? 回答1: In Learning Perl , where this example comes from, we're at the very beginning of showing you

Is that one argument or none for a Perl 6 block?

风格不统一 提交于 2020-01-01 09:43:12
问题 What is the Perl 6 way to tell the difference between an argument and no argument in a block with no explicit signature? I don't have any practical use for this, but I'm curious. A block with no explicit signature puts the value into $_ : my &block := { put "The argument was $_" }; The signature is actually ;; $_? is raw . That's one optional argument. The @_ variable isn't defined in the block because there is no explicit signature. There's the no argument, where $_ will be undefined: &block

How can I take a reference to a Perl subroutine?

时光毁灭记忆、已成空白 提交于 2020-01-01 08:43:25
问题 I'm having some trouble figuring out how to make a reference to a subroutine in an external module file. Right now, I'm doing this: External file package settingsGeneral; sub printScreen { print $_[0]; } Main use settingsGeneral; my $printScreen = settingsGeneral::printScreen; &$printScreen("test"); but this result into an error: Can't use string ("1") as a subroutine ref while "strict refs" in use 回答1: As noted in perlmodlib, you should start your module's name with an uppercase letter: Perl

Why am I getting “called too early to check prototype” warnings in my Perl code?

落爺英雄遲暮 提交于 2020-01-01 02:11:10
问题 I have a Perl file like this: use strict; f1(); sub f3() { f2(); } sub f1() {} sub f2() {} In short, f1 is called before it is defined. So, Perl throws a warning: "f1 called too early to check prototype". But same is the case with f2 , the only diff being that it is called from inside another subroutine. It doesn't throw a warning for f2 . Why? What is the best way to resolve this issue? declare the subroutine before it is called call the sub like this: &f1(); 回答1: You can completely avoid

perl subroutine returning array and str but they are getting merged

廉价感情. 提交于 2019-12-31 03:52:05
问题 sub process_feed { my ($line) = @_; my @lines; my $last_received = ""; while (1) { if ($line =~/^{(.*?)}(.*)/) { push @lines, $1; $line = $2; } else { $last_received = $line; last; } } print "sending back @lines, $last_received\n"; return (@lines, $last_received); } my (@lines, $leftover) = process_feed("{hi1}{hi2}{hi3"); print "got lines: @lines\n"; print "got last_recevied, $leftover\n"; OUTPUT: sending back hi1 hi2, {hi3 got lines: hi1 hi2 {hi3 got last_recevied, EXPECTED: sending back hi1

Fortran - Return an anonymous function from subroutine

房东的猫 提交于 2019-12-30 09:55:43
问题 I am trying to generalize a function call from a subroutine. So my idea is something like this if (case1) then call MainSubroutine1(myFun) elseif (case2) call MainSubroutine2(myFun) end if do i = 1,4 data = myFun(i) end do I realize this is kind of vague but I am not sure if this is possible. Thank you, John edit 1/31/14 7:57 AM I am sorry for the vague way I phrased this. I was thinking something similar to what @haraldki did but I was hoping that I could define an anonymous function within

Passing two or more arrays to a Perl subroutine

不问归期 提交于 2019-12-29 04:42:05
问题 I am having trouble passing and reading arguments inside subroutine which is expected to have two arrays. sub two_array_sum { # two_array_sum ( (1 2 3 4), (2, 4, 0, 1) ) -> (3, 6, 3, 5) # I would like to use parameters @a and @b as simply as possible } # I would like to call two_array_sum here and pass two arrays, @c and @d I have seen and tried several examples from the web, but none of them worked for me. 回答1: There are two ways you can do this: by prototype by reference But before I