Perl "require" question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl "require" question
# 1  
Old 11-03-2012
Perl "require" question

So a script can tell where it's located by the $0 variable right? OK

So what if another script calls "require" on it? Does calling $0 still refer to where it's located? Apparently not, it refers to the script that called require on it. So when that required script tries to call require on other scripts using relative paths, those requires fail if the requiring script is not found in the same directory.

Is there a way to fix this? Thanks

---------- Post updated at 03:03 PM ---------- Previous update was at 12:26 PM ----------

Nevermind it looks like __FILE__ fixes my problem.
# 2  
Old 11-03-2012
Quote:
Originally Posted by stevensw
Is there a way to fix this?
Yes: never ever use relative paths in a script. That is an absolute no-no. This is not only true for perl but any language.

I hope this helps.

bakunin
# 3  
Old 11-03-2012
Quote:
Originally Posted by bakunin
Yes: never ever use relative paths in a script. That is an absolute no-no. This is not only true for perl but any language.

I hope this helps.

bakunin
Relative paths are nice when you want to be able to move bundles of files around and still have them work though.

Also nice when you want to create development instances of piece of code by for instance, putting it in CVS, checking it out somewhere else and still have it function fine.
# 4  
Old 11-03-2012
You don't use relative paths even then. Use a "master path" to which you add parts to maintain a certain sub-tree. You can change the "master path" to change tree locations.

Example:

Code:
mypath="/some/where"
logpath="$mypath/log"
cfgpath="$mypath/etc"
xxxpath="$mypath/somewhere/else"

etc.

I hope this helps.

bakunin
# 5  
Old 11-03-2012
Yes but if you have more than one file all these files have to use this master path. You have to change it in each of these files one at a time. You only have a single point of edit if it's one file. I'd rather not even have any points of edit when moving a bundle of files.

Or have them use the file with the master path in it, but again then we'd be using relative paths to accomplish that.

For files that apply to all instances of a program I'm not against using absolute paths though.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

3. Shell Programming and Scripting

[Solved] "Which Perl" Usage Question . . .

Greetings! Just a basic syntax question :) Today, I want to launch a perl script from the command line with an explicit path to wherever perl may be installed on any particular system. In my bumblings, I came up with this: which perl | /etc/something.plOf course it doesn't work; but I was... (2 Replies)
Discussion started by: LinQ
2 Replies

4. Shell Programming and Scripting

Perl: Use variables in "require" file

So lets say I have: 1.pl: $a = 1; $b = 2; require "2.pl"; 2.pl print $a; How to make 1.pl's variables available to 2.pl? Thanks (2 Replies)
Discussion started by: stevensw
2 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. Shell Programming and Scripting

perl: question about the regex "=~"

Hello all Is there a "not" reversal method for the =~ regex thingy in perl ? for example, in the snippet below, i have placed a ! in front of the =~ to "not it".. although it quite obviously doesn't work and is just me trying to get across the question in a way that somebody may understand :o... (2 Replies)
Discussion started by: rethink
2 Replies

7. UNIX for Advanced & Expert Users

A question/problem about oracle "tns listener" and "enterprise manager"

hi, I have a problem about the Oracle related components. I'm not able to find any answer yet, and waiting for your responses... Here is the configuration of my system: * an IBM P550 machine, * an AIX 5.3 running on it and * an oracle database, already installed on it. The problem (or... (1 Reply)
Discussion started by: talipk
1 Replies

8. UNIX and Linux Applications

A question/problem about oracle "tns listener" and "enterprise manager"

hi, I have * an IBM P550 machine, * an AIX 5.3 running on it and * an oracle database, already installed on it. The problem (or question of my own) is: Oracle tns listener, "CT_LISTENER", and the enterprise manager (EM) of the instance, which is uniq instance and called... (0 Replies)
Discussion started by: talipk
0 Replies

9. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question