shift @ARGV not working - PerlMonks
文章推薦指數: 80 %
rayh has asked for the wisdom of the Perl Monks concerning the following question: Quick one here. If called as script.pl -s, why doesn't this ...
Wedon'tbitenewbieshere...much
PerlMonks
shift@ARGVnotworking
byrayh(Initiate)
Log inCreate a new user
The Monastery Gates
Seekers of Perl Wisdom
Meditations
Cool Uses For Perl
Obfuscation
Tutorials
Poetry
Reviews
Perl News
Donate
Recent Threads
Newest Nodes
Super Search
PerlMonks Discussion
What's New
onMay09,2013at18:37 UTC
(#1032814=perlquestion:printw/replies,xml)
NeedHelp??
rayhhasaskedforthewisdomofthePerlMonksconcerningthefollowingquestion:
Quickonehere.Ifcalledasscript.pl-s,whydoesn'tthisprint"-s"?
#!/usr/bin/perl
if($ARGV[0]eq'-s'){
my$argument=shift@ARGV;
}
print"$argument";
[download]
Iknowthecomparisonistruebuttheshiftdoesn'tseemhappy.IfItype"printshift@ARGV",Icanseethe-s.Howeveritdoesn'twanttobeassignedtoavariable.Commentonshift@ARGVnotworkingDownloadCode
Repliesarelisted'BestFirst'.
Re:shift@ARGVnotworking
bytoolic(Bishop)onMay09,2013at18:44 UTC
VariablesandScoping:
#!/usr/bin/perl
my$argument;
if($ARGV[0]eq'-s'){
$argument=shift@ARGV;
}
print"$argument";
[download]
Tip#1fromtheBasicdebuggingchecklist:
usestrictandwarnings
[reply][d/l]
Re^2:shift@ARGVnotworking
byrayh(Initiate)onMay09,2013at18:58 UTC
Derp.Duh.Iwasgettingalittletoofancyinalargerscriptandoverlookedtheuseof"my"inanewblock.Thanks![reply]
Re:shift@ARGVnotworking
byblue_cowdawg(Monsignor)onMay09,2013at18:47 UTC
#!/usr/bin/perl-w
usestrict;
foreachmy$i(0..$#ARGV){
printf"\$ARGV[%d]=\"%s\"\n",$i,$ARGV[$i];
}
while(my$arg=shift@ARGV){
printf"%s\n",$arg;
}
#
#--------------8
延伸文章資訊
- 1perl_how.txt
底下利用一些例子,逐步介紹Perl 的使用從變數的定義到流程、迴圈的控制. ... @ARGV 是全部的參數@ARGV[0] 是第一個參數, @ARGV[1] 是第二個參數print "\@AR...
- 2shift in Perl - Perl Maven
The shift function in Perl will remove the first value of the array passed to it and return it. ....
- 3defaults to shifting @ARGV : Special Variables - Perl
#!/usr/bin/perl -w use strict; sub version { print "version\n"; } my $option = shift; # defaults ...
- 4Perl Display And Pass Command Line Arguments With @argv
Use the $ARGV[n] to display argument. We use the $#ARGV to get total number of passed argument to...
- 5Perl shift Function - Tutorialspoint
This function returns the first value in an array, deleting it and shifting the elements of the a...