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

Re: Re: aliasing subs

by december (Pilgrim)
on May 06, 2003 at 12:09 UTC ( [id://255853]=note: print w/replies, xml ) Need Help??


in reply to Re: aliasing subs
in thread aliasing subs

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. ;)

Replies are listed 'Best First'.
Re: Re: Re: aliasing subs
by edan (Curate) on May 06, 2003 at 12:27 UTC
    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.

Re: Re: Re: aliasing subs
by jdporter (Paladin) on May 06, 2003 at 12:46 UTC
    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://255853]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (6)
As of 2024-04-23 16:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found