954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Perl file path

Hi everyone!

Is there a function in Perl which is similar to the following

getServletContext().getRealPath("/filepathInContext")

which is in Java?

The getRealPath method returns a String containing the real path for a given virtual path .

I can't seem to find one.
Any help would be much appreciated.
TIA.

gutchi
Newbie Poster
7 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
 


Please explain your input and the function returned or expected output...

k_manimuthu
Junior Poster in Training
93 posts since Jun 2009
Reputation Points: 55
Solved Threads: 24
 
Please explain your input and the function returned or expected output...

I have "/index.jsp" as the "/filepathInContext". When i pass it on the getRealPath method, it returns the file path (i.e http://host/contextPath/index.jsp )..

From the result, I'm going to extract the path using Perl's substr and index functions.
I will then use the result and append it to the filename of a new jsp file in that same path. Then I can have, http://host/contextPath/result.jsp .

I'd be happy to answer any clarifications since I'm not sure if I explained that correctly.

gutchi
Newbie Poster
7 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
 


I don't know the how the reply helpful to you. But I hope you will get a bit. Below I place the sample code for you. For better understand purpose do the below steps and execute the program
1) Create path like 'e:\dani\delim\path\example'
2) Place the perl program
3) Create the file like 'e:\dani\japh.pl'
4) Run the perl file.From the result, I'm going to extract the path using Perl's substr and index functions. From the result, I'm going to extract the path using Perl's substr and index functions.
No need to use substr function here. Better you have use the 'File::Basename' module. That gives the base directory and file name.
The 'abs_path' gives the absolute path of the given file.

use File::Basename;
use Cwd 'abs_path';

# Get the Absolute path a particular file
my $path =  abs_path("../../../japh.pl" );
print "\nabs path  : ", $path;

# Get the base directory of the file
my $base_dir = dirname($path);
print "\nbase path : ", $base_dir;

# Append another one file to the base dir
my $another_file = "$base_dir/myfile";
print "\nAnother file : ", $another_file;
k_manimuthu
Junior Poster in Training
93 posts since Jun 2009
Reputation Points: 55
Solved Threads: 24
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: