Perl script to Hyperlink to open a file based on the name of the file in each cell - perl

I had developed a perl script to look for matching strings from text files and copied it into a spread sheet, I had also included the text file name into the spreadsheet for corresponding matching string. Now I want to hyperlink to open the file by clicking the file name in each cell.
Any help would be much appreciated! I'm not much of a programmer... Thanks!

Here is some examples by using the Spreadsheet::WriteExcel
# External link to a local file
$worksheet->write('B2', 'external:Italy.xls');
# External link to a local file with worksheet
$worksheet->write('B3', 'external:Italy.xls#Sales!B3');
# External link to a local file with worksheet and alternative string
$worksheet->write('B4', 'external:Italy.xls#Sales!B4', 'Link');
# External link to a local file with worksheet and format
$worksheet->write('B5', 'external:Italy.xls#Sales!B5', $format);
# External link to a remote file, absolute path
$worksheet->write('B6', 'external:c:/Temp/Asia/China.xls');
# External link to a remote file, relative path
$worksheet->write('B7', 'external:../Asia/China.xls');
# External link to a remote file with worksheet
$worksheet->write('B8', 'external:c:/Temp/Asia/China.xls#Sales!B8');
# External link to a remote file with worksheet (with spaces in the name)
$worksheet->write('B9', q{external:c:/Temp/Asia/China.xls#'Product Data'!B9});
You can see the write API for more information.

Related

Perl Net::FTP not lists particular file

I am trying to list the file of remote host using Net::FTP Perl module. Later I want to download it using put command.
Will it possible to list the particular file using ls command
Syntax I am using is:
$remote_dir = "/home/user/test_dir/";
$ftp->cwd($remote_dir);
$file_name = "test.txt";
print $ftp->ls($file_name);
This file is exists in remote server when I tried checking it manually. But listing is not happening.
ftp->ls(); is used for only listing the directory contents or can it be used to list the particular file from a directory as I mentioned above?
ftp->ls(); is used for only listing the directory contents or can it be used to list the particular file from a directory
From the documentation:
ls ( [ DIR ] )
Get a directory listing of DIR, or the current directory.
Thus, it is clearly documented to list a directory, not a specific regular file.

WWW::Mechanize : How to upload file under different file name?

How to make WWW::Mechanize upload file under different file name?
I would like web server to see/record file name different from file name on my computer.
my $file = [
'filename-on-disk.txt', # The file you'd like to upload.
'filename-for-upload.txt', # The filename you'd like to give the web server.
'Content-type' => 'text/plain' # Any other flags you'd like to add go here.
];
$mech->post("http://example.com/upload.cgi", [
'upload' => $file
]);
Taken from:
https://gist.github.com/gaurav/253111#file-file-upload-pl
You can use a hard link:
link "file_name_on_your_computer","desired_new_name";
# Code to upload the newly created link
unlink "desired_new_name";
Notes
You need not worry about disk space usage if you're using a large file: the file is not duplicated, the hard link is simply a new name for the same inode.
This solution is limited to just those filesystems that support hard links. If you're on Windows, this may end up creating and uploading only a shortcut to your original file.

How to extract .gz file with .txt extension folder?

I'm currently stuck with this problem where my .gz file is "some_name.txt.gz" (the .gz is not visible, but can be recognized with File::Type functions),
and inside the .gz file, there is a FOLDER with the name "some_name.txt", which contains other files and folders.
However, I am not able to extract the archive as you would manually (the folder with the name "some_name.txt" is extracted along with its contents) when calling the extract function from the Archive::Extract because it will just extract the "some_name.txt" folder as a .txt file.
I've been searching the web for answers, but none are correct solutions. Is there a way around this?
From Archive::Extract official doc
"Since .gz files never hold a directory, but only a single file;"
I would recommend using tar on the folder and then gz it.
That way you can use Archive::Tar to easily extract specific file:
Example from official docs:
$tar->extract_file( $file, [$extract_path] )
Write an entry, whose name is equivalent to the file name provided to disk. Optionally takes a second parameter, which is the full native path (including filename) the entry will be written to.
For example:
$tar->extract_file( 'name/in/archive', 'name/i/want/to/give/it' );
$tar->extract_file( $at_file_object, 'name/i/want/to/give/it' );
Returns true on success, false on failure.
Hope this helps.
Maybe you can identify these files with File::Type, rename them with .gz extension instead of .txt, then try Archive::Extract on it?
A gzip file can only contain a single file. If you have an archive file that contains a folder plus multiple other files and folders, then you may have a gzip file that contains a tar file. Alternatively you may have a zip file.
Can you give more details on how the archive file was created and a listing of it contents?

How to give current directory as Path name while reading an Excel file?

I am trying to update an already existing Excel file "Test.xls" Now, instead of giving the complete path of the Test.xls file, I wish to know how to read the Test.xls file from current directory. I am using Eclipse in Windows environment. Below is my code:
my $Excel = Win32::OLE->GetActiveObject('Excel.Application')
|| Win32::OLE->new('Excel.Application', 'Quit');
my $Book = $Excel->Workbooks->Open("Test.xls"); #This line throws an error but works if I give complete path name as D:/eclipse/workspace/testing/Test.xls
my $Sheet = $Book->Worksheets(1);
foreach my $data (#ifrules)
{
$Sheet->Cells($row,$col)->{'Value'} =$data;
$row++;
}
$Book->Close;
I have tried various options like ./Test.xls, .\Test.xls and others too.
From what I found, you can use getcwd() to get the current working directory. You can then concatenate that with the name of the file within $Excel->Workbooks->Open() to give the current directory plus the name of the file.
Source Link
Explanation: The basic issue here is that you are passing the file name to Excel, which doesn't know about Perl's current working directory. That is why you have to find out the full path name and give it to $Excel->Workbooks->Open().

How to handle this situatiuon in Perl

I am having a configuration INI file to store all configuration required for my script to run. I have a Logger.PM which uses Log4Perl, and ConfigReader.PM which reads the INI file and stores the value in global variable. My Start.PL is the entry point where i call the methods from Logger and configreader.
What I do currently
In Start.PL I hardcoded the INI file path
In Logger.Pm I harcoded the directory name where log files should be stored
What I want
I want the INI file path as configurable
I want the log folder path to be taken from the INI file
I could do this by following
Pass the INI file path as a parameter to the start.pl
Read the INI file and get the folder path from INI file
What I could face is that
I cannot use the Logger.PM in ConfigReader (partially) since the
folder name required for logger is part of INI file
I want to log every step of my script (for logging/debugging purpose in case of failure. )
I can use print but this will write to console and to capture i need to use >>log.txt. Then i will be forced to maintain 2 logs for my application which is not what I wanted
Anyone have a good solution for this scenario?
You can pass INI file path in command line using Getopt::Long, and command line switches for istance:
Start.pl --ini=/path/to/INI_file
Here is a code sample to show what changes are needed in Start.pl, in order to have switches:
#!/usr/bin/env perl
use v5.12;
use strict;
use Getopt::Long;
# That little tiny 's' after 'ini=' is for string
GetOptions ( 'ini=s' => \my $ini_file );
say $ini_file;
After this change, you can read all options from your INI file, including log folder path ( are you already using a module to manage INI files like Config::IniFiles? ).
There is something still unclear in your question about print: although one of my master said that print with a pair of square brackets is the best debugger in the world, why use print when you have set up Log::Log4perl?
When you say that Logger.PL can't be used in ConfigReader, are you referring to the log object?