Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Thanks haukex for the answer!r

Sometimes there are contradictions. ... Can you show some examples?

My original problem was to understand the way from the subroutine call in the script source code (the list of expressions) to the list of parameters (formal argument) in the definition of the subroutine.

LISTs do automatic interpolation of sublists. That is, when a LIST is evaluated, each element of the list is evaluated in list context, and the resulting list value is interpolated into LIST just as if each individual element were a member of LIST (from List value constructors)
Like the flattened incoming parameter list, the return list is also flattened on return. (from perlsub)
The Perl model for function call and return values is simple: all functions are passed as parameters one single flat list of scalars, and all functions likewise return to their caller one single flat list of scalars. Any arrays or hashes in these call and return lists will collapse, losing their identities --but you may always use pass-by-reference instead to avoid this. Both call and return lists may contain as many or as few scalar elements as you'd like. (from perlsub)

Based on those quotes, learning a little about the Perl interpreter and using B::Deparse and B::Concise, I have learnt a more perlish way of thinking.

  • LIST is a list of expressions in the source code.
  • When LIST is evaluated the result is stored on the argument stack in the interpreter. During this evaluation the result is flatten.
  • This is the argument list, the input to the subroutine. This list contains references to the argument values.
  • The argument values can be accessed from the subroutine definition by using @_ and $_n. @_ is the list of argument values. $_n is the value of argument n.

I have several times reread the parts of the documentation which cover the subroutine call. Have still problems to understand everything. I do not understand it so well that I can say there are contradictions

Probably I am still thinking that the text describes what you see in the source and not the result after evaluation.

The word "list" is often used. Often it is not clear to which list it refers. The word list is also part of the term "list value". Is "list values" many "list value" or is it the values in a list?

Interface to split

How or where can you find the restrictions on the arguments to split?

This split $_[0], $_[1], $_[2]; and this split $_[0], @_[1,2]; should have given the same result ("a", "b", "c")!

use strict; use warnings; use 5.010; use Path::Tiny qw( path ); use Data::Dump qw(dump dd ddx); use B::Concise qw(set_style add_callback walk_output); sub concise { my $case = shift; my $fh; say ''; my $walker = B::Concise::compile( '-src', '-basic', $case ); $fh = path( 'split_call_' . $case )->openw_utf8; #walk_output($fh); $walker->(); say ''; $walker = B::Concise::compile( '-src', '-exec', $case ); $walker->(); } my $pat = ':'; my $str = 'a:b:c'; my $limit = -1; my @par = ( $pat, $str, $limit ); if (1) { sub splitC { my @rv = split $_[0], $_[1], $_[2]; return \@rv; } ddx splitC(@par); concise('splitC'); } if (1) { sub splitC1 { my @rv = split $_[0], @_[1,2]; return \@rv; } ddx splitC1(@par); concise('splitC1'); } __DATA__ Part of output: # split_call.pl:34: ["a", "b", "c"] main::splitC: 8 <1> leavesub[1 ref] K/REFC,1 ->(end) - <@> lineseq KP ->8 # 30: my @rv = split $_[0], $_[1], $_[2]; 1 <;> nextstate(main 3 split_call.pl:30) v:*,&,{,x*,x&,x$,$,fea +=1 ->2 4 </> split(/":"/ => @rv:3,4)[t5] vK/LVINTRO,ASSIGN,LEX ->5 3 <|> regcomp(other->4) sK ->9 - <1> ex-aelem sK/2 ->3 - <1> ex-rv2av sKR/STRICT,1 ->- 2 <#> aelemfast[*_] s ->3 - <0> ex-const s ->- - <1> ex-aelem sK/2 ->a - <1> ex-rv2av sKR/STRICT,1 ->- 9 <#> aelemfast[*_] s/key=1 ->a - <0> ex-const s ->- - <1> ex-aelem sK/2 ->4 - <1> ex-rv2av sKR/STRICT,1 ->- a <#> aelemfast[*_] s/key=2 ->4 - <0> ex-const s ->- # 31: return \@rv; 5 <;> nextstate(main 4 split_call.pl:31) v:*,&,{,x*,x&,x$,$,fea +=1 ->6 - <@> return K ->- - <0> pushmark s ->6 7 <1> srefgen sK/1 ->8 - <1> ex-list lKRM ->7 6 <0> padav[@rv:3,4] lRM ->7 # split_call.pl:45: [-1] main::splitC1: i <1> leavesub[1 ref] K/REFC,1 ->(end) - <@> lineseq KP ->i # 41: my @rv = split $_[0], @_[1,2]; b <;> nextstate(main 10 split_call.pl:41) v:*,&,{,x*,x&,x$,$,fe +a=1 ->c e </> split(/":"/ => @rv:10,11)[t5] vK/LVINTRO,ASSIGN,LEX,IMPLI +M ->f d <|> regcomp(other->e) sK ->j - <1> ex-aelem sK/2 ->d - <1> ex-rv2av sKR/STRICT,1 ->- c <#> aelemfast[*_] s ->d - <0> ex-const s ->- o <@> aslice sK ->p j <0> pushmark s ->k - <1> ex-list lK ->m - <0> ex-pushmark s ->k k <$> const[IV 1] s ->l l <$> const[IV 2] s ->m n <1> rv2av[t4] sKR/STRICT,1 ->o m <#> gv[*_] s ->n p <$> const[IV 0] s ->e # 42: return \@rv; f <;> nextstate(main 11 split_call.pl:42) v:*,&,{,x*,x&,x$,$,fe +a=1 ->g - <@> return K ->- - <0> pushmark s ->g h <1> srefgen sK/1 ->i - <1> ex-list lKRM ->h g <0> padav[@rv:10,11] lRM ->h

In reply to Re^2: Split does not behave like a subroutine by bojinlund
in thread Split does not behave like a subroutine by bojinlund

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-03-28 12:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found