#define in perl


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers #define in perl
# 1  
Old 10-31-2007
#define in perl

Hi friends,

I am not sure if perl questions can be raised here. Smilie
But I have a doubt if there is a way to do "#define" in perl, like in C.

Does anyone know if it is feasible (without CPAN modules)?

Thanks,
Srini
# 2  
Old 10-31-2007
Please understand that #define is a preprocessor directive. Strictly, it is not related to C.

If all you want is named constants, you can try the constant pragma:

constant - perldoc.perl.org
# 3  
Old 10-31-2007
Offering more info abt my scenario..

Hi,
Thanks for your quick suggestion. I grazed over the 'constant' pragma.
From the given link,
Code:
 it's probably not correct to put a constant declaration inside of a conditional statement

My main aim is to use the constant in a 'if' condition.
To elaborate, my scenario is:
Code:
if(defined($input_file)) {
  open(FH,"<$input_file);
  #define INPUT "FH";
} else {
  #define INPUT "STDIN";
}
while ($line = <INPUT>) {
...
}

Is there an apposite mechanism to replace that "#define"?

Thanks,
Srini
# 4  
Old 11-01-2007
check if Constant::Runtime module is useful in your case
# 5  
Old 11-01-2007
Quote:
Originally Posted by srinivasan_85
Hi,
Thanks for your quick suggestion. I grazed over the 'constant' pragma.
From the given link,
Code:
 it's probably not correct to put a constant declaration inside of a conditional statement

My main aim is to use the constant in a 'if' condition.
To elaborate, my scenario is:
Code:
if(defined($input_file)) {
  open(FH,"<$input_file);
  #define INPUT "FH";
} else {
  #define INPUT "STDIN";
}
while ($line = <INPUT>) {
...
}

Then that is not strictly a "constant".

Why do you have to do it like that? You can take a reference of a filehandle and then use it with the slurp ( <> ).

Code:
if(defined($input_file)) {
  open(FH,"<$input_file);
  $input = \*FH;
} else {
  $input = \*STDIN;
}
while ($line = <$input>) {
...
}

# 6  
Old 11-01-2007
Thanx a lot!

@YOGESH => Thanx for the suggestion. But since I don't have permissions to use CPAN modules, I am not able to try it out as well.
@CBKIHONG => Your solution works perfectly. Thanks a lot!
Also, please enlighten me if "\*" is used to get the address of a symbol and "\$" to get the address of a variable.

Since I'm a beginner in perl, my queries might sound frivolous.

Thanks,
Srini
# 7  
Old 11-01-2007
Quote:
Originally Posted by srinivasan_85
Also, please enlighten me if "\*" is used to get the address of a symbol and "\$" to get the address of a variable.
Mostly, yes, although the whole truth is more complicated. Look it up from the camel books when you have reached that far.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Define Variables

Hi, I just define the variable in script and use those script in another script but the variable not recognize. test1.sh #!/bin/bash DB="test_db" USR="test_user" PWD="test_pwd" HST="24.254.87.12" test2.sh #!/bin/bash ./test1.sh mysql -u $USR -p $PWD -h $HST... (2 Replies)
Discussion started by: fspalero
2 Replies

2. Programming

When to define functions in C?

Hey everyone. So I'm looking at a few C programming resources, and it seems, by convention how you should write and define a function, is first declare it's existence before your main...then call it somewhere in your main, and then define after, at the end of the program? Is this necessary? I mean... (7 Replies)
Discussion started by: Lost in Cyberia
7 Replies

3. Shell Programming and Scripting

In Perl can i define a hash with value as variable?

Hi, Is it possible in perl to have a hash defined with variables as theirs key values, like: %account = ('username' => 'boy', 'password' => $password); Thanks (1 Reply)
Discussion started by: zing_foru
1 Replies

4. Programming

#define in c

Hi, I had a head file, looks like #define MIN_NUM 10 #define MAX_NUM 10 is there any way to get "MAX_NUM" from 10? thanks. peter (9 Replies)
Discussion started by: laopi
9 Replies

5. Programming

help with #define in C

if i do this in C #define NUM 1234512345 then how come i cant print it out using int main(int argc, char **argv) { printf("%d\n", NUM); return 0; } well the result is -1219236538, why isnt it 1234512345 ? (7 Replies)
Discussion started by: omega666
7 Replies

6. Programming

#define

Hello, I would like to conditionaly comment in my code source some fields from arrays. So I use the property ## from the #define definition. my code: ... #define slet /##* #define etsl *##/ ... const T_SVT_ADLL_A653_DESC A_DESC = { { slet qwerty etsl SLICING,... (3 Replies)
Discussion started by: cypleen
3 Replies

7. Shell Programming and Scripting

Define multiple mail recipents in a variable in perl

hi I have a perl script from which I call a shell script and pass mail variable to it. The mail works fine if I give 1 recipient but fails for multiple. conv.pl:- $mialing = "anu\@abc.com" rest.sh $mialing rest.sh mail -s "hi" $1 This works fine But I need to define multiple... (2 Replies)
Discussion started by: infyanurag
2 Replies

8. Programming

mysterious #define

in the header file orville.h, outside of the #ifdef #endif , there is the following #define JOB_CONTROL /* support job-control */ As you can see, the JOB_CONTROL macro has no value associated with it. Here is what I go when I ran grep on the entire source code. $ grep -iR... (6 Replies)
Discussion started by: frequency8
6 Replies
Login or Register to Ask a Question