Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
If you call split like this @rv = split $_[0], @_[ 1 .. 2 ] you get the return value undef.

This is not true, it gives (99). Update 1: Presuming @_ = (qr{:}, 'a:b:c', 99) from the given example. Update 2: A capturing group in the regex does not change the result for this case.

To understand this behaviour you need to consider function prototypes. The compiler recognises split being called with two arguments, where the second is a list that gets assigned to a single scalar. This results in the last element of the list being assigned to the scalar, just like in

$x = (1, 2, 4); # $x = 4
This behaviour is caused by the usage of an array slice. A "pure" array would be evaluated in scalar context giving its length.

In the following example the sub show_args sees its arguments just like split.

UPDATE 3: As suspected by bojinlund in Re^2: My problems to understand the Perl documentation and proven by LanX in Re^4: My problems to understand the Perl documentation, the sub show_args does not see its arguments exactly as split.

#!/usr/bin/perl use strict; use warnings; use Data::Dump 'dd'; use feature 'say'; sub show_args ($_;$) { dd @_; } sub call_show_args { say "single:"; show_args $_[0], $_[1], $_[2]; say "flat:"; show_args @_; say "slice:"; show_args @_[0 .. 2]; say "slice split:"; show_args $_[0], @_[1 .. 2]; say "array split:"; my $p = shift; show_args $p, @_; } $_ = 'default'; call_show_args qr{:}, 'a:b:c', 99; __DATA__ single: (qr/:/, "a:b:c", 99) flat: (3, "default") slice: (99, "default") slice split: (qr/:/, 99) array split: (qr/:/, 2)



In reply to Re: My problems to understand the Perl documentation [updated] by jo37
in thread My problems to understand the Perl documentation by bojinlund

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 or How to display code and escape characters are good places to start.
Log In?

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2022-05-26 14:53 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (93 votes). Check out past polls.