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.

Recommended Answers

All 3 Replies

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

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.

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;
commented: Thanks again for the great answer k manimuthu. +2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.