On Thu, Nov 24, 2011 at 7:19 PM, JPH <jph4dot...@xs4all.nl> wrote: > I found the script below at http://hints.macworld.com/** > dlfiles/is_tcp_port_listening_**pl.txt<http://hints.macworld.com/dlfiles/is_tcp_port_listening_pl.txt> > > I am trying to figure out what's happening at lines 20-23. > Why is the author using 'shift ||' and not a plain $host = $hostname; > > Anyone to enlighten me? > > Thanks! > > JP > > --- > > 1 #!/usr/bin/perl -w > 2 # > 3 # Author: Ralf Schwarz <r...@schwarz.ath.cx> > 4 # February 20th 2006 > 5 # > 6 # returns 0 if host is listening on specified tcp port > 7 # > 8 > 9 use strict; > 10 use Socket; > 11 > 12 # set time until connection attempt times out > 13 my $timeout = 3; > 14 > 15 if ($#ARGV != 1) { > 16 print "usage: is_tcp_port_listening hostname portnumber\n"; > 17 exit 2; > 18 } > 19 > 20 my $hostname = $ARGV[0]; > 21 my $portnumber = $ARGV[1]; > 22 my $host = shift || $hostname; > 23 my $port = shift || $portnumber; > > -- > To unsubscribe, e-mail: beginners-unsubscr...@perl.org > For additional commands, e-mail: beginners-h...@perl.org > http://learn.perl.org/ > > > I'm not sure what the meaning is of this but the thing that is happening is simple enough. You have @ARGV which contains [ 'A host name', 'A port number']. On line 20 you set $hostname = $ARGV[0] = 'A host name' and on line 21 you set $portnumber = $ARGV[1] = 'A port number'. So far so good, then on line 22 you assign $host the contents of $_[0] or $hostname, and on 23 you set $host = $_[1] or $portnumber;
Now I am not familiar enough with the perl innards to fully understand the logic behind this construction, but basically in this setup you will prefer the use of @_ over the information in @ARGV after all the value after the || will only be used in case the shift argument results in an undef assignment to $host or $port. Not much clearer I'm sorry but that is as far as I understand it. Regards, Rob