writing a function like map, sort, grep

Discussion in 'Perl Misc' started by John, Nov 2, 2004.

  1. John

    John Guest

    Is there a way to write a function that ban be run like the
    aforementioned ones, i.e.

    Foo BLOCK LIST

    I want to do this because I sometimes want LIST to be a real list, and
    sometimes a function. I know there are other ways to do it, I was just
    curious if something like this can be implemented easily.

    TIA, John
    John, Nov 2, 2004
    #1
    1. Advertising

  2. John

    John Guest

    John wrote:
    > Is there a way to write a function that ban be run like the
    > aforementioned ones, i.e.
    >
    > Foo BLOCK LIST
    >
    > I want to do this because I sometimes want LIST to be a real list, and
    > sometimes a function. I know there are other ways to do it, I was just
    > curious if something like this can be implemented easily.
    >
    > TIA, John
    >
    >


    I don't mean a function, I mean a CODE reference, which I could
    determine inside Foo (hopefully).
    John, Nov 2, 2004
    #2
    1. Advertising

  3. John wrote:
    > Is there a way to write a function that ban be run like the
    > aforementioned ones, i.e.
    >
    > Foo BLOCK LIST


    You can use prototypes

    sub Foo(&@)

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Nov 2, 2004
    #3
  4. John

    Matija Papec Guest

    X-Ftn-To: John

    John <> wrote:
    >Is there a way to write a function that ban be run like the
    >aforementioned ones, i.e.
    >
    > Foo BLOCK LIST
    >
    >I want to do this because I sometimes want LIST to be a real list, and
    >sometimes a function. I know there are other ways to do it, I was just
    >curious if something like this can be implemented easily.


    It seems that it can, I saw some example using prototypes,

    sub apply (&@) {
    my $sub = shift;
    $sub->() for my @ret = @_;
    @ret;
    }

    print
    $str = join "", apply {tr/abc/def/; s/d/123/g} qw/jen dva/;



    --
    Matija
    Matija Papec, Nov 2, 2004
    #4
  5. John

    Ben Morrow Guest

    Quoth Gunnar Hjalmarsson <>:
    > John wrote:
    > > Is there a way to write a function that ban be run like the
    > > aforementioned ones, i.e.
    > >
    > > Foo BLOCK LIST

    >
    > You can use prototypes
    >
    > sub Foo(&@)


    Note that this will *not* allow the foo EXPR, LIST usage of map, grep,
    nor the named or missing sub of sort (as is reflected by the fact that
    prototype returns undef for all of these).

    Ben

    --
    "If a book is worth reading when you are six, *
    it is worth reading when you are sixty." - C.S.Lewis
    Ben Morrow, Nov 2, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. js
    Replies:
    4
    Views:
    363
    Fredrik Lundh
    Jan 3, 2007
  2. Patrick Kowalzick
    Replies:
    5
    Views:
    456
    Patrick Kowalzick
    Mar 14, 2006
  3. sajuptpm
    Replies:
    7
    Views:
    793
    Terry Reedy
    Sep 8, 2010
  4. Navin
    Replies:
    1
    Views:
    667
    Ken Schaefer
    Sep 9, 2003
  5. Marc Girod

    map and grep again...

    Marc Girod, Jul 16, 2010, in forum: Perl Misc
    Replies:
    3
    Views:
    85
Loading...

Share This Page