Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: aliasing subs

by broquaint (Abbot)
on May 06, 2003 at 10:54 UTC ( [id://255836]=note: print w/replies, xml ) Need Help??


in reply to aliasing subs

This cannot be done easily I'm afraid as when you alias globs they merely point to the other glob (or in this case the subroutine). So when you call the aliased subroutine there's no difference to the the call except the name that you use (so caller() is useless). You can probably get the information using DB or one of the B:: modules though, but that would seem overkill in this case.

A simpler technique may be just to have a base sub and then wrap the other subs around it e.g

sub base { return "doing stuff" } sub foo { return base(). " in foo\n" } local *bar = sub { return base(). " in bar\n" }; foo(), bar(); __output__ doing stuff in foo doing stuff in bar
Simpler still use one of the Hook:: modules, or just take Zaxo's advice and use the ever-handy File::Spec.
HTH

_________
broquaint

Replies are listed 'Best First'.
Re: Re: aliasing subs
by december (Pilgrim) on May 06, 2003 at 12:09 UTC

    But the problem is that in the current form, all output comes from one pattern match. The base sub has to return either $a or $b, $pathname or $filename; if I use wrapping subs, I still loose the context.

    Unless ofcourse I add an extra parameter that specifies whether I'm interested in the pathname or the filename, but then I don't need the wrapping subs either.

    I could make seperate subs obviously, but then I would have to duplicate the same pattern matching, and that wouldn't be much fun training-wise, would it. ;)

      The base sub has to return either $a or $b, $pathname or $filename

      Err... no.

      sub do_the_match { my $dir = shift; my ($path, $name) = ($dir =~ /(.*)\/([^\/]*)$/); return ($path, $name); }
      --
      3dan

        Yes, I'm sorry. I meant *must* return, by my own specification. I know I can return a list, but I think that would make using the function a bit harder, since I usually only need either the path or the file.

        That's what complicates the matter; the output depends completely on what name was used to call the function, so if I can't find that out, my implementation is quite useless.

        It's something that I would like to see implemented in a new version of Perl, though. I think it would be handy in some cases... More clear to read than anonymous subs.

      That's not exactly true. Consider:
      sub match { my $context = (caller(1))[3]; my $arg = shift; my @result = $arg =~ /$pattern/; # $context =~ /dirname/ ? $result[0] : $context =~ /basename/ ? $result[1] : ... # etc. # otherwise, return the whole result vector: @result } sub dirname { &match } sub basename { &match } # etc.

      jdporter
      The 6th Rule of Perl Club is -- There is no Rule #6.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2024-04-23 12:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found