RE: Shift Question





> -----Original Message-----
> From: Dave Adams [mailto:davidlamontadams@xxxxxxxxx]
> Sent: Tuesday, September 27, 2005 5:17 PM
> To: beginners perl
> Subject: Shift Question
>
> QUESTION: What is the purpose of the line "my $msg = shift;"? I am
> guessing it is for the @_ array but what list element is there to be
> shifted off? It seems that the shift function is in a lot of
> subroutines and I am confused as to their purpose.

>From what I understand shift defaults to @_ in a subroutine unless
specified otherwise. If your sub looked like this:

sub write_log($) {
my $msg;
print "$msg\n";
}

$teststring = 'Test Msg to SysLog';
write_log($teststring);

You'd get no output because $msg is undef.

>
> #!/bin/perl -w
> (my $PROGNAME = $0) =~ s/^.*\///;
> (my $SHORT_PROGNAME = $PROGNAME) =~ s/\.pl$//;
> my $SYSLOG_PRIORITY = 'info';
> my $SYSLOG_FACILITY = 'local4';
>
> use Sys::Syslog;
>
> sub write_log($) {
> # write a datestamped message to the logfile, e.g.:
> # &write_log('some message');
>
> my $msg = shift;
>
> # print message to logfile:
> openlog($SHORT_PROGNAME,'pid','');
> syslog("$SYSLOG_FACILITY|$SYSLOG_PRIORITY", '%s', "$msg");
> closelog();
>
> }
> $teststring = 'Test Msg to SysLog';
> write_log($teststring);
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@xxxxxxxx
> For additional commands, e-mail: beginners-help@xxxxxxxx
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>

.



Relevant Pages

  • sitemap generator for Perl
    ... I want to run the sitemap generator ... Returns the minimum number of links to traverse from the root URL of ... my $class = shift; ...
    (perl.beginners)
  • Re: How can I create instantiable objects (not classes)?
    ... a child object inherits not only its parent object's ... sub fee { ... my $class = shift; ... For example, an object of type Car might receive a message named "ticket," and since a car does not know what to do with a ticket, it would pass that message to an object of type Driver. ...
    (comp.lang.perl.misc)
  • Re: Packages and returning errors
    ... > array intact. ... sub is_a_instance_method { ... my $class = shift; ... You need to fix the scope of $error by moving its declaration outside ...
    (comp.lang.perl.misc)
  • Re: passing database data to a sub
    ... > I'm not sure of the difference, why isn't it a subroutine? ... > sure about this 'shift' thing anyway :-) ... > sub teardown ... > # Setup the template to use for the output. ...
    (perl.beginners)
  • Re: Massive failed FTP attempts.
    ... made it a little more generic so that it could monitor any log file. ... To run as a daemon and monitor proftpd, sending lockout notifications to ... sub prune_old_entries ... my $line = shift; ...
    (Security-Basics)