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

callback or call now?

by John M. Dlugosz (Monsignor)
on Oct 16, 2002 at 18:39 UTC ( [id://205792]=perlmeditation: print w/replies, xml ) Need Help??

In Module Design strawman - Exporter::VA, I’m planning a feature where if the argument is a code ref, it is called immediatly (by the code exploring this structure that is; obviously an undecorated function would be called “immediatly” when building the data structure) to get the real result. e.g. fobble => \&figure_it_out.

However, there seems to be a demand for importing by hard link, also. This is easy for other types of symbols, but a contradiction for functions. That is, $x => \$_internal_x is distinguishable from $y => \&generate_y, but if the symbol is itself a function, you can't tell them apart.

Originally I decided to let it mean “call back”, since you can write &foo => sub { \&_hard_link } to get the other case. But can it be simpler/easier?

Well, I think I've got it!

How about, for function symbols, a code ref means “now”, and a ref to a scalar means “later”?

That is,

&foo => \&figure_it_out, &bar => \\&hard_link
The extra backslash is only one more character, rather than any other form of decorating it which has to surround it and/or involve more words. And, the meaning of backslash is consistant with “take it literally, not special meaning now”.

I think the idea would make a handy idiom for any situation like this, when you're configuring things with code blocks. For example, in user-interface building, Tk has a few forms with various semantics.

Any thoughts?

—John

Replies are listed 'Best First'.
Re: callback or call now?
by grinder (Bishop) on Oct 16, 2002 at 21:49 UTC

    You say it's "a reference to a scalar", but don't you mean to say it's a reference to a code reference? You can add as many backslashes as you like, you only have to dereference them sufficiently to get at the underlying referent.

    I think I understand, but I'm a bit puzzled by your terminology. Seems like a cool trick to me, let me see if I understand you correctly:

    #! /usr/bin/perl -w use strict; sub foo { "this is foo" } sub bar { "this is bar" } my @later; sub do_it { my $ref = shift; if( ref($ref) eq 'CODE' ) { print "do it now: ", $ref->(), "\n"; } elsif( ref($$ref) eq 'CODE' ) { push @later, $$ref; } } do_it( \&foo ); do_it( \\&bar ); print "deferred ", $_->(), "\n" for @later;

    Is that what you mean? If so, yeah, I like it.


    print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u'
Re: callback or call now?
by rir (Vicar) on Oct 17, 2002 at 01:42 UTC
    This is a puppy that will grow up to bite you. It is cute and appealing though.

    Your solution implies that all scalars will be scalar coderefs, at least for some set of keys. And Then this set of
    keys, or its universal nature, must be unmistakeable otherwise your attempted idiomatic meaning becomes lost
    because it is just normal code.

    If parameters may be needed you will need to deal with array references or such anyway.

    A good programmers' idiom is complete in itself, like an idiomatic phrase in natural language.
    Your meaning exists in one place, but must be interpreted in another. This plays against its utility.
    I would be more explicit:

    $foo => \&immediate, $bar => sub { "immediate" }, $bey => [ \&immediate, $arg, ], $mah => [ -defer, \&delayed, ],
      If a "hard link" value is always a double-reference, written with two backslashes and the (properly-typed symbol), then it is consistent. We have:
      &func => \\&internal_func, $x => \\$private_variable, &foo => \&figure_it_out_at_import_time, $y => \&figure_this_out_too,
      The form
      $z => \$what_is_this
      which could be unambiguous for everything other than code symbols can instead be incorrect. Always use a double-ref for hard link.

      —John

        If a "hard link" value is always a double-reference, written with two backslashes and the (properly-typed symbol), then it is consistent.

        My point was aimed in the other direction: are all double-references ever and always going to correspond to your current definition of a "hard link". And not just in this block of code.

        That level of unambiguity you won't attain. That may be setting the bar unnecessarily high.

        I do think that in two years, the puppy will a dog, and anyone who looks will be tracing the calls to find the point of the extra indirection.

        You could document it, but then what is the point?

Re: callback or call now?
by erikharrison (Deacon) on Oct 17, 2002 at 16:13 UTC

    I think that you've got it turned around. The common case, in my mind, would be import by ref instead of import by callback. The second slash means "Something a little funny is going on" which would be the callback case, to my mind.

    Cheers,
    Erik

    Light a man a fire, he's warm for a day. Catch a man on fire, and he's warm for the rest of his life. - Terry Pratchet

      If backslash simply means "not the default", then its meaning varies with your expectations.

      I'm thinking that it more-specifically means "take it literally, not as instructions to find the answer.". Then you get this progression:

      foo => &bar means call bar now when the structure is being built. foo => \&bar means call bar later, when foo is imported (and bar can see the context of what the impoter is asking for). Finally, foo => \\&bar will call bar even later, when foo is actually called after being imported.

      —John

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-04-26 09:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found