in reply to Re: How to check if a scalar value is numeric or string?
in thread How to check if a scalar value is numeric or string?

Actually the exact requirement is as below:

1. Use GetOptions to process the input command line.
2. Do the required validation. This will result in modifying the command line paramteres
   and resultant will be stored in a hash.
3. Use this hash in order to form the command line again.

The problem is while doing the 3rd step, I need to distinguish if a given argument is a toggle or a parameter. So, I want to distinguish between 1( toggle ) and '1'( argument ).
  • Comment on Re^2: How to check if a scalar value is numeric or string?

Replies are listed 'Best First'.
Re^3: How to check if a scalar value is numeric or string?
by ikegami (Patriarch) on Aug 27, 2009 at 15:02 UTC

    Why don't you save a copy of the command line before calling GetOptions?

    If you can't because you're rearranging the command line somehow, you'll need to pass your reconstructor some of the information you pass to GetOptions (as in, whether a parameter is a toggle or takes a value).

      Since I am modifying the resultant hash drastically, it is very difficult to keep track of the changes in the hash.

        Since I am modifying the resultant hash drastically,

        Simply looking at the variable can change its internal format, so you don't want to rely on that.

        $ perl -MDevel::Peek -le'$|=1; $x=1; Dump $x; print "x=$x"; Dump $x' SV = IV(0x816a438) at 0x814f6cc REFCNT = 1 FLAGS = (IOK,pIOK) <--- $x is a number IV = 1 x=1 SV = PVIV(0x8150b10) at 0x814f6cc REFCNT = 1 FLAGS = (IOK,POK,pIOK,pPOK) <--- $x is a number and a string IV = 1 PV = 0x81651b8 "1"\0 CUR = 1 LEN = 4

        If you wanted to spend some effort, you could actually use the list you passed to GetOptions. If not, the following could do with very little effort:

        sub reconstruct_toggle { ... } sub reconstruct_with_val { ... } sub reconstruct_opt_val { ... } reconstruct( foo => \&reconstruct_toggle, bar => \&reconstruct_with_val, special => sub { ... }, );
Re^3: How to check if a scalar value is numeric or string?
by bv (Friar) on Aug 27, 2009 at 15:35 UTC

    Your requirement mentions GetOptions. Does this mean you are using Getopt::Long? If so, how are you specifying the option? opt=s, opt:s, opt=i, opt:i, opt, etc?

    Getopt::Long is usually good about setting sane values. If you asked for an integer with :i, it will set your value to 0 if there was no argument given, so you can test with exists to see if it was set.

    $,=' ';$\=',';$_=[qw,Just another Perl hacker,];print@$_;
Re^3: How to check if a scalar value is numeric or string?
by GrandFather (Saint) on Aug 27, 2009 at 21:30 UTC

    If you know at the point where you store the value that it is a 1 or a '1', then store '1' (including the quote marks) for the string version.


    True laziness is hard work