http://qs321.pair.com?node_id=167707


in reply to Re: what does shift do?
in thread what does shift do?

So, why exactly was it needed at all in my example?

Wouldn't:
send_http_header('text/plain'); print("mod_perl rules!\n");
be identical? (guess I should try this, huh?)

It didn't work... Undefined subroutine !

P.S. the original example was the ENTIRE program, not within a subroutine, that's why I was so puzzled as to what was shifting (and why).

Replies are listed 'Best First'.
Re: Re: Re: what does shift do?
by Juerd (Abbot) on May 19, 2002 at 20:48 UTC

    sub greet { my $name = shift; print "Hello, $name\n"; } greet('dev2000');

    In mod_perl, with Apache::Registry, your script is cached in a sub, and that sub gets the Apache object as its argument. You could of course write this:

    $_[0]->send_http_header('text/plain'); $_[0]->print("mod_perl rules!\n");
    But it's easier to have a named variable for that, especially when you use other subs and your @_ gets overwritten.

    Back to the point, your script:
    my $r = shift; ...
    is wrapped in a sub so it can be cached:
    package Some::URL::Based::Package::Name;use Apache qw(exit);sub handle +r { #line 1 script.pl my $r = shift; ... }
    And suddenly, it all makes sense.

    - Yes, I reinvent wheels.
    - Spam: Visit eurotraQ.
    

Re(2): what does shift do?
by dmmiller2k (Chaplain) on May 19, 2002 at 20:41 UTC
    Because $r is being used as an object, by calling its send_http_header() and print() methods. Without the shift (I presume this is inside a subroutine), the object represented by the reference $r would never be referenced and the code would not work.

    P.S., without seeing the context in which your example lives, it I do not exactly know what kind of object $r is , but from its method names, I suspect it's one of the Http::XXX'en.

    dmm

    If you GIVE a man a fish you feed him for a day
    But,
    TEACH him to fish and you feed him for a lifetime

      P.S., without seeing the context in which your example lives, it I do not exactly know what kind of object $r is , but from its method names, I suspect it's one of the Http::XXX'en.

      The context is mod_perl, and probably Apache::Registry, which wraps scripts in subs for caching.

      - Yes, I reinvent wheels.
      - Spam: Visit eurotraQ.