Feeds:
Posts
Comments

Archive for the ‘Language’ Category

Function pointers

A useful techniques is the ability to have pointer to functions. Their declaration is easy: write the declaration as it would be for the function, example:

int my_func(int var_a, int var_b);

and the following form make a pointer which pointed to my_func:

int (*my_func_p)(int, int);

Once you have got the pointer, you can assign the address of the function you declared by using just its name:

like an array, a function name is returned into an address when it is used in an expression. You can call the function using one of two form:

  1. (more…)

Read Full Post »

Error handling in Perl

  • Using unless
    • The unless is the logical opposite to if. Statements can completely bypass the success status and only be executed if the expression returns false.
      • example: die “Error message\n”unless(chdir(“dir”))
      • the unless statement is best used when you want to raise an error only if the expression fails.
  • Using the conditional operator
    • For some short tests, you can use the conditional operator
      • example: print (exists($hash{$key}))
  • Using Warn function
    • Warn function just raises a warning, a message is printed to STDERR, but no further action is taken
      • example: chdir(“dir”) or warn “warn message”;
  • Using Die function
    • Die function works like warn, except that is also calls exit, within a normal script, this function has the effect of immediately terminating execution.
      • example: chdir(“dir”) or die “die message”;
  • Using Carp function
    • Carp function is the basic equivalent of warn and prints the message to STDERR without existing the script and printing the script name
      • example: carp “error message”; will result in “error message, at scriptname.pl line number”
  • Using Cluck function
    • Cluck function is a sort of supercharged carp, it follows the same basic principle but also prints a stack trace of all the modules that led to the function being called, including the information on the original script.
  • Using Croak function
    • Croak function is the equivalent of die. it reports the caller one level up. Like die, this function also exists the script after reporting the error to STDERR.

Read Full Post »

The main() function’s double life :::

  • int main (int argc, char *argv[]);
  • int main (int argc, char **argv);

The global variables set by getopt() include:

  1. optarg ::: A pointer to the current option argument, if there is one
  2. optind ::: An index of the next argv pointer to process when getopt() is called again
  3. optopt ::: This is the last known option

If you don’t want getopt to print error message, you can set opterr to zero

————————————————————————————————-

Read Full Post »

Tie tech in Perl

hide an object class in a simple variable::::::{ tie $scalar, ‘package’, ARGUMENTS… }

——————————————————————————————————————————————————

The tie() function binds a variable to a class (package) that will provide the implementation for access methods for that variable. Once this magic has been performed, accessing a tied variable automatically triggers method calls in the proper class. The complexity of the class is hidden behind magic methods calls. The method names are in ALL CAPS, which is a convention that Perl uses to indicate that they’re called implicitly rather than explicitly–just like the BEGIN() and END() functions.

  1. Tying scalar:::
  2. Tying arrays:::
  3. Tying hashes:::
  4. Tying filehandle:::

——————————————————————————————————————————————————

Arrays::::::

perl array tying

Hashes::::::

perl hash tie

FileHandles::::::

perl filehandle tying

Read Full Post »