Re: Shift Question

2005-09-27 Thread Chris Devers
On Tue, 27 Sep 2005, Dave Adams wrote:

> What is the purpose of the line "my $msg = shift;"? 

In the context of subroutines, it copies the first scalar argument 
passed to the routine to the variable $msg. If more than one argument 
was passed, the others aren't touched by this statement -- as you note, 
they are left alone in the @_ array. 

Therefore, you sometimes see things like this:

  sub multiply {
my $first_factor  = shift;
my $second_factor = shift;
my $product = $first_factor * $second_factor;
return $product;
  }

And other times see that abbreviated a bit, as:

  sub multiply {
my ( $first_factor, $second_factor ) = @_;
my $product = $first_factor * $second_factor;
return $product;
  }

They work about identically, but the first form gives you room to expand 
the script in the future, by doing something like this:

my $first_factor  = shift;
my $second_factor = shift or 1; 

This lets you define a default value if the one noted wasn't specified.

More broadly, you can use that trick to handle unspecified arguments, 
as:

  my $msg = shift or "No message specified.";

Et cetera.


Also, don't bother with subroutine prototypes, e.g.

  sub write_log($)

or

  sub multiply($$)

as they just make the code harder to maintain, while doing little to 
make the code clearer or more robust. The reasoning behind them is good, 
but in practice they're more trouble than they're worth.
  


-- 
Chris Devers

‡}“5Z¶’K4!
-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 


Re: Shift Question

2005-09-27 Thread John W. Krahn
Mulander wrote:
> If I understood you question properly you want to know why people use
> shift in subrutines and how does shift work.
> 
> I will try to make it short:
> shift works on lists, it removes the first element of the list

perldoc -q "What is the difference between a list and an array"

You can't use shift() on a list, only on an array:

$ perl -le'my $x = shift qw(10 11 12 13 14)'
Type of arg 1 to shift must be array (not list) at -e line 1, at EOF
Execution of -e aborted due to compilation errors.


> and returns it as a lvalue

The term 'lvalue' has a specific meaning in Perl (and CS) which usually means
something that can be assigned to.  Functions like substr() and vec() are
lvalue subs while shift() is not.

$ perl -le'my $x = "abc"; substr( $x, 2 ) = "xyz";'
$ perl -le'my @x = qw"a b c"; shift( @x ) = "xyz";'
Can't modify shift in scalar assignment at -e line 1, near ""xyz";"
Execution of -e aborted due to compilation errors.


> 
> [snip]
> 
> Hope this will clear some things up, you can check also:
> perldoc -f shift
> perldoc -f unshift
> perldoc -f pop
> perldoc -f push

And also:

perldoc -f splice
perldoc perlsub



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Shift Question

2005-09-27 Thread Wiggins d'Anconia
Mulander wrote:
> If I understood you question properly you want to know why people use
> shift in subrutines and how does shift work.
> 
> I will try to make it short:
> shift works on lists, it removes the first element of the list ( the 0
> indexed element ) and returns it as a lvalue ( if there are no more
> elements in a list it returns undef ).
> Here is an example:
> 
> my @list = qw(a b c d);
> print shift @list,"\n";
> print "my list is now @list\n";
> print shift @list,"\n";
> print "my list is now @list\n";
> print shift @list,"\n";
> print "my list is now @list\n";
> print shift @list,"\n",
> print "my list is now @list\n";
> 
> this should print something like this ( althoug I did not have the
> time to test it )
> a
> my list is now b c d
> b
> my list is now c d
> c
> and so on...
> If you use shift without giving it the list name to work on it will
> refer to @_ or @ARGV ( it is decided upon the file scope ). So when
> you define a sub like this:
> 
> sub somesub {
> my $arg1 = shift;
> }
> 
> You did something simmilar to my $arg1 = $_[0]; but more elegant ( in
> my opinion ) and you removed the first element from the arguments list
> ( witch is quite usefull ).
> 
> Hope this will clear some things up, you can check also:
> perldoc -f shift
> perldoc -f unshift
> perldoc -f pop
> perldoc -f push
> 

Additionally a very common idiom and where you may be seeing this so
much is within OOP style programs/modules. In the case of object
oriented syntax Perl automagically tacks on the class name or instance
object as the first argument to the subroutine (method). So you will
very commonly see,

sub class_method {
  my $class = shift;
  my (%other_args) = @_;
}

