Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: my sub ?

by maverick (Curate)
on Mar 25, 2002 at 19:57 UTC ( #154194=note: print w/replies, xml ) Need Help??

in reply to my sub ?

You could make anonymous subrouting references to (x) in sub a & b like so:
sub a { my $ref = sub { print "x1\n"; }; &$ref; } sub b { my $ref = sub { print "x2\n"; }; &$ref; } &a; &b;
The only thing that will look different is the call to the fucntion if you can live with that.

perl -l -e "eval pack('h*','072796e6470272f2c5f2c5166756279636b672');"

Replies are listed 'Best First'.
Re: Re: my sub ?
by shotgunefx (Parson) on Mar 25, 2002 at 20:00 UTC
    Doesn't calling a sub reference with & imply the passing of your @_ ?


    "To be civilized is to deny one's nature."
      Yes it does, but if you just stick $ref; out there by itself, or with () after it, perl doesn't get that that's supposed to be a function call and throws a syntax error.

      You pretty much have to have the & as far as I know.

      Or you could use $ref->(); It's monday and I'm being a blonde.

      Update:Thanks chromatic, Fletch, and shotgunefx for the clue-by-four-ing.

      perl -l -e "eval pack('h*','072796e6470272f2c5f2c5166756279636b672');"

        $ref->() works just fine. As would &{$ref}().

        Unless I want the behavior, I usually call a code ref with
        Probably 9 out of 10 times you can get away with it but it can product subtle bugs. I wasn't originally aware of the implied passing of @_ and it was never a problem.. until.


        "To be civilized is to deny one's nature."

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://154194]
[marto]: hmm, may have to patch CPAN::Meta to move from search.cpan to metacpan in the META.json/yml files
[Corion]: marto: Heh - it seems that they plan to keep the links alive for a long time. But still, I plan on moving PM to use/generate the new links
[Corion]: And I think it's better to generate links to the new world instead of keeping the older links alive by generating new versions of them ;)
[marto]: yeah, I guess it's supposed to be a permanent redirect, but better to make the move where possible

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (12)
As of 2018-05-23 09:22 GMT
Find Nodes?
    Voting Booth?