FAQ
I tried to find the answers to these in the Synopses, but I couldn't.
Plan B is to ask the mailing list.

- What does the "first" method/subroutine return when no elements of
the list match? Does it return the empty list? Does the return value
count as undefined/failure?

- What type is the $buf argument to the IO::Readable::read method
supposed to be? Should it be a Buf, and, if so, does the size of the
Buf's elements matter? How would one indicate a desired element size
when reading to an uninitialized Buf? A similar question also applies
to IO::Writeable::write. (Also, "Writable" is misspelled.)

- How does one get/set the number/size of elements in a Buf object?

- Exactly what happens if & when a call to open() fails? Is an
exception thrown, and, if so, how does one catch it?

- How does one declare multiple variables of the same type with a
single "my" statement? Is it "my Int ($x, $y);", "my(Int $x, Int
$y);", or something else? Are the parentheses still necessary when
declaring more than one variable? What about when initializing more
than one variable?

- Is there some sort of shortcut (e.g., of the form ".‽method") for
calling a method on an object if the object is defined and returning
undef if it is not defined? I was hoping that ".?method" could do
this, but it doesn't seem to (in Rakudo, at least).

Thank you in advance,
Minimiscience

Search Discussions

  • Chas. Owens at Jul 12, 2009 at 4:07 am

    On Sat, Jul 11, 2009 at 22:02, Minimisciencewrote:
    I tried to find the answers to these in the Synopses, but I couldn't.  Plan
    B is to ask the mailing list.

    - What does the "first" method/subroutine return when no elements of the
    list match?  Does it return the empty list?  Does the return value count as
    undefined/failure?
    from http://perlcabal.org/syn/S32/Containers.html
    grep takes a list or array of values and returns a lazily evaluated list
    comprised of all of the values from the original list for which the $test
    smart-matches as true.
    ...
    first works exactly like grep but returns only the first matching value.

    Since grep is defined as returning a list of matching elements and first is
    defined as being the same as grep, I would say that it returns an empty list
    if nothing matches. The empty list is one of the false values.
    - What type is the $buf argument to the IO::Readable::read method supposed
    to be?  Should it be a Buf, and, if so, does the size of the Buf's elements
    matter?  How would one indicate a desired element size when reading to an
    uninitialized Buf?  A similar question also applies to IO::Writeable::write.
    (Also, "Writable" is misspelled.)
    from http://perlcabal.org/syn/S32/IO.html#IO%3A%3AReadable

    It is important to realize that this is "raw" read. You're going to have
    plain octets stored in $buf, if this is actually encoded data, you're
    going to need to encode it later, or use "getc" or other
    IO::Readable::Encoded methods.

    Given that it claims that $buf will will contain plain octets, $buf should
    be a Buf8.
    - How does one get/set the number/size of elements in a Buf object?
    No idea, I am not certain it has been defined yet.
    - Exactly what happens if & when a call to open() fails?  Is an exception
    thrown, and, if so, how does one catch it?
    The open function does throw an exception, and you catch it with try:

    #!perl6

    use v6;

    my $fh;
    try {
    $fh = open "foo.txt";
    CATCH {
    say "oops, file doesn't exist"
    }
    };

    say "made it";
    - How does one declare multiple variables of the same type with a single
    "my" statement?  Is it "my Int ($x, $y);", "my(Int $x, Int $y);", or
    something else?  Are the parentheses still necessary when declaring more
    than one variable?  What about when initializing more than one variable?
    At least currently, only

    my Int $x;
    my Int $y;

    works.
    - Is there some sort of shortcut (e.g., of the form ".‽method") for calling
    a method on an object if the object is defined and returning undef if it is
    not defined?  I was hoping that ".?method" could do this, but it doesn't
    seem to (in Rakudo, at least).
    Not a clue.
    Thank you in advance,
    Minimiscience


    --
    Chas. Owens
    wonkden.net
    The most important skill a programmer can have is the ability to read.
  • Patrick R. Michaud at Jul 12, 2009 at 1:46 pm

    On Sun, Jul 12, 2009 at 12:07:14AM -0400, Chas. Owens wrote:
    On Sat, Jul 11, 2009 at 22:02, Minimisciencewrote:
    - How does one declare multiple variables of the same type with a single
    "my" statement?  Is it "my Int ($x, $y);", "my(Int $x, Int $y);", or
    something else?  Are the parentheses still necessary when declaring more
    than one variable?  What about when initializing more than one variable?
    At least currently, only
    my Int $x;
    my Int $y;
    works.
    The following works also -- note there has to be a space between
    "my" and the leading paren, or else it's treated like a function call:

    my (Int $x, Int $y);
    - Is there some sort of shortcut (e.g., of the form ".‽method") for calling
    a method on an object if the object is defined and returning undef if it is
    not defined?  I was hoping that ".?method" could do this, but it doesn't
    seem to (in Rakudo, at least).
    Not a clue.
    ".?method" seems to work for me in Rakudo:

    $ cat x
    my $x = undef;
    say ($x.?foo).perl;

    $ ./perl6 x
    undef

    Pm
  • Minimiscience at Jul 12, 2009 at 4:33 pm

    On Jul 12, 2009, at 9:46 AM, Patrick R. Michaud wrote:
    ".?method" seems to work for me in Rakudo:

    $ cat x
    my $x = undef;
    say ($x.?foo).perl;

    $ ./perl6 x
    undef
    This doesn't work when the variable is assigned to a typed container:

    #!/usr/bin/env perl6
    use v6;
    my Str $x = undef;
    say ($x.?comb).perl;

    This gives a "Use of uninitialized value" warning when run.

    -- Minimiscience
  • Minimiscience at Jul 12, 2009 at 4:29 pm

    On Jul 12, 2009, at 12:07 AM, Chas. Owens wrote:
    Since grep is defined as returning a list of matching elements and
    first is
    defined as being the same as grep, I would say that it returns an
    empty list
    if nothing matches. The empty list is one of the false values.
    Does the empty list count as undefined for the purposes of //?
    Namely, does "(^5).first({ @data[$^i] == $something }) // -1" work as
    expected?
    Given that it claims that $buf will will contain plain octets, $buf
    should
    be a Buf8.
    Then why is the type of $buf conspicuously absent from the method
    signature? It seems like there should be more to it than that.

    -- Minimiscience
  • Yary at Jul 12, 2009 at 5:46 pm

    On Sun, Jul 12, 2009 at 9:29 AM, Minimisciencewrote:
    On Jul 12, 2009, at 12:07 AM, Chas. Owens wrote:

    Since grep is defined as returning a list of matching elements and first
    is
    defined as being the same as grep, I would say that it returns an empty
    list
    if nothing matches.  The empty list is one of the false values.
    Does the empty list count as undefined for the purposes of //?  Namely, does
    "(^5).first({ @data[$^i] == $something }) // -1" work as expected?
    Let's try it
    perl6
    my @data=2..6;my $something=7;say (^5).first({ @data[$^i] ==
    $something }) // -1;
    -1
    # set $something = 4 and the result is 2, correct, @data[2] == 4
  • Moritz Lenz at Jul 12, 2009 at 6:01 pm

    Minimiscience wrote:
    I tried to find the answers to these in the Synopses, but I couldn't.
    Plan B is to ask the mailing list.

    - What does the "first" method/subroutine return when no elements of
    the list match? Does it return the empty list? Does the return value
    count as undefined/failure?
    I guess it will return a Nil, which is the empty list in list context,
    and undef in item context.
    - How does one get/set the number/size of elements in a Buf object?
    Since it's a mutable container, I'd speculate that you'd just use it
    numeric context. Or call the .elems method. But that need speccing.
    - Exactly what happens if & when a call to open() fails? Is an
    exception thrown, and, if so, how does one catch it?
    it fail()s (ie returns an unthrown exception)

    Cheers,
    Moritz

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupperl6-users @
categoriesperl
postedJul 12, '09 at 2:02a
activeJul 12, '09 at 6:01p
posts7
users5
websiteperl6.org

People

Translate

site design / logo © 2021 Grokbase