in reply to perl typecasting

Something that will help you is to use strictures (use strict; use warnings;). They pick up errors and questionable code early.

In the case of Perl being shown a non-integer string in a context where a number is expected use warnings; would tell you that something odd is happening:

use strict; use warnings; my $notAnInt = '#10'; print "It's 10\n" if $notAnInt == 10; print "It's 0\n" if $notAnInt == 0; print "It's #10\n" if $notAnInt eq '#10';

prints:

Argument "#10" isn't numeric in numeric eq (==) at noname1.pl line 6. It's 0 It's #10

Note that a non-numeric string tends to be numified as 0.


Perl is environmentally friendly - it saves trees

Replies are listed 'Best First'.
Re^2: perl typecasting
by perl_junkie (Acolyte) on Feb 04, 2008 at 22:23 UTC
    Thanks guys..!! This helps..!!!

    But I dont have 'strict' and 'warnings' at my workplace. I tried using it and it says that the module could not be found. This is turning out to be quite a problem...!!

    But, i think I can use the \d matching given above. This should enable me to confirm if all characters are valid numbers.

    Again.. Thanks for your help.. And sorry I didnt post code. I was not aware of the norms of the forums here and also the code slice was kinda large....!!!

      Not having strict and warnings seems quite odd, as I believe they're part of core Perl. As for the code you've been requested to post, it would be the code that is being used to perform the validations, which I would expect to be fairly short: a single regexp to validate integers (untested: $integer =~ /^ *[+-]?[0-9]+$/;, i.e., any number of blanks, an optional sign, and at least one digit before the end-of-string) and a more complex regexp to validate floats (in my other language, I'd just try to read a string as float or as an integer and handle the cases where there's a non-zero error code returned).


      emc

      Information about American English usage here and here.

      Floating point issues? Read this before posting: http://docs.sun.com/source/806-3568/ncg_goldberg.html

      The pragmata strict and warnings are part of the core distribution and have been for many years. Your perl installation may be damaged or your PERL5LIB environment variable may be wrong. Check the output from perl -V.

      In particular, note the version of perl and the values of PERL5LIB and @INC.


      TGI says moo

      You can turn on warnings, at least, from the command line when you invoke perl, e.g.,

      perl -w dubious_script.pl

      You can also turn on warnings from within a script with the statement

      BEGIN { $^W = 1; }

      Without access to the strict module, you will have trouble using many, if not most, of the modules referred to by others. A very strange situation, as has been mentioned; are you sure it's not available?

        Anonymous monk,

        If I use the strict module, it throws an error saying that the used module cannot be found. Is there anyway I can confirm this? check some path may be..... I experience the same errors on using the date and the time modules. The only one that works is the Fcntl module. Please let me know your thoughts.

        Thanks..!!!