or

sub instance_method {
  my $self = shift;
  my (@other_args) = @_;
}

These would be called like,

Class::Object->class_method(key1 => 'val1');

or

my $object = new Class::Object;
$object->instance_method('arg1','arg2');

I often use this syntax for writing non-OOP libraries just because it
has become so common otherwise. In which case having the name of the
class really doesn't matter much.

Finally, it is common to use 'shift' with subroutines that take a very
common x # of arguments, and then take a "hash" like list.

HTH,

http://danconia.org

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




RE: Shift Question

2005-09-27 Thread Ryan Frantz


> -Original Message-
> From: Ryan Frantz
> Sent: Tuesday, September 27, 2005 5:27 PM
> To: Dave Adams; beginners perl
> Subject: RE: Shift Question
> 
> 
> 
> > -Original Message-
> > From: Dave Adams [mailto:[EMAIL PROTECTED]
> > 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.

I meant to be a little more explicit; in your code, the list element is
the string 'Test Msg to SysLog'.  shift'ing it into $msg gives you
something to print.

> 
> >
> > #!/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: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> > <http://learn.perl.org/> <http://learn.perl.org/first-response>
> >
> 
> 
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
> 


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




RE: Shift Question

2005-09-27 Thread Ryan Frantz


> -Original Message-
> From: Dave Adams [mailto:[EMAIL PROTECTED]
> 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: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>  
> 


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: shift question

2004-08-30 Thread John W. Krahn
[EMAIL PROTECTED] wrote:
I found this in a template for creating subroutines, this is the base
that is created when you use the template to create the subroutine.
So now the newbie part, why would you place "my  $par1 = shift;" in the
subroutine template, and what does it do??
Basically I am trying to find out if I need to modify the template or
not. Any help would be greatly appreciated.
Oh and btw I looked at the shift function and it applies to the @_
array, which is not being used in this subroutine, and neither is @par1
, so my only guess would be that the template is creating a verifiably
empty variable called $par1 .
sub Irfan
{
  my  $par1 = shift;
return ;
} # --  end of subroutine Irfan  --
Inside of a subroutine "shift" with no arguments is the same as "shift @_" and 
outside of a subroutine "shift" with no arguments is the same as "shift 
@ARGV".  You can read all about subroutines in the perlsub document.

perldoc perlsub

John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



RE: shift question

2004-08-30 Thread Moon, John
Subject: RE: shift question

[EMAIL PROTECTED] wrote:
> Ok fantastic, I totally understand that, and if there were going to be
> more than one thing passed, just insert $par2 = shift; on the next
> line and then the second argument is in $par2, I assume.right??

Yes.

You might also see it this way:

   my ($par1, $par2) = @_;

The latter construct is not destructive of @_, while using shift() is. It
usually doesn't matter.

...

Please understand that you are passing a "list"... 

So 

&func($a, @b);

is different than 

&func($a, $b);

Consider the example:
perl -e '$a=1;
@b=(2,3,4);
$c=5;]
&func($a,@b,$c);
sub func{
my ($a, @b,$c) = @_;
print "$a,$c,$b[3]\n";}'

Please note the absents of a value for $c in the sub above... 

So...

&func($a,[EMAIL PROTECTED],$c);

perl -e '
$a=1;
@b=(2,3,4);
$c=5;
&func($a,[EMAIL PROTECTED],$c);
sub func{
my ($a, $b,$c) = @_;
print "$a,$c,$b->[2]\n";}'


jwm

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




RE: shift question

2004-08-30 Thread Wiggins d Anconia
> [EMAIL PROTECTED] wrote:
> > Ok fantastic, I totally understand that, and if there were going to be
> > more than one thing passed, just insert $par2 = shift; on the next
> > line and then the second argument is in $par2, I assume.right??
> 
> Yes.
> 
> You might also see it this way:
> 
>my ($par1, $par2) = @_;
> 
> The latter construct is not destructive of @_, while using shift() is. It
> usually doesn't matter.
> 

Bob is giving correct, though terse answers.  Subroutines receive their
arguments in @_, in the special case that a subroutine acts as a method
(aka in OOP (Object Oriented) Perl) then the first argument is either
the object/instance/referent or the class name. In *most* cases this
argument is considered different (in meaning) than the rest, because it
is tacked on to the beginning by Perl automagically, so to retrieve the
object/class and to restore the "user specified" argument list you
generally want to C it off. So you end up with,

sub method1 {
  my $self = shift;
  ...
  # now @_ contains only those arguments provided by the user
}

Along with the possibly more commonly used, 

sub method2 {
  my $self = shift;
  my ($arg1, $arg2) = @_;
  ...
}

sub method3 {
  my $self = shift;
  my (@args) = @_;
  ...
}

sub method4 {
  my $self = shift;
  my (%args) = @_;
  ...
}

And finally the above iterations with the more specific...

sub method5 {
  my $class = shift;
  ...
}

Because Perl's rules in this area are loose the same applies to
functional (non-OOP) techniques as well, though less idiomatically
(IMHO).  In other words less often will you want to shift off the
arguments one at a time, or that the first argument is significantly
different (in meaning) than the others.  Having said that, it is not a
rule and should not be taken as one, however can be applied when just
making a template. 

Personally I would suggest adding a blank C as the last line of
the sub, and possibly providing a documentation template.  To me these
are more important than how you are going to accept your arguments.

http://danconia.org

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




RE: shift question

2004-08-30 Thread Bob Showalter
[EMAIL PROTECTED] wrote:
> Ok fantastic, I totally understand that, and if there were going to be
> more than one thing passed, just insert $par2 = shift; on the next
> line and then the second argument is in $par2, I assume.right??

Yes.

You might also see it this way:

   my ($par1, $par2) = @_;

The latter construct is not destructive of @_, while using shift() is. It
usually doesn't matter.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




RE: shift question

2004-08-30 Thread christopher . l . hood
Ok fantastic, I totally understand that, and if there were going to be
more than one thing passed, just insert $par2 = shift; on the next line
and then the second argument is in $par2, I assume.right??



Chris Hood  
Investigator Verizon Global Security Operations Center 
Email: [EMAIL PROTECTED] 
Desk: 972.399.5900

Verizon Proprietary 

NOTICE - This message and any attached files may contain information
that is confidential and/or subject of legal privilege intended only for
the use by the intended recipient.  If you are not the intended
recipient or the person responsible for delivering the message to the
intended recipient, be advised that you have received this message in
error and that any dissemination, copying or use of this message or
attachment is strictly forbidden, as is the disclosure of the
information therein.  If you have received this message in error please
notify the sender immediately and delete the message. 

-Original Message-
From: Bob Showalter [mailto:[EMAIL PROTECTED] 
Sent: Monday, August 30, 2004 2:41 PM
To: Christopher L. Hood; [EMAIL PROTECTED]
Subject: RE: shift question

[EMAIL PROTECTED] wrote:
> OK here comes the newbie question.
> 
> I found this in a template for creating subroutines, this is the base
> that is created when you use the template to create the subroutine.
> 
> So now the newbie part, why would you place "my  $par1 = shift;" in
> the subroutine template, and what does it do??
> 
> Basically I am trying to find out if I need to modify the template or
> not. Any help would be greatly appreciated.
> 
> Oh and btw I looked at the shift function and it applies to the @_
> array, which is not being used in this subroutine, and neither is
> @par1 , so my only guess would be that the template is creating a
> verifiably empty variable called $par1 .
> 
> 
> 
> sub Irfan
> {
>   my  $par1 = shift;
> 
> return ;
> } # --  end of subroutine Irfan  --

@_ holds the actual arguments passed to the subroutine call.

So if you call the sub as:

   Irfan('foo');

Then inside the sub:

   my $par1 = shift;

will assign the first argument ('foo'), to $par1

This is a very common idiom.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>





--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




RE: shift question

2004-08-30 Thread Bob Showalter
[EMAIL PROTECTED] wrote:
> OK here comes the newbie question.
> 
> I found this in a template for creating subroutines, this is the base
> that is created when you use the template to create the subroutine.
> 
> So now the newbie part, why would you place "my  $par1 = shift;" in
> the subroutine template, and what does it do??
> 
> Basically I am trying to find out if I need to modify the template or
> not. Any help would be greatly appreciated.
> 
> Oh and btw I looked at the shift function and it applies to the @_
> array, which is not being used in this subroutine, and neither is
> @par1 , so my only guess would be that the template is creating a
> verifiably empty variable called $par1 .
> 
> 
> 
> sub Irfan
> {
>   my  $par1 = shift;
> 
> return ;
> } # --  end of subroutine Irfan  --

@_ holds the actual arguments passed to the subroutine call.

So if you call the sub as:

   Irfan('foo');

Then inside the sub:

   my $par1 = shift;

will assign the first argument ('foo'), to $par1

This is a very common idiom.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Shift question...

2001-12-21 Thread Michael R. Wolf

Wim De Hul <[EMAIL PROTECTED]> writes:

> my $var = shift;
> 
> I thought that shift puts a variable in an array? What does this mean?

Did you check the documentation on "shift" *before* posting
the question?




>From "Programming Perl":

3.2.143 shift

shift ARRAY
shift

This function shifts the first value of the array off and
returns it, shortening the array by 1 and moving everything
down. (Or up, or left, depending on how you visualize the
array list.) If there are no elements in the array, the
function returns the undefined value. If ARRAY is omitted,
the function shifts @ARGV (in the main program), or @_ (in
subroutines). See also unshift, push, pop, and splice. The
shift and unshift functions do the same thing to the left
end of an array that pop and push do to the right end.


-- 
Michael R. Wolf
All mammals learn by playing!
   [EMAIL PROTECTED]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: Shift question...

2001-12-20 Thread Bob Showalter

> -Original Message-
> From: Wim De Hul [mailto:[EMAIL PROTECTED]]
> Sent: Thursday, December 20, 2001 10:48 AM
> To: [EMAIL PROTECTED]
> Subject: Shift question...
> 
> 
> Hello guys ( and girls),
> 
> While I was reading a script, I saw the lines:
> 
> my $var = shift;
> 
> I thought that shift puts a variable in an array? What does this mean?

Nope, that's unshift().

shift() removes the first element from the array, while unshift()
adds a element at the front of the array. push() and pop() add
and remove an element from the end of the array.

shift() without an array works on @ARGV if outside of a sub, 
or @_ inside a sub.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Shift question...

2001-12-20 Thread Michael Stidham

The way I understand it
Shift removes the first element of the list and moves (or "shifts") every 
remaining element of the list to the left to cover the gap. Shift then 
returns the removed element.ex:)
@list = qw(1,2,3);
$fisrtval = shift(@list);
Hope this helped...Stiddy


>From: Wim De Hul <[EMAIL PROTECTED]>
>To: [EMAIL PROTECTED]
>Subject: Shift question...
>Date: Thu, 20 Dec 2001 16:48:16 +0100
>
>Hello guys ( and girls),
>
>While I was reading a script, I saw the lines:
>
>my $var = shift;
>
>I thought that shift puts a variable in an array? What does this mean?
>
>Thanks,
>
>Wim.
>
>
>--
>To unsubscribe, e-mail: [EMAIL PROTECTED]
>For additional commands, e-mail: [EMAIL PROTECTED]
>


_
Send and receive Hotmail on your mobile device: http://mobile.msn.com


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: Shift question...

2001-12-20 Thread John Edwards

shift returns the first value of the array. When no array is defined it
operates on the default array @_

You will most likely see the line you mention as the first line of a
subroutine. E.g

&call_sub('John');

sub call_sub {
my $name = shift;
print "Name is $name\n";
}

What's happening here is that the value passed to the subroutine call_sub is
stored in a locally scoped default array for that subroutine. When you
perform a shift on the array it returns the data passed. This is stored in
the local $name scalar for use within the subroutine. Some other ways you
may see this written are

my $name = $_[0];   # Uses a direct route to the first element of the
array
my ($one, $two) = @_; # When passing more than one value to the subroutine,
assign them to two different scalars within the array

HTH

John

-Original Message-
From: Wim De Hul [mailto:[EMAIL PROTECTED]]
Sent: 20 December 2001 15:48
To: [EMAIL PROTECTED]
Subject: Shift question...


Hello guys ( and girls),

While I was reading a script, I saw the lines:

my $var = shift;

I thought that shift puts a variable in an array? What does this mean?

Thanks,

Wim.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


--Confidentiality--.
This E-mail is confidential.  It should not be read, copied, disclosed or
used by any person other than the intended recipient.  Unauthorised use,
disclosure or copying by whatever medium is strictly prohibited and may be
unlawful.  If you have received this E-mail in error please contact the
sender immediately and delete the E-mail from your system.



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]