Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

shift context...... 1

Status
Not open for further replies.

mjoyner

IS-IT--Management
Jul 26, 2009
46
US
I am new to PERL and I thought that shift had to do with arrays.

What is this doing?

$opt_H = shift unless ($opt_H);

And this one too?

($opt_t) || ($opt_t = shift) || ($opt_t = "GAUGE");


If exist OR ????? OR equals "Guage"

Thanks

[root@netwatch ~]# yum remove windows
Loaded plugins: fastestmirror
Setting up Remove Process
No Match for argument: windows
No Packages marked for removal

OH YEAH!
 
If this is outside of a subroutine, it's the same as:
$opt_H = shift @ARGV unless ($opt_H);
@ARGV is an array that contains the arguments that were passed to the script. So it's saying: unless $opt_H already contains a value, populate $opt_H with the first element of @ARGV

if this is in a subroutine, same deal, but it's acting on the default array @_ which contains arguments that were passed to the subroutine.

in the last example.. if $opt_t contains something, the statement ends there. If not, it tries to get something from @ARGV, if there's nothing in @ARGV it sets $opt_t to 'GAUGE'
 
Thanks chazoid!!!

It makes sense. This NAGIOS plugin is actually using the Getopt::Long perl module.

So it is confusing to me to see why they are playing with the parameters.

The parameters can come in any order, it would be wierd just to pull the next parameter from the array and assume that it is "option K"

Thanks again


[root@netwatch ~]# yum remove windows
Loaded plugins: fastestmirror
Setting up Remove Process
No Match for argument: windows
No Packages marked for removal

OH YEAH!
 
When Getopt::Long gets options (via getOptions), everything in @ARGV that it didn't make any sense of stays in @ARGV when it's done.

So for example

Code:
myscript -d --verbose --level 3 abc def xyz

and your GetOptions only tells it about -d, --verbose, and --level=i... it only parses that stuff out of the args, and leaves abc, def, and xyz in @ARGV ($ARGV[0] = "abc")

Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Kirlse,

That was exactly what I was missing.......... so:

($opt_t) || ($opt_t = shift) || ($opt_t = "GAUGE");

MEANS:

(if opt_t defined)
OR
(if theres @ARGV, assign it to $opt_t)
OR
assign "GUAGE" to $opt_t



[root@netwatch ~]# yum remove windows
Loaded plugins: fastestmirror
Setting up Remove Process
No Match for argument: windows
No Packages marked for removal

OH YEAH!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top