CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
[SOLVED]Problem with saving argument from input @ARGV to scalar

 



waldauf
Novice

Aug 16, 2012, 1:24 AM

Post #1 of 5 (1524 views)
[SOLVED]Problem with saving argument from input @ARGV to scalar Can't Post

Hello,

I run perl script with input arguments from command line. At first I recognize first argument and then I would like to save second argument of @ARGV array to scalar variable. But I have problem with saving in SWITCH-CASE structure:


Code
#!/usr/bin/perl -w 
# Using SWITCH (case) statment
use Switch;

foreach my $arg (@ARGV) {
print "Input arguments: @ARGV\n";
print "Actual argument: $arg\n";
print "Second argument: $ARGV[1]\n\n";
switch ($arg) {
case "-c" {
print "Actual argument $arg\n";
print "Second argument: $ARGV[1]\n";
my $secarg = $ARGV[1];
#my $secarg = shift (@ARGV);
print "Shifted argument: $secarg";
}
else {
print "Unknown argument: $arg\n";
}
}
print "End of cycle.\n\n";
}


print "Shifted argument: $secarg";


And result which I see in command line:

Code
$ ./test.pl -c a 
Name "main::secarg" used only once: possible typo at ./test.pl line 25.
Input arguments: -c a
Actual argument: -c
Second argument: a

Actual argument -c
Second argument: a
Shifted argument: aEnd of cycle.

Input arguments: -c a
Actual argument: a
Second argument: a

Unknown argument: a
End of cycle.

Use of uninitialized value in concatenation (.) or string at ./test.pl line 25.
Shifted argument:


Can somebody help me resolve this problem? Or is in Perl some function to sort input arguments?


Thx,

Waldauf


(This post was edited by waldauf on Aug 16, 2012, 4:52 AM)


BillKSmith
Veteran

Aug 16, 2012, 4:44 AM

Post #2 of 5 (1516 views)
Re: [waldauf] Problem with saving argument from input @ARGV to scalar [In reply to] Can't Post

Do not ignore the warning message! It is trying to tell you that the $secarg that you are printing is a global variable, not lexical that you store into. (With "use strict;"this program would not have even tried to run.) You want both statements to refer to the same lexical variable. Remove the 'my' from $secarg and declare it (add my $secarg) before the for loop.
Good Luck,
Bill


waldauf
Novice

Aug 16, 2012, 4:52 AM

Post #3 of 5 (1514 views)
Re: [BillKSmith] Problem with saving argument from input @ARGV to scalar [In reply to] Can't Post

BillKSmith, you're the smart head ;]. Your advice helped me.

Thx,

Waldauf


FishMonger
Veteran / Moderator

Aug 16, 2012, 6:39 AM

Post #4 of 5 (1506 views)
Re: [waldauf] [SOLVED]Problem with saving argument from input @ARGV to scalar [In reply to] Can't Post

Here are a couple points that Bill did not cover.

The Switch module is a source filter which is known to be buggy and was removed from core as of v5.13.1 and is no longer being recommended. It has been replaced with given/when.
http://perldoc.perl.org/perlsyn.html#Switch-Statements

Rather than manually parsing the args like that, it would be much better to use the Getopt::Long module.
http://search.cpan.org/~jv/Getopt-Long-2.38/lib/Getopt/Long.pm


waldauf
Novice

Aug 16, 2012, 7:56 AM

Post #5 of 5 (1499 views)
Re: [FishMonger] [SOLVED]Problem with saving argument from input @ARGV to scalar [In reply to] Can't Post

FishMonger, you're smarter head :]]. Thx for your sent URL links. That is what I wanted. It is better then my SWITCH-CASE structure.

Waldauf

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives