Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Emoji Progress Spinners

by pme (Monsignor)
on Feb 07, 2021 at 13:46 UTC ( [id://11128014]=note: print w/replies, xml ) Need Help??


in reply to Emoji Progress Spinners

Hi Monks,

Can someone tell me where else this function chaining EXPR (chr hex) can be used in perl? Thanks.

my @chars = map chr hex, @code_points;

Replies are listed 'Best First'.
Re^2: Emoji Progress Spinners
by haukex (Archbishop) on Feb 07, 2021 at 14:43 UTC
    Can someone tell me where else this function chaining EXPR (chr hex) can be used in perl?

    It's just a combination of function calls and map:

    $ perl -MO=Deparse,-p -e 'map chr hex, @code_points' map(chr(hex($_)), @code_points);

    The behavior of chr and hex can be achieved with the (_) prototype, though prototypes are one of those "only use this if you know what you are doing" kind of thing because they change how Perl code is actually parsed (see also).

    Parentheses can be omitted in calls to subs which the compiler has already seen.

    $ perl -wMstrict -le 'sub foo {} foo "bar"' # ok $ perl -wMstrict -le 'sub foo {} foo("bar")' # ok $ perl -wMstrict -le 'foo("bar"); sub foo {}' # ok $ perl -wMstrict -le 'foo "bar"; sub foo {}' # fails String found where operator expected at -e line 1, near "foo "bar"" (Do you need to predeclare foo?) syntax error at -e line 1, near "foo "bar"" Execution of -e aborted due to compilation errors.

    The only thing special here is map and grep, which have as an alternative to their map {...} ... and grep {...} ... forms the map ..., ... and grep ..., ... forms. The former two can be emulated using prototypes (e.g. (&@)), while the latter two can't.

      Parentheses can be omitted in calls to subs which the compiler has already seen.

      And to named operators such as hex and chr.

      Seeking work! You can reach me at ikegami@adaelis.com

Re^2: Emoji Progress Spinners
by kcott (Archbishop) on Feb 07, 2021 at 19:06 UTC

    G'day pme,

    Thankyou for your excellent question. Those of us who've been coding Perl for a quarter of a century or more, may often rely upon muscle memory rather than using any actual brain cells. When I start a new script, I often find that I've witten

    #!/usr/bin/env perl use strict; use warnings;

    before I've even thought about the problem.

    Any Perl function that has function LIST can be expanded using function function-returning-list LIST. This is pretty much what 🦛 says.

    Many commands allow the use of $_, check the documentation.

    Hopefully, combined with other responses, that answers your question. Of course, if anything is still unclear, ask for further clarification.

    — Ken

      Thank you all for your answers. Meanwhile I found answer myself to my question.
      @a2 = map chr hex, @a1;
      can be written as
      @a2 = map chr(hex()), @a1; # or @a2 = map chr(hex($_)), @a1;
      And the original syntax can be used in a simple assignment statement like this.
      $x = char hex 41; # which means $x = 'A';
Re^2: Emoji Progress Spinners
by hippo (Bishop) on Feb 07, 2021 at 14:41 UTC

    Any function (or sub) which returns a value (scalar, list, ref, whatever) can be acted upon by another function.


    🦛

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11128014]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2024-04-19 22:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found