Professional Web Applications Themes

alias a function - PERL Beginners

Hello list, I was looking into the best way (and for what reasons) you'd create an "alais" function. For example: If you want foo() and bar() to be able to be used interchangeably would it be best to do: sub foo { return "Howdy $_[0]"; } sub bar { return foo(_); } or do a typeglob: sub foo { return "Howdy $_[0]"; } *bar = \&foo; or another way ?? TIA Lee.M - JupiterHost.Net...

  1. #1

    Default alias a function

    Hello list,

    I was looking into the best way (and for what reasons) you'd create an
    "alais" function.

    For example:

    If you want foo() and bar() to be able to be used interchangeably would
    it be best to do:

    sub foo { return "Howdy $_[0]"; }
    sub bar { return foo(_); }

    or do a typeglob:

    sub foo { return "Howdy $_[0]"; }
    *bar = \&foo;

    or another way ??

    TIA

    Lee.M - JupiterHost.Net
    Jupiterhost.Net Guest

  2. #2

    Default Re: alias a function

    Jupiterhost.Net wrote:
     

    for these simple examples, it doesn't matter much and i would go with the
    first method. functionality wise, they are pretty much the same but there
    are differences that you might want to observe when your foo function gets
    more complicated. for example, the first method has an extra function call
    which means it's a tiny bit slower and more importantly, the stock frame
    will be different and caller return different trace for this purpose so you
    might want to consider 'goto &foo' instead. if none of those matters to
    you, i would just use the first method.

    david
    --
    s$s*$+/<tgmecJ"ntgR"tgjvqpC"vuwL$;$;=qq$
    \x24\x5f\x3d\x72\x65\x76\x65\x72\x73\x65
    \x24\x5f\x3b\x73\x2f\x2e\x2f\x63\x68\x72
    \x28\x6f\x72\x64\x28\x24\x26\x29\x2d\x32
    \x29\x2f\x67\x65\x3b\x70\x72\x69\x6e\x74
    \x22\x24\x5f\x5c\x6e\x22\x3b\x3b$;eval$;
    David Guest

  3. #3

    Default Re: alias a function



    david wrote:
     
    >
    >
    > for these simple examples, it doesn't matter much and i would go with the
    > first method. functionality wise, they are pretty much the same but there
    > are differences that you might want to observe when your foo function gets
    > more complicated. for example, the first method has an extra function call
    > which means it's a tiny bit slower and more importantly, the stock frame
    > will be different and caller return different trace for this purpose so you
    > might want to consider 'goto &foo' instead. if none of those matters to[/ref]

    So that woudl be:
    sub foo { return "Howdy $_[0]"; }
    sub bar { goto &foo; }
    ??
    goto() just kind of uses the current _ if I remeber right, correct?
     

    Thanks David I appreciate the input! I'll have to perldoc -f goto since
    I'm not realy familiar with it and its own issues :)

    Also a bit of benchmarking may also be in order now that I have a 3rd
    way! and more to think about contect wise.

    Thanks again!

    Lee.M - JupiterHost.Net

     
    Jupiterhost.Net Guest

  4. #4

    Default Re: alias a function

    From: "JupiterHost.Net" <net> 
    >
    > So that woudl be:
    > sub foo { return "Howdy $_[0]"; }
    > sub bar { goto &foo; }
    > ??
    > goto() just kind of uses the current _ if I remeber right, correct?[/ref]

    Not just that. If all you wanted was to call foo with the current _
    youd write it like this:

    sub bar { &foo; }

    The
    goto &foo;
    does more. It replaces bar() by foo() in call stack. So if foo()
    calls caller() or croaks() it wil look like bar() was never called at
    all, the call stack will look like foo() was called instead.

    In either case ... I would use the typeglob solution:

    sub bar; *bar = \&foo;

    sub bar; is there to declare the function, so that Perl knows there
    is such a function. This is important if you do not use braces around
    parameters:

    bar 1, 2, 3;

    Jenda
    ===== cz === http://Jenda.Krynicky.cz =====
    When it comes to wine, women and song, wizards are allowed
    to get drunk and croon as much as they like.
    -- Terry Pratchett in Sourcery

    Jenda Guest

  5. #5

    Default Re: alias a function

     
    >
    >
    > Not just that. If all you wanted was to call foo with the current _
    > youd write it like this:
    >
    > sub bar { &foo; }
    >
    > The
    > goto &foo;
    > does more. It replaces bar() by foo() in call stack. So if foo()
    > calls caller() or croaks() it wil look like bar() was never called at
    > all, the call stack will look like foo() was called instead.
    >[/ref]

    Excellent info as usual Jenda!
     

    That is what I will do as
    1) If you say to do it I beleive it :) (Seriously I *love* Mail:Sender)
    and
    2) If I'm looking at the Benchmarking I did correctly glob is faster
    (adding the sub bar; seemed to make it even faster!!)


    Benchmark: timing 100000 iterations of glob, goto, sub...
    glob: 0.036653 wallclock secs ( 0.01 usr + 0.00 sys = 0.01 CPU)
    10000000.00/s (n=100000)
    (warning: too few iterations for a reliable count)
    goto: 0.121074 wallclock secs ( 0.06 usr + 0.00 sys = 0.06 CPU)
    1666666.67/s (n=100000)
    (warning: too few iterations for a reliable count)
    sub: 0.104079 wallclock secs ( 0.06 usr + 0.00 sys = 0.06 CPU)
    1666666.67/s (n=100000)
    (warning: too few iterations for a reliable count)
    Rate sub goto glob
    sub 1666667/s -- -0% -83%
    goto 1666667/s 0% -- -83%
    glob 10000000/s 500% 500% --

     

    Awesome info, I really appreciate those details you bring out!
     
    Jupiterhost.Net Guest

  6. #6

    Default Re: alias a function

     

    Excellent, thansk for the info David!
     
    >
    >
    > always a good idea[/ref]

    As good as use strict; ad use warnings; :)
     
    Jupiterhost.Net Guest

  7. #7

    Default Re: alias a function

    Jupiterhost.Net wrote:
     
    >>
    >>
    >> for these simple examples, it doesn't matter much and i would go with the
    >> first method. functionality wise, they are pretty much the same but there
    >> are differences that you might want to observe when your foo function
    >> gets more complicated. for example, the first method has an extra
    >> function call which means it's a tiny bit slower and more importantly,
    >> the stock frame will be different and caller return different trace for
    >> this purpose so you might want to consider 'goto &foo' instead. if none
    >> of those matters to[/ref]
    >
    > So that woudl be:
    > sub foo { return "Howdy $_[0]"; }
    > sub bar { goto &foo; }
    > ??
    >[/ref]

    yes
     

    that's one feature of goto &foo and another has to do with transfering the
    current call to another so caller is fooled to believe the function is
    actually called directly
     

    always a good idea

    david
    --
    s$s*$+/<tgmecJ"ntgR"tgjvqpC"vuwL$;$;=qq$
    \x24\x5f\x3d\x72\x65\x76\x65\x72\x73\x65
    \x24\x5f\x3b\x73\x2f\x2e\x2f\x63\x68\x72
    \x28\x6f\x72\x64\x28\x24\x26\x29\x2d\x32
    \x29\x2f\x67\x65\x3b\x70\x72\x69\x6e\x74
    \x22\x24\x5f\x5c\x6e\x22\x3b\x3b$;eval$;
    David Guest

Similar Threads

  1. How to Alias a Function name within a Class?
    By Alberto in forum PHP Development
    Replies: 3
    Last Post: June 4th, 06:50 PM
  2. [PHP] problem: creating global alias inside function
    By Mike Ford in forum PHP Development
    Replies: 3
    Last Post: July 29th, 03:34 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139