Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: what does shift do?

by dmmiller2k (Chaplain)
on May 19, 2002 at 20:27 UTC ( [id://167706]=note: print w/replies, xml ) Need Help??


in reply to what does shift do?

shift applies to an array, as in, shift @a or, shift(@a). It removes the first (i.e., 0th) element from the array, shifting the remaining elements back by one (hence the name, I suppose), and returns the first element as its value.

So, if you have an array, initialized like this:

my @a = ( 'a', 'b', 'c'); my $x = shift @a;

then after this code executes, $x will contain the scalar value, 'a', and @a will contain, ( 'b', 'c' ).

In lexical scope (i.e., within a subroutine), shift by itself, without any arguments, implicitly acts upon the variable @_, which contains the subroutine's arguments.

At file scope (i.e., outside of any subroutine), it implicitly operates instead upon @ARGV (also known as ::@ARGV, or main::@ARGV), which contains the command line parameters passed to the script.

See the docs, "shift".

dmm

There are no stupid questions -- just stupid-making responses

Replies are listed 'Best First'.
Re: Re: what does shift do?
by dev2000 (Sexton) on May 19, 2002 at 20:36 UTC
    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).

      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.
      

      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.
        

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-19 22:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found