Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

What does the SHIFT bit do in a constructor

by Fian (Novice)
on May 25, 2001 at 16:32 UTC ( #83274=perlquestion: print w/replies, xml ) Need Help??

Fian has asked for the wisdom of the Perl Monks concerning the following question:

In a constructor what does this line mean/do?

Specifically what does the 'shift' bit do.

my $classname = shift;

I understand arrays, and push/pop from the end and shift/unshift from the start but I don't see where the value for $classname in the above line comes from.

Tanks Munksss.

Replies are listed 'Best First'.
Re: What does the SHIFT bit do in a constructor
by arhuman (Vicar) on May 25, 2001 at 16:35 UTC
    Your variable name say it all ;-)
    It's the class/package name used with bless...

    From perltoot :

    To do this, all we have to do is check whether what was passed in was a reference or not. If so, we were invoked as an object method, and we need to extract the package (class) using the ref() function. If not, we just use the string passed in as the package name for blessing our referent.
    sub new { my $proto = shift; my $class = ref($proto) || $proto; my $self = {}; $self->{NAME} = undef; $self->{AGE} = undef; $self->{PEERS} = []; bless ($self, $class); return $self; }
    It's done this way to permit inheritance...

    Check perlboot for other (good) informations...

    "Only Bad Coders Code Badly In Perl" (OBC2BIP)
      Note that many people who have used OO programming for a very long time (starting with merlyn who has been using it since the early 80's) detest the use of ref there. Object methods and instance methods are different, no need to confuse them. That said I would write the above as:
      sub new { my $self = bless {}, shift; @$self{'PEERS', 'NAME', 'AGE'} = []; return $self; }
      which looks cleaner to my eyes. YMMV.

        In the abstract, I understand and somewhat agree with this criticism. But when you get into a specific object implementation, it often makes a great deal of sense.

        It comes down to whether the reader will clearly understand what: my $other= $object->new(); is meant to do. I find that for many specific object implementations, this is a quite natural idiom. So I agree that you should think about what that idiom is supposed to mean in your specific class before you decide either way!

        A similar criticism is that you should not name your constructors "new" either. Again, I agree with this in the abstract but find that when I get down to real cases, the criticism usually doesn't apply. For many objects, trying to find a more descriptive name for the constructor that describes how I want to go about creating the object just adds confusion.

                - tye (but my friends call me "Tye")
Re: What does the SHIFT bit do in a constructor
by davorg (Chancellor) on May 25, 2001 at 16:38 UTC

    When you call a class method (for example my $obj = MYObject->new), the class name is automatically passed in as the first element of @_. A similar thing happens when you call object methods, the object is passed in as the first element of @_.

    For more details see perlboot, perltoot, perlobj and Damian Conway's book Object Oriented Perl.

    --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

Re: What does the SHIFT bit do in a constructor
by japhy (Canon) on May 25, 2001 at 16:37 UTC
    Constructors are usually called like so:
    $obj = Class->new(...); # or $obj = new Class ...;
    Either way, assuming there's no funny inheritance to deal with, you're doing the equivalent of
    $obj = Class::new('Class', ...);
    The same goes for when you're doing object calls (like $obj->method(...) or method $obj ...). Thus, the class name (or object) is the first argument to the function.

    japhy -- Perl and Regex Hacker
Re: What does the SHIFT bit do in a constructor
by Vynce (Friar) on May 25, 2001 at 17:01 UTC
    the one thing i don't think anybody mentioned yet explicitly is that shift shifts @_ by default.

    combine this with the fact that the OO syntax (Foo::Bar->new or new Foo::Bar) automatically puts the classname at the front of the argument list, and you see what's going on.
Re: What does the SHIFT bit do in a constructor
by jorg (Friar) on May 25, 2001 at 17:03 UTC
    Leaving all this class constructor stuff alone :
    if you use shift without an array as argument it will shift from @_ (which is the array of arguments passed to a sub) when called in a sub or from @ARGV (which is the array of arguments passed to the perl program) when called from the main program body.

    Thus :

    myshift("testme"); sub myshift{ print shift; #same result print shift @_; }


    Jorg

    "Do or do not, there is no try" -- Yoda
Re: What does the SHIFT bit do in a constructor
by diarmuid (Beadle) on May 25, 2001 at 16:51 UTC
    Well that looks like a line you seen in alot of subroutines eg
    sub do_something { my $classname = shift; .... } do_somthing($something);
    All it does is take the first element off the array @_ (which in this case contains the elements of the array passed to the subroutine) and puts that value into $classname

    From the perldoc

    See also `unshift', `push', and `pop'.  `Shift()'
    and `unshift' do the same thing to the left end of
    an array that `pop' and `push' do to the right end.
    

    Diarmuid

      Sorry, I just spotted that you mentioned "in a constructor", well it's a similar idea. The guys above have explained it well.

      Diarmuid

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2021-10-25 06:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (89 votes). Check out past polls.

    Notices